By farald on
Hi!
Noob alert!
I am using custom nodetypes with custom fields on my site.
I have one newbie question regarding mysql output from a custom nodetype using custom nodefields.
I am using this script to access the database to output a block with nodetitles plus date, slightly modified from one I found on this site:
$listlength="3";
$charlength="250";
$taxo_id = 27;
$content_type = 'snippet';
unset ($output);
$result1 = pager_query("SELECT n.title, n.nid,
nr.teaser, n.uid, n.created, u.name FROM {node} n join {node_revisions} nr on (n.nid = nr.nid and n.vid = nr.vid) INNER JOIN {users} u ON n.uid = u.uid INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$content_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created DESC", $listlength);
while ($node = db_fetch_object($result1)) {
$output .= "<a href='?q=node/" . $node->nid . "/print' rel='gb_page_center[500, 300]'>" . $node->title . "</a><br>" . format_date($node->created, 'small') . substr(strip_tags($node->teaser), 0, $charlength) ."<br><br>";
}
$output .= "";
print $output;
I have a nodetype called snippet, which contains a custom textfield called "medieselskap". this is called to by the system using this phrase:
print $node->field_medieselskap[0]['view']
So my noob_question is this: how do I get the above script to output this value?
Thanks in advance:)
Comments
Some pointers on how you might fetch the field
But first a suggestion, you should really change 'pager_query' to 'db_query_range' as the use of 'pager_query' may/will cause problems if the page you are looking at has paged content. pager_query is used to page a subset of data while db_query_range simply limits the number of records returned (what you want in this case).
As for getting 'field_medieselskap', there are two basic ways:
1) Get the full node using node_load(), so just inside the while loop (before you set $ouput) add
$node = node_load($node->nid);. The plusses are it is easy, and if the field type does something special it will set the view value. The negative is it is slower than adding the field to the query, though probably not enough when dealing with three nodes.2) Modify the query to directly fetch the field data. The change here depends on how your field is stored. There are two cases. There will be a table called 'content_type_snippet' and their may be a column called 'field_medieselskap', in this case you could modify the query like this
In this case we add content_type_snippet to the set of tables and select the field field_medieselskap. The unprocessed value is accessed as $node->field_medieselskap.
Depending on the field type it may be stored in it's own field type in a table that in this case would be called 'content_field_medieselskap'.
In this case we add 'content_field_medieselskap' to the list of tables. The fields to fetch (**** above) will depend on the content type.
Drawback to either of these approaches is they depend on how CCK stores the data.