liam1412 Posted January 24, 2007 Share Posted January 24, 2007 Hello people.Would anybody be able to tell me how I can echo out a result set of 10 in to 2 rows of 5 etc. Any help will be much appreciated.Thanks Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2007 Share Posted January 24, 2007 I'm not sure I quite understand Quote Link to comment Share on other sites More sharing options...
craygo Posted January 24, 2007 Share Posted January 24, 2007 Can use this. Just change the query and set the number of rows you want.Not sure if you want to go down then across, or across then down. This will go down then across.[code]<table width=300 border=0 align=center><?php//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc$numcols = 5; // how many columns to display$numcolsprinted = 0; // no of columns so far// get the results to be displayed$query = "SELECT * FROM my_table ORDER BY my_field ASC LIMIT 10";$mysql_result = mysql_query($query);// get each rowwhile($myrow = mysql_fetch_assoc($mysql_result)){// set rows you want to view here$id = $myrow['id'];if ($numcolsprinted == $numcols) {print "</tr>\n<tr>\n";$numcolsprinted = 0;}// output row from databaseecho "<td>$id</td>\n";// bump up row counter$numcolsprinted++;} // end while loop$colstobalance = $numcols - $numcolsprinted;for ($i=1; $i<=$colstobalance; $i++) {}print "<TD></TD>\n";?></table>[/code]Ray Quote Link to comment Share on other sites More sharing options...
paul2463 Posted January 24, 2007 Share Posted January 24, 2007 set a counter in the while loop as well, then run an "if" statement to see if the count is equal to 5, if so then dont echo out the next line just do a carriage return. is that what you mean?[code]<?php$query = "Blah";$result = mysql_query($query) or die ("unable to run query". mysql_error());$count = 1;while ($row = mysql_fetch_assoc($result){ if ($count == 5){ echo "<BR>";}else{ echo $row['whatever'];}$count++;}?>[/code] Quote Link to comment Share on other sites More sharing options...
liam1412 Posted January 24, 2007 Author Share Posted January 24, 2007 Quicker than I thought. Yeah Thats what I meant. Thanks peepsLiam ;) 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.