Page 1 of 1

PHP table question.

Posted: Wed Sep 16, 2009 7:41 am
by timecatcher
Ok well I have my script set up so that it will echo out 10 of the values found in a specific database table. These values are echoed into a HTML table, however at the moment they all just go in one like strait down. Im wanting to know if its possible to make it so that I could get like 4 values to appear per line then goes down to the next bit for another 4 and so on, to make 12 in total.

Like this:

{} {} {}
{} {} {}
{} {} {}

Instead of this:
{}
{}
{}
{}

Heres the script at the moment:

Code: Select all

<?php
require("../includes/config.php");
echo "$open";
if(isset($loggedout))
{
die("$loggedout");
}
//Makes the username's first letter uppercase.
        $ucusername = ucfirst($username);
echo "<h1>$ucusername's House Storage</h1>";
//Checks in the 'inventory' whether there are any items belonging to the user with the ID set to their account.
$query = mysql_query("SELECT * FROM storage WHERE userid='$id' LIMIT 10");
    while($rows = mysql_fetch_array($query))
        {
        $itemid = $rows['itemid'];
        $itemsquery = mysql_query("SELECT * FROM items WHERE id='$itemid'");
//
        while($rows2 = mysql_fetch_array($itemsquery))
            {
            $itemimage = $rows2['image'];
            $itemname = $rows2['name'];
            $itemdescription = $rows2['description'];
            }
            if(mysql_num_rows($query) =< 3)
                {
echo<<<echo
                <center><table><tr ><td width='80'><img src='http://kurukolands.co.uk/images/items/$itemimage'><br /></td><tr><font color='green'><b>$itemname</b></font></tr></tr></table></center>
echo;
                }
            if(mysql_num_rows($query) =< 3)
                {
echo<<<echo
                <center><table><tr ><td width='80'><img src='http://kurukolands.co.uk/images/items/$itemimage'><br /></td><tr><font color='green'><b>$itemname</b></font></tr></tr></table></center>
echo;
                }
        }
echo "$close";
?>
Thanks, in advance. Im sure im just missing something silly, but my table knowledge has always been a bit pants.

Re: PHP table question.

Posted: Wed Sep 16, 2009 7:55 am
by superdezign
timecatcher wrote:Like this:

{} {} {}
{} {} {}
{} {} {}

Instead of this:
{}
{}
{}
{}
I didn't read any further than this, so excuse me if this doesn't answer your question. If you are using a loop to output, put your table rows on the outer loop and the table data on the inner loop.

Code: Select all

echo '<table>';
 
for ($i = 0; $i < $foo; $i++) {
    echo '<tr>';
 
    for ($j = 0; $j < $bar; $j++) {
        echo '<td>' . $data . '</td>';
    }
    echo '</tr>';
}
echo '</table>';

Re: PHP table question.

Posted: Wed Sep 16, 2009 8:00 am
by timecatcher
Well yes im looping but I want only 12 results from the database to be displayed per page, in the order I showed.

{} {} {} {}
{} {} {} {}
{} {} {} {}

Is there anyway to specify so that the results come out like that, with 4 results per line, before the pagination kicks in on the 13th result.

Hope that made sense.

Thanks again.

Re: PHP table question.

Posted: Wed Sep 16, 2009 8:05 am
by superdezign
Well, if you are sure that it will always be twelve, and you know that you want 3 rows and 4 columns, then you would replace $foo with 3 and $bar with 4. $data would be your data from the database.

Re: PHP table question.

Posted: Wed Sep 16, 2009 8:15 am
by timecatcher
Ok thanks.

Re: PHP table question.

Posted: Wed Sep 16, 2009 9:52 am
by timecatcher
Ok I've asked a friend to help me out a bit and im still confused, and not getting any further.

Here is the code as it looks now:

Code: Select all

<?php
require("../includes/config.php");
echo "$open";
if(isset($loggedout))
{
die("$loggedout");
}
//Makes the username's first letter uppercase.
        $ucusername = ucfirst($username);
echo "<h1>$ucusername's House Storage</h1>";
//This does...
 
//Checks in the 'inventory' whether there are any items belonging to the user with the ID set to their account.
$query = mysql_query("SELECT * FROM storage WHERE userid='$id' LIMIT 10");
 
$x = 0;
while($rows = mysql_fetch_array($query))
        {
        $itemsquery = mysql_query("SELECT * FROM items WHERE id=$rows[itemid]");
        $rows2 = mysql_fetch_array($itemsquery);
        $y = $x % 5;
    if ($y == 0)
    {
    echo "<tr>";
    }
echo "<td><p align=center><img src='http://kurukolands.co.uk/images/items/$rows2[image]'><br /><font color='green'><b>$rows2[name]</b></font></p></td>";
if ($y == 4)
    {
        echo "</tr>";
    }
    $x++;
}
    echo "</table>";
        
echo "$close";
?>
It still just lines the whole lot up. Rather than the 4 by 3 rectangle I want them to be.

Re: PHP table question.

Posted: Wed Sep 16, 2009 10:29 am
by peterjwest
I took out the MySQL so I could test it. You were missing the <table> tag. I also simplified and generalised your code somewhat.

Code: Select all

<?php
$array = array(1,2,3,4,5,6,7,8,9,10,11,12);
echo "<table>";
$nCols = 4;
$x = 0;
foreach($array as $rows) {
    if ($x % $nCols == 0) { echo "<tr>"; }
    echo "<td><p>$rows</p></td>";
    if (($x + $nCols + 1) % $nCols == 0) { echo "</tr>"; }
    $x++;
}
echo "</table>";    
?>