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!
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: 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.
<?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: Posting Code in the Forums to learn how to do it too.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
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.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
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.