Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:47:58+00:00 2026-06-09T02:47:58+00:00

I’ve been reading some q&a but still haven’t managed to build my algorithm, so

  • 0

I’ve been reading some q&a but still haven’t managed to build my algorithm, so I’m asking for some help/suggestions on how to.

So, I have a MySQL table and for each mysql table row I want to display it in a table. I want my table to have only 4 cols (meaning the number of rows is variable depending on the data in the database). So, in html table’s (row 1, col 1) would be the data from db table row 1, html (row 1, col 2) data from db table row 2 … (row 2, col 1) data from db table row 5 and so on.

So, the table would be something like:

[data from db row #1] [data from db row #2] [data from db row #3] [data from db row #4]

[data from db row #5] [data from db row #6] [data from db row #7] [data from db row #8]

and so on…

This is what I have to get the data from db:

function get_albums() {
    $albums = array();
    
    $albums_query = mysql_query("SELECT 'albums'.'album_id', 'albums'.'role_id', 'albums'.'timestamp', 'albums'.'name', LEFT('albums'.'description',50) as 'description', COUNT('images'.'image_id') as 'image_count'
    FROM 'albums'
    LEFT JOIN 'images'
    ON 'albums'.'album_id' = 'images'.'album_id'
    GROUP BY 'albums'.'album_id'");
        
    // Loop through all images
    while ($albums_row = mysql_fetch_assoc($albums_query)) {
        // multidimensional array
        $albums[] = array(
                    // associative array
                    'id' => $albums_row['album_id'],
                    'role' => $albums_row['role_id'],
                    'timestamp' => $albums_row['timestamp'],
                    'name' => $albums_row['name'],
                    'description' => $albums_row['description'],
                    'image_count' => $albums_row['image_count']
        );
    }   
    return $albums;
}

To display that info:

$albums = get_albums();

echo '<table>';
for ($i=0; $i<(count($albums)/4); $i++) {
    echo '<tr>';

    //HERE IS WHERE I'M LOST

    foreach ($albums as $album) {
        echo '<a href="view_album.php?aid=',$album['id'],'">',$album['name'],'</a> (',$album['image_count'],') image(s)<br />',$album['description'],'...';

    }
    echo '</tr>';
}
echo '</table>';

So, any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:47:59+00:00Added an answer on June 9, 2026 at 2:47 am
    // keep calculations outside of the for loop if you can
    
    // this will display all albums (count($albums) / 4) times.
    
    $limit = count($albums) / 4;
    for ($i = 0; $i < $limit; $i++) {
        echo '<tr>';
    
        foreach ($albums as $album) {
            echo '<td><a href="view_album.php?aid=' . 
                $album['id'] . '">' . $album['name'] . '</a> (' . 
                $album['image_count'] . ') image(s)<br />' . 
                $album['description'] . '...</td>';
        }
        echo "</tr>";
    }
    

    What you’re probably looking for is

    <?php
    $counter = 0;
    
    foreach($albums as $album) :
        // start new row 
        if ($counter == 0): ?>
            <tr>
        <?php endif; ?>
        <td><a href="view_album.php?aid=<?= $album['id'] ?>"><?= $album['name'] ?></a> (<?= $album['image_count'] ?>) image(s)<br />         
            <?= $album['description'] ?>...</td>
        <?php if ($counter++ == 3) : ?> <!-- 4 records printed in row; end row & reset counter -->
            </tr>
        <?php
            $counter = 0;
        endif;
    endforeach;
    
    $lastCount = $counter;
    // print remaining empty cells
    while ($counter ++ < 4) : ?>
        <td>&nbsp;</td>
    endwhile;
    // close row if necessary
    if ($lastCount != 3) :
        </tr>
    endif;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.