Djsvell Djsvell - 1 month ago 7
MySQL Question

MySQL output in PHP tables

I am currently designing a product selection page.

I wanna show my users a series of products that I have in my SQL table.

I want to output my SQL into php tables, but I dont want one long table.

I need one table per product.

In short terms,

Product 1, in one table

Break

Product 2, in other table.

<?php
$con=mysqli_connect("localhost","root","","headsets");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `modeller");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>


The sql part works fine, its just the way php output it I want to change

krasipenkovs code

My code

Answer

Try with this:

<?php
$con=mysqli_connect("localhost","root","","headsets");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `modeller`");

$i = 0;
while($row = $result->fetch_assoc())
{
    echo "<table border='1'>";
    //if ($i == 0) {
      //$i++;
      //echo "<tr>";
      //foreach ($row as $key => $value) {
        //echo "<th>" . $key . "</th>";
      //}
      //echo "</tr>";
    //}
    echo "<tr>";
    foreach ($row as $value) {
      echo "<td>" . $value . "</td>";
    }
    echo "</tr>";

    echo "</table><br />";
}
mysqli_close($con);
?>
Comments