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

Link to comment
Share on other sites

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

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.