PHP tables consisting of 3 rows loaded from SQL database

I have a database loaded with different churches information, I am trying to insert all the information from the database into a PHP table with 3 rows.

I would like the structure of each cell to be:

Church Name
Image
Pastor Name

I can easily insert all data into a table, but I cannot get it to display as 3 rows.

echo("<table>");
while($row = mysql_fetch_array($rs))
{
        echo("<tr>");
        echo("<td>");
        echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
        echo("<img src=\"" . $row['image'] . "\"><br>");
        echo($row['pastorName'] . "<br><br>");
        echo("</td>");
        echo("<td>");
        echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
        echo("<img src=\"" . $row['image'] . "\"><br>");
        echo($row['pastorName'] . "<br><br>");
        echo("</td>");echo("<td>");
        echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
        echo("<img src=\"" . $row['image'] . "\"><br>");
        echo($row['pastorName'] . "<br><br>");
        echo("</td>");
        echo("</tr>");

}
echo("</table>");

Doing this causes me to have 3 rows in correct structure, but having duplicate data. I understand that I have not changed the id, but am not sure what I should do

Answers


You're repeating the data on the row. If you want to show 3 items per row, you need to add a counter and a statement to draw the table row breaks, as below:

$int_Col = 1;
echo("<table>");
while($row = mysql_fetch_array($rs))
{
    if ($int_Col == 1) {
       echo("<tr>");
    }

    echo("<td>");
    echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
    echo("<img src=\"" . $row['image'] . "\"><br>");
    echo($row['pastorName'] . "<br><br>");
    echo("</td>");

    if ($int_Col == 3) { 
        echo("</tr>");
        $int_Col = 1;
    } else {
      $int_Col++;
    }
}

if ($int_Col > 1) { 
   echo("<td colspan='". (3 - $int_Col) ."'>&nbsp;</td></tr>");
}
echo("</table>");

The last check ($int_Col > 1) should ensure the table is rendered properly with an empty cell - should span the correct amount of cells that were not drawn on the current row.


Just clarify, I think there's two definitions of "rows" here.

  • MySQL table rows
  • HTML table rows

I believe you're after getting 3 MySQL table rows into one HTML table row.

Use the following to split the HTML row every 3 MySQL rows:

$i = 0;
while($row = mysql_fetch_array($rs))
{
    // whatever you're already doing(ish)

    $i++;
    if($i % 3 == 0)
    {
         echo('</tr><tr>');
    }
}

You'll need to put some extra checks in in the event that the total number of MySQL rows doesn't divide exactly by 3 because in that case you'll have one or two empty cells to fill at the end of the table.


If I get your code right you are creating 3 rows for every row in the database. mysq_fetch will run the loop for every single row, so the right content of the loop would be:

    echo("<tr>");
    echo("<td>");
    echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
    echo("<img src=\"" . $row['image'] . "\"><br>");
    echo($row['pastorName'] . "<br><br>");
    echo("</td>");
    echo("</tr>");

Need Your Help

PHP Reading XML Issue

php xml parsing

I need your help once again!

How to install a plugin from github?

ruby-on-rails git

I have tryed to run this code in my console: