bigkev1983 Posted April 13, 2008 Share Posted April 13, 2008 Hey guys I'm putting together a results page, which you can see a sample of here: http://88.208.206.63/riseoverseas/search.php?country=1 I'd like the results to be in a GRID format and I used some code from the Internet to do this. Unfortunately, instead of the data appearing as a grid, the table just repeats downwards instead. This is the code for the table/repeat region. Can anyone please let me know where I'm going wrong here? <?php $searchquery = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $searchresult = mysql_query($searchquery); while($searchrow = mysql_fetch_array($searchresult)) { ?> <table > <tr> <?php $searchrow_endRow = 0; $searchrow_columns = 3; // number of columns $searchrow_hloopRow1 = 0; // first row flag do { if($searchrow_endRow == 0 && $searchrow_hloopRow1++ != 0) echo "<tr>"; ?> <td> <table class="dsR23" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="dsR24"> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo($searchrow['img01']); ?>" alt="" border="0" /><br /> <br /> <?php echo($searchrow['town']); ?></div> <div class="txt_bodysearch"> <p><?php echo($searchrow['title']); ?><br /> <?php echo($searchrow['price']); ?><br /> Projected yield <?php echo($searchrow['yield']); ?></p> </div> </td> </tr> </table> </td> <?php $searchrow_endRow++; if($searchrow_endRow >= $searchrow_columns) { ?> </tr> <?php $searchrow_endRow = 0; } } while ($row_searchrow = mysql_fetch_assoc($searchrow)); if($searchrow_endRow != 0) { while ($searchrow_endRow < $searchrow_columns) { echo("<td> </td>"); $searchrow_endRow++; } echo("</tr>"); }?> </table> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 13, 2008 Share Posted April 13, 2008 I'm not too sure what all those <table> tags are about... But take a look at this thread/FAQ here Oh, and also, when you post you code can you use the tags please - makes it much easier to read. Quote Link to comment Share on other sites More sharing options...
bigkev1983 Posted April 13, 2008 Author Share Posted April 13, 2008 ??? <?php $searchquery = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $searchresult = mysql_query($searchquery); while($searchrow = mysql_fetch_array($searchresult)) { ?> <table > <tr> <?php $searchrow_endRow = 0; $searchrow_columns = 3; // number of columns $searchrow_hloopRow1 = 0; // first row flag do { if($searchrow_endRow == 0 && $searchrow_hloopRow1++ != 0) echo "<tr>"; ?> <td> <table class="dsR23" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="dsR24"> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo($searchrow['img01']); ?>" alt="" border="0" /> <?php echo($searchrow['town']); ?></div> <div class="txt_bodysearch"> <p><?php echo($searchrow['title']); ?> <?php echo($searchrow['price']); ?> Projected yield <?php echo($searchrow['yield']); ?></p> </div> </td> </tr> </table> </td> <?php $searchrow_endRow++; if($searchrow_endRow >= $searchrow_columns) { ?> </tr> <?php $searchrow_endRow = 0; } } while ($row_searchrow = mysql_fetch_assoc($searchrow)); if($searchrow_endRow != 0) { while ($searchrow_endRow < $searchrow_columns) { echo("<td> </td>"); $searchrow_endRow++; } echo("</tr>"); }?> </table> Quote Link to comment Share on other sites More sharing options...
bigkev1983 Posted April 13, 2008 Author Share Posted April 13, 2008 Thanks - I must confess I tried using that PHP script but couldn't get it to work with my data. OK, so now I have two examples. The first is: http://88.208.206.63/riseoverseas/search-repeater.php?country=1 In this example, as you can see the region repeats correctly but with the same data for each one (code below - I left the select query at the top as well just in case that has something to do with it). <?php $searchquery = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $searchresult = mysql_query($searchquery); while($searchrow = mysql_fetch_array($searchresult)) { ?> <table > <tr> <?php $searchresult_endRow = 0; $searchresult_columns = 3; // number of columns $searchresult_hloopRow1 = 0; // first row flag do { if($searchresult_endRow == 0 && $searchresult_hloopRow1++ != 0) echo "<tr>"; ?> <td> <table class="dsR23" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="dsR24"> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo($searchrow['img01']); ?>" alt="" border="0" /><br /> <br /> <?php echo($searchrow['town']); ?></div> <div class="txt_bodysearch"> <p><?php echo($searchrow['title']); ?><br /> <?php echo($searchrow['price']); ?><br /> Projected yield <?php echo($searchrow['yield']); ?></p> </div> </td> </tr> </table> </td> <?php $searchresult_endRow++; if($searchresult_endRow >= $searchresult_columns) { ?> </tr> <?php $searchresult_endRow = 0; } } while ($row_searchresult = mysql_fetch_assoc($searchresult)); if($searchresult_endRow != 0) { while ($searchresult_endRow < $searchresult_columns) { echo("<td> </td>"); $searchresult_endRow++; } echo("</tr>"); }?> </table> <br /> <?php } ?> In the example below, the region repeats with the correct data but only repeats downward http://88.208.206.63/riseoverseas/search-repeater.php?country=1 Here's the code: <?php $searchquery = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $searchresult = mysql_query($searchquery); while($searchrow = mysql_fetch_array($searchresult)) { ?> <table > <tr> <?php $searchrow_endRow = 0; $searchrow_columns = 3; // number of columns $searchrow_hloopRow1 = 0; // first row flag do { if($searchrow_endRow == 0 && $searchrow_hloopRow1++ != 0) echo "<tr>"; ?> <td> <table class="dsR23" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="dsR24"> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo($searchrow['img01']); ?>" alt="" border="0" /><br /> <br /> <?php echo($searchrow['town']); ?></div> <div class="txt_bodysearch"> <p><?php echo($searchrow['title']); ?><br /> <?php echo($searchrow['price']); ?><br /> Projected yield <?php echo($searchrow['yield']); ?></p> </div> </td> </tr> </table> </td> <?php $searchrow_endRow++; if($searchrow_endRow >= $searchrow_columns) { ?> </tr> <?php $searchrow_endRow = 0; } } while ($row_searchrow = mysql_fetch_assoc($searchrow)); if($searchrow_endRow != 0) { while ($searchrow_endRow < $searchrow_columns) { echo("<td> </td>"); $searchrow_endRow++; } echo("</tr>"); }?> </table> <br /> <?php } ?> Go gentle on me, I'm a graphic designer by trade so a lot of this web stuff is very new for me right now Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Im a bit confused as to why we have a while loop and a do-while loop. However, it should be a simple matter of taking the code from the FAQ/thread i posted above, and putting your query and and data into it: <?php $query = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; echo "<td>"; ?> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo $img01; ?>" alt="" border="0" /><br /> <br /> <?php echo$town; ?></div> <div class="txt_bodysearch"> <p><?php echo $title; ?><br /> <?php echo $price; ?><br /> Projected yield <?php echo $yield; ?></p> </div> <?php echo "</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> You might want to try the above on a separate page, to make sure nothing else is interefering. Then put it back in this page to get it working how you want. Quote Link to comment Share on other sites More sharing options...
sasa Posted April 14, 2008 Share Posted April 14, 2008 change last part if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> to if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo '</tr>'; } ?> </table> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Yes, you're right - otherwise the code will create a new row unnecessarily. I also forgot to open the table tag. That makes the code: <table> <?php $query = "SELECT * FROM `property` WHERE `country` = ".$_GET['country']; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; echo "<td>"; ?> <div class="txt_titlesearch"> <img class="dsR13" src="images/property/<?php echo $img01; ?>" alt="" border="0" /><br /> <br /> <?php echo$town; ?></div> <div class="txt_bodysearch"> <p><?php echo $title; ?><br /> <?php echo $price; ?><br /> Projected yield <?php echo $yield; ?></p> </div> <?php echo "</td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; echo '</tr>'; } ?> </table> Quote Link to comment Share on other sites More sharing options...
bigkev1983 Posted April 14, 2008 Author Share Posted April 14, 2008 You beauties! Thanks guys - you're heroes! Kev 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.