Jump to content

[SOLVED] Output/echo MySQL data from table rows to xHTML lists in groups of six <li>


cgm225

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>";

 

 

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.