Jump to content

PHP loop to populate table question


yyx748

Recommended Posts

i need to populate my table in this way but i dont seem to get it working.

 

4 columms. rows depend on number of items.

 

pic1  text1  pic2  text2

pic3  text3  pic4  text4

pic5  text5  pic6  text6

 

==================

this are my sql codes

 

$SQLstring = "Select * from filmo WHERE id='$id'";

$result = @mysqli_query($conn, $SQLstring);

$Row = mysqli_fetch_assoc($result);

 

====my variables====

$Row['img'] /// image

$vRow['text'] //// text

 

i need the codes for the table like using for loop or while loop to make a table with those items inside.. thanks!!

Link to comment
https://forums.phpfreaks.com/topic/128865-php-loop-to-populate-table-question/
Share on other sites

Not tested, so there might be some errors.

 

$records_per_row = 2;

$SQLstring = "Select * from filmo WHERE id='$id'";
$result = @mysqli_query($conn, $SQLstring);

if (!result)
{
    echo "There was an error running the query<br /><br />";
    echo "Query: $SQLstring<br /><br />";
    echo "Error: " . @mysqli_error();
    exit();
}


if (mysqli_num_rows()==0)
{
    echo "There were no results";

}
else
{
    $current_row_rec = 1;

    echo "<table>\n";
    while ($Row = mysqli_fetch_assoc($result))
    {
        if ($current_row_rec > $records_per_row)
        {
            echo "</tr>\n";
            $current_row_rec = 1;
        }

        if ($current_row_rec == 1)
        {
            echo "<tr>\n";
        }

        echo "<td>{$Row['img']}</td>\n";
        echo "<td>{$Row['text']}</td>\n";
        $current_row_rec++;
    }

    while ($current_row_rec <= $records_per_row)
    {
        echo "<td></td>\n";
        echo "<td></td>\n";
        $current_row_rec++;
    }

    echo "</table>\n";
}

I think it's because I added some code to complete the row if there was not enough records to fill a row, but I forgot the ending TR tag. Change the last while loop as followsL

 

    if ($current_row_rec != $records_per_row+1)
    {    while ($current_row_rec <= $records_per_row)
        {
            echo "<td></td>\n";
            echo "<td></td>\n";
            $current_row_rec++;
        }
        echo "</tr>\n";
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.