cgm225 Posted February 5, 2008 Share Posted February 5, 2008 I have a MySQL database with an albums table that contains rows, each with an album name in it. I want to take all the entries of that table, let's say thirty total entries/rows, and echo them all in three xHTML lists (i.e. three progressive groups of ten). Could someone help me accomplish this? I know how to echo all the entries, but do not know how I would echo the groups of ten list items within three sets of <ul> tags. Any ideas? Thank you all in advance! Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/ Share on other sites More sharing options...
cgm225 Posted February 6, 2008 Author Share Posted February 6, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459381 Share on other sites More sharing options...
mmarif4u Posted February 6, 2008 Share Posted February 6, 2008 Did u mean want to show 10 records per page. Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459382 Share on other sites More sharing options...
cgm225 Posted February 6, 2008 Author Share Posted February 6, 2008 No, 10 records per xhtml list, with all three lists on the same page.. Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459385 Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 Set three 'for' loops: echo "<ul>"; for($i = 0; $i < 10 && $i < count($mysqlResults); $i++) { echo "<li>"; echo $mysqlresults[i]; echo "</li>"; } echo "</ul>"; echo "<ul>"; for($i = 10; $i < 20 && $i < count($mysqlResults); $i++) { //same as last 'for' loop } echo "</ul>"; Do the same thing for your third loop. $mysqlResults has to be an array with the fetched results of your msqyl query. Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459402 Share on other sites More sharing options...
cgm225 Posted February 6, 2008 Author Share Posted February 6, 2008 Ok, I understand your logic and this is definitely on the right track (and thank you for your help!); however, while I gave the example of 30 entries in the initial post, it could very well be 130 rows/entries. Therefore, is there a way to make a more dynamic loop that will repeat 10 item lists regardless of the total number of entries (maybe some sort of loop within a loop?)? In other words, I don't want to have to keep manually adding loops when more entries are added. Also, to be more specific about my actual project, here is the basic code I am using to output my album entries currently. How would you use this mysql result in that type of loop? $query = "SELECT * FROM albums ORDER BY timestamp DESC"; $album_results = mysql_query($query); while($row = mysql_fetch_array($album_results)) { $id = $row['id']; $full_title = $row['full_title']; $short_title = $row['short_title']; $path = $row['path']; $dir = $row['dir']; echo "$id | $full_title | $short_title | $path$dir<br />"; //For this post and simplicity sake, this entire echoed line could be one list item } Thank you so much for all the help already! Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459430 Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 In that case I would do it differently: $query = "SELECT * FROM albums ORDER BY timestamp DESC"; $album_results = mysql_query($query); $i = 0; echo "<ul>"; while($row = mysql_fetch_array($album_results)) { $id = $row['id']; $full_title = $row['full_title']; $short_title = $row['short_title']; $path = $row['path']; $dir = $row['dir']; echo "<li>$id | $full_title | $short_title | $path$dir</li>"; //For this post and simplicity sake, this entire echoed line could be one list item $i += 1; if($i == 10) { echo "</ul><ul>"; } } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459446 Share on other sites More sharing options...
cgm225 Posted February 6, 2008 Author Share Posted February 6, 2008 Brilliant! Thank you so much! I did have to modify your last if statement, but, regardless, this has been so helpful! The final code: $query = "SELECT * FROM albums ORDER BY timestamp DESC"; $album_results = mysql_query($query); $i = 0; echo "<ul>"; while($row = mysql_fetch_array($album_results)) { $id = $row['id']; $full_title = $row['full_title']; $short_title = $row['short_title']; $path = $row['path']; $dir = $row['dir']; echo "<li>$id | $full_title | $short_title | $path$dir</li>"; //For this post and simplicity sake, this entire echoed line could be one list item $i += 1; if(is_int($i/10)) { echo "</ul><ul>"; } } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459462 Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 Heh, oh ya, I didn't test it. Mine would have only broken off the first 10 replies! Quote Link to comment https://forums.phpfreaks.com/topic/89608-solved-outputecho-mysql-data-from-table-rows-to-xhtml-lists-in-groups-of-six/#findComment-459495 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.