In Drupal 7, I'm trying to retrieve the image path for a given fid.
If I am running a query such as the following, that is fine and dandy and returns a bunch of numbers that relate to the images on each row.
$query = db_query("Select s.field_swatch_image_fid as fid from {field_data_field_swatch_image} s");
foreach ($query as $row) {
$swatch_fid => $row->fid .'<br />';
}
However, the table contains no information as to how to retrieve the actual image. Does anyone know the secret to pull the actual image URL into the foreach loop, or is there some sort of render function that needs to be run on the fid values? I'm guessing it is easy, but I can't seem to find any documentation on this.
Also, if I wanted to apply a style to the images, would that be a necessary step if the style was already set on the field image itself, and if so, what would be that process?
EDIT: here is the full query. I was doing it manually because I have to retrieve the value and send a json object back to jquery and then render it in jquery. But this is kindof the start to a different and larger question.
Declare database variables to be used in the database call
- so far this was easier than looking at all the giant field names Drupal creates in the database
- hoping to rewrite the query in the new improved drupal 7 style soon
*/
$swatchcolor = "field_data_field_swatch_color"; // sc - swatch color data table
$color = "field_swatch_color_value";
$states = "field_data_field_state"; // s the table that holds the swatch's reference to the state
$state = "field_state_value";
$swatchref = "field_data_field_swatch_ref"; // sr - swatch reference target table
$target = "field_swatch_ref_target_id";
$imagetable = "field_data_field_swatch_image"; // swatch image ref
$parent_ref = "field_product_ref_target_id";
// If a swatch has not been selected, run this query and select all colors as options
if($swc == '') {
$query = db_query ("Select n.nid AS nid, n.title AS title, s.$state AS state
FROM {node} n
INNER JOIN {$swatchref} sr ON sr.$target = n.nid
INNER JOIN {$states} s ON s.entity_id = sr.entity_id
WHERE (s.$state IN $theregion)
GROUP BY title");
} else {
$query = db_query ("Select n.nid AS nid, n.title AS title, sc.$color AS color, s.$state AS state
FROM {node} n
INNER JOIN {$swatchcolor} sc ON sc.entity_id = n.nid
INNER JOIN {$swatchref} sr ON sr.$target = n.nid
INNER JOIN {$states} s ON s.entity_id = sr.entity_id
WHERE (sc.$color = '$swc') AND (s.$state IN $theregion)
GROUP BY title");
}
EDIT!! Here is how the image path was integrated into the json output:
$json = array();
foreach ($query as $row) {
// get the image fid
$fid = $row->thefid;
$file = file_load($fid);
// $file = str_replace('public:\/\/', '', $file1);
// create the output as an array
$jsonelement=array(
'title' => $row->title,
'path' => $file->uri,
'style_name' => 'swatch'
);
array_push($json,$jsonelement);
}
// encode to json
$jsonstring = drupal_json_encode($json);
//output the json string
drupal_json_output($jsonstring);