2

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);

1 Answer 1

9

There is no need for manual queries.

You can load a file using file_load($fid), and then the path is in $file->uri. It might look a bit stringe ('public://something.jpg'), but that's a so called stream wrapper, and it works just fine with plain PHP file functions.

It's not exactly clear to me what you are trying to achieve; if you want to render an image field from a node/user/..., there are better ways to embed title and things like that, but this would work if you really just have a field. (That you are trying to query the field data table suggests otherwise, though.)

<?php
  $file = file_load($fid);
  $variables = array(
    'path' => $file->uri,
    'style_name' => 'your_style_name',
  );
  print theme('image_style', $variables);
?>
5
  • i want to up your score, but I am too new to the forum. But thank you very much! I think you answered the question for me. In looking at the new edits, I wonder if there are ways to avoid using the database call? The end product is a colorslider and an interactive map that relates products to a location and a color. But maybe there is a more efficient way to make this database call or to avoid it?
    – Starfs
    Commented Jan 4, 2012 at 1:56
  • @Starfs If you're worried about performance, you can either cache the resulting HTML yourself using the cache API or you can e.g. rely on drupal.org/project/entitycache (and maybe a different cache backend like memcache).
    – Berdir
    Commented Jan 4, 2012 at 8:31
  • Since I need to do a database call and the only access I have to the information is based on the data returned in the call, is there any way to utilize this ( $file = file_load($fid); ) within the foreach loop that creates my json element?
    – Starfs
    Commented Jan 4, 2012 at 17:37
  • Nevermind, I figured it out. $json = array(); foreach ($query as $row) { // get the image fid $fid = $row->thefid; $file = file_load($fid); // create the output as an array $jsonelement=array( 'title' => $row->title, 'swatchimgid' =>$row->thefid, '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);
    – Starfs
    Commented Jan 4, 2012 at 19:31
  • You can also use file_load_multiple($array_of_$fids), this will only do a single db query to fetch all files.
    – Berdir
    Commented Jan 5, 2012 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.