Jump to content

Handling the first result of a query differently to subsequent results


saturday79

Recommended Posts

Hi,

 

I am returning a set of image URLs and metadata from a search in a database. What I'd like to do is take the first result of that search, display the image and give it a name, then display only thumbnails for the other images returned by the search (thumbnail URL is also a returned item), but have the ability for users then to toggle which image is shown large and which are shown as thumbnails. In order to do this, I need to know how to do something different with the first result returned by a search compared with the rest. I'm happy to fiddle around if someone could post a link to where that's explained? Hopefully I've posted this question in the right area, apologies if not.

 

Many thanks in advance!

There are various ways you could do what you want, which is best kind of depends on the overall goal of the code and how much the first row would vary compared to the rest.

 

One way is to just fetch the first row, then do your loop for the rest.

$first=$result->fetch();
//Process first row
while ($row=$result->fetch()){ 
   //Subsequent rows
}
Another way is to use a flag which is handy if the processing changes only slightly.

$first=true;
while ($row=$result->fetch()){
   if ($first){
      $first=false;
      //first row stuff
   }
   //further processing
}
You could also just fetch all the results into an array then modify index [0] as necessary.

$data = $results->fetchAll();
//do something with $data[0].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.