Dynamic table [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Dynamic table [SOLVED]

Post 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
Last edited by YoussefSiblini on Sat Dec 01, 2012 6:23 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dynamic table

Post 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>';
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Dynamic table

Post by Benjamin »

Easiest is to use divs float left with width set in a container with a fixed width.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Dynamic table

Post 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
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Dynamic table

Post by YoussefSiblini »

Hi,
social_experiment this is adding the same product three times in one line.

Joe
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dynamic table

Post 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.
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: Dynamic table

Post by YoussefSiblini »

I used Benjamin idea which worked fine, thank you all for your help :)
Post Reply