Page 1 of 1

Layout of pictures issue.

Posted: Wed Sep 16, 2009 2:15 pm
by timecatcher
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Ok we'll earlyer I had issues with the layout of these pictures for a script im writing. Since then however it has worked out to an extent but now even if I only have two pictures in the database it shows a lot of them I don't really know how to explain very well. But just tell me if theres more you need to know.

Heres the code:

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");
 
while($rows = mysql_fetch_array($query))
        {
        echo "<table>";
        $itemsquery = mysql_query("SELECT * FROM items WHERE id=$rows[itemid]");
        $rows2 = mysql_fetch_array($itemsquery);
        for($x = 0; $x < 3; $x++)
            {
            echo "<tr>";
            for($c = 0; $c < 4; $c++)
                {
                echo "<td><img src='http://kurukolands.co.uk/images/items/$rows2[image]'><br /><font color='green'>$rows2[name]</font></td>";
                }
            echo "</tr>";
            }
        echo "</table>";
        }
echo "$close";
?>
Thanks.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Layout of pictures issue.

Posted: Wed Sep 16, 2009 5:13 pm
by pickle
Your query is retrieving all rows for a particular item. I'm assuming the intended result is to retrieve all the pictures for a given item.

However, you only retrieve a row from your result set once, and store it in $rows2. Your 2 for() loops are then iterating as they should, but only dealing with $rows2 - which will only ever hold one row from your result set. You need to put your mysql_fetch_array() call inside your second for() loop. You'll also need to deal with the situation where the number of rows you have isn't a multiple of 4.

Re: Layout of pictures issue.

Posted: Thu Sep 17, 2009 1:35 am
by timecatcher
Alright thanks that helped a little, but any idea how I should set that out then please? Since Im slightly confused as to where I put the fetch_array, and what bits I change at the moment. Sorry.

Thanks, TC.

Re: Layout of pictures issue.

Posted: Mon Sep 21, 2009 2:10 pm
by pickle
Well, think about what fetch_array() is doing. It's retrieving the next row from the result set. Your 2 nested for() loops are attempting to make a 3 x 4 grid of pictures. To make that grid, you need to iterate through your result set one row at a time so you can place the row's data in the correct spot in the grid.

Re: Layout of pictures issue.

Posted: Mon Sep 21, 2009 2:17 pm
by superdezign
Basically, think about when you need to get the next result in your code. Make sure that once you are finished with one row, you move on to the next one.