Page 1 of 1

Dynamic table [SOLVED]

Posted: Fri Nov 30, 2012 5:36 pm
by YoussefSiblini
Hi,
I want to output products details from my database into the page, I am using this:

Code: Select all

<?php
$connect = mysqli_connect("localhost","root" ,"", "database");
$query = "select * FROM products";
$res = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($res))
{
    echo "description: " . $row[6] . "<br/><br/>";
}
?>
This is outputting all the products underneath each other, how can I output 3 products beside each other and the other underneath the same 3 beside each other?


Thanks in advance :)


Joe

Re: Dynamic table

Posted: Fri Nov 30, 2012 5:43 pm
by social_experiment
I'm thinking a for loop inside the while loop

Code: Select all

<?php
echo '<table>';
while ($row = mysqli_fetch_array($res))
{
   echo '<tr>';
   for ($i=1; $i<=3; $i++)
    echo "<td>description: " . $row[6] . "</td>";
   }
   echo '</tr>';
}
echo '</table>';
?>

Re: Dynamic table

Posted: Fri Nov 30, 2012 5:50 pm
by Benjamin
Easiest is to use divs float left with width set in a container with a fixed width.

Re: Dynamic table

Posted: Sat Dec 01, 2012 6:02 am
by YoussefSiblini
Hi Guys,

Thank you, both solutions works perfect, I have question to social_experiment, wouldn't i be more than 3 so how the for loop keep working, sorry I just want to understand this so I don't keep asking.

Joe

Re: Dynamic table

Posted: Sat Dec 01, 2012 2:03 pm
by YoussefSiblini
Hi,
social_experiment this is adding the same product three times in one line.

Joe

Re: Dynamic table

Posted: Sat Dec 01, 2012 5:43 pm
by califdon
We don't know what fields are in your database table, so we can't guess what it is you want to show in each row. If it's only the description, you don't need to write "Select * From products", in fact you should specify just the columns you need, then that will determine what values i should have in the for statement.

Re: Dynamic table

Posted: Sat Dec 01, 2012 6:23 pm
by YoussefSiblini
I used Benjamin idea which worked fine, thank you all for your help :)