Snippet to get latest image based on taxonomy: Is there a more efficient way to do what I am doing?

kanthan - July 26, 2008 - 12:54

Hi all

I've been looking for a way to create a block which will display the latest image uploaded in a particular taxonomy category. So I eventually came up up with the following snippet which does what I want it to do. My question: Surely there's a "proper" Drupal way to do what I am doing? My three sql calls seems a bit wasteful. BTW, I'm not a programmer, so be gentle.

-K.

<?php
/**
* Display latest image based on a taxonomy category
* To change which taxonomy is listed, edit the $taxd_id number.
*/
$taxo_id = 17;
$list_no =1;
$image_size="_original";
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id LIMIT $list_no";
$result = db_query($sql);
while (
$anode = db_fetch_object ($result)) {
 
$kp_nid = $anode->nid;
  }
$sql = "SELECT fid FROM image WHERE nid = $kp_nid AND image_size = \"$image_size\" LIMIT $list_no";
$result = db_query($sql);
while (
$anode = db_fetch_object ($result)) {
 
$kp_fid = $anode->fid;
  }
$sql = "SELECT filepath FROM files WHERE fid = $kp_fid LIMIT $list_no";
$result = db_query($sql);
while (
$anode = db_fetch_object ($result)) {
 
$kp_filepath = $anode->filepath;
  }
$output = "<IMG SRC=\"/$kp_filepath\">";
return
$output;
?>

Personally I just use the

nevets - July 26, 2008 - 14:38

Personally I just use the taxonomy image module which allows you to associate an image with a term and provided a function for render the html based on a term.

Re: Personally I just use the...

kanthan - July 26, 2008 - 23:45

Well, duh...

Thanks Nevets. I'm looking at that as we speak.

 
 

Drupal is a registered trademark of Dries Buytaert.