mattennant Posted April 19, 2006 Share Posted April 19, 2006 hello therei'm not sure if this is dead simple or impossible. I'm trying to retrieve one set of query results from a database if one condition is met, otherwise i want to display another set of query results. to loop these results i am using a while loop. This is working for one condition, but i don't know how to make it work for the other condition. Sorry i don't know if this is the right terminology. Here's the code[code] <?php do { ?> <tr> <td valign="top"><img src=" <?php if ($colname_services_body == "-1") { echo $row_top_image['image']; }else{ echo $row_image['image']; } ?> " width="197" /></td> </tr> <?php } while ($row_image = mysql_fetch_assoc($image)); //after this i'm not sure tried things such as or while ($row_top_image = mysql_fetch_assoc($image)); ?>// but that ain't working[/code]any help much appreciated Quote Link to comment Share on other sites More sharing options...
Orio Posted April 19, 2006 Share Posted April 19, 2006 Try this:[code]<?php$i = 0;do { ?> <tr> <td valign="top"><img src=" <?php if ($colname_services_body == "-1") { echo $row_top_image['image']; $i++; }else{ echo $row_image['image']; $i++; } ?> " width="197" /></td> </tr> <?php } while ($row_image = mysql_fetch_assoc($image));if($i == 1){while ($row_top_image = mysql_fetch_assoc($image));}?>[/code]What basicly it does is setting a var called 'i' that its value is 0. In the do...while loop each time $i is being increased. If the loop runs just once (in case that the condition of the while is false), $i's value will be 1. If the loop runs more than once, its will be greater than 1. So I added the second while this way if($i == 1){second while};Hope it helps :)Orio. Quote Link to comment Share on other sites More sharing options...
poirot Posted April 19, 2006 Share Posted April 19, 2006 I think this will do:[code] <?php do { ?> <tr> <td valign="top"><img src=" <?php if ($colname_services_body == "-1") { while ($row_image = mysql_fetch_assoc($image)) { echo $row_image['image']; } } else { mysql_data_seek($image, 0); while ($row_top_image = mysql_fetch_assoc($image)) { echo $row_top_image['image']; } } ?> " width="197" /></td> </tr> <?php } [/code]What I don't understand though, is why you are doing this since $row_top_image['image'] and $row_image['image'] are exactly the same thing (will have the same values, because its the same result resource "$image").Also note that since you are fetching the same result more than once, I had to move the internal pointer back: mysql_data_seek():[a href=\"http://www.php.net/manual/en/function.mysql-data-seek.php\" target=\"_blank\"]http://www.php.net/manual/en/function.mysql-data-seek.php[/a] Quote Link to comment Share on other sites More sharing options...
mattennant Posted April 19, 2006 Author Share Posted April 19, 2006 thanks so much for your helpi think i may be seriously over complicating this businessin essence what i am trying to do is display 3 images (tagged firstpage) from a database when the user enters the page.ie. when the user hits www.domain.php the variable $colname_services_body is "-1"therefore display thes imagesecho $row_top_image['image']; selecting the first page images from the databaseif the user then goes to www.domain.php?RecordID=myvariablethen three images tagged with myvariable are displayedi'm guesing that i'm making a simple thing complicated, but it sure is doing my brain in and i'm not really sure if that makes it clear, thanks again mat Quote Link to comment 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.