saturday79 Posted February 24, 2014 Share Posted February 24, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/286491-handling-the-first-result-of-a-query-differently-to-subsequent-results/ Share on other sites More sharing options...
kicken Posted February 24, 2014 Share Posted February 24, 2014 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]. Quote Link to comment https://forums.phpfreaks.com/topic/286491-handling-the-first-result-of-a-query-differently-to-subsequent-results/#findComment-1470509 Share on other sites More sharing options...
saturday79 Posted February 24, 2014 Author Share Posted February 24, 2014 Fantastic, many thanks! I'll have a play with those and see which works best. Quote Link to comment https://forums.phpfreaks.com/topic/286491-handling-the-first-result-of-a-query-differently-to-subsequent-results/#findComment-1470513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.