2

I'm trying to create something like the following:

But my current code has given me:

The part in white is what I get with the code below, and the part in black is what I get when I add in two more <td> rows. The problem is I can't get 3 pieces of data per row to be created - only vertically or horizontally.

$result = mysqli_query($con,"SELECT * FROM show_listing");
while($row = mysqli_fetch_array($result))
{

echo "<table>

<tr>
<td>".$row['name']."'</td>
</tr>

</table>";
}

mysqli_close($con); 

It may be simpler than I think but does anyone know how I could do this?

1
  • Whenever possible, please use text instead of graphics. Commented Sep 24, 2013 at 15:48

2 Answers 2

1
$c = 0;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
    if ($c%3 == 0) {
        if ($c) echo "</tr>";
        echo "<tr>";
    }
    echo "<td>".$row['name']."</td>";

    $c++;
}
if ($c) echo "</tr>";
echo "</table>";
Sign up to request clarification or add additional context in comments.

Comments

0

You have the correct idea. You do not want the declaration in the while loop as the result is that you will get multiple tables.

So I moved the table declarations outside of the while loop. I also made the declarations conditional based on the current column. You can now set the variable $columns to however many columns you want.

$result = mysqli_query($con,"SELECT * FROM show_listing");
echo "<table>"
$columns=3;
$current_column=1;
while($row = mysqli_fetch_array($result))
{

if($current_column==1) {
    echo "<tr>";
}

echo "<td>".$row['name']."'</td>";

if($current_column==$columns) {
     echo "</tr>";
     $current_column=1;
} else 
     $current_column++;
}
echo "</table>

mysqli_close($con); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.