Jump to content

Handling the first result of a query differently to subsequent results


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].
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.