I have the following code which does not seem to be working. As far as i can tell the predefined arrays are each of the same size – i have 32 rows in my table “homepage” where “username”, “image” and “website” are three fields. For testing purposes the usernames are 1 to 32. And the image and website fields are blank – at the moment (this will change).
The code i have is:
$sorthpge = mysql_query ("SELECT * FROM homepage ORDER BY no DESC LIMIT 32");
$links = array();
$images = array();
$usern = array();
$array_Length_1 = count($usern);
for ($i=0; $i<$array_Length_1; $i++)
{
while ($row_1 = mysql_fetch_assoc ($sorthpge)) {
$images[$i] = $row_1['image'];
$links[$i] = $row_1['website'];
$usern[$i] = $row_1['username'];
if($images[$i] == ""){
$images[$i] = "uploads/default.png";
$links[$i] = "register.php?no=";
}
else
{
if($images[$i] == "auction"){
$images [$i] = "uploads/auction.png";
$links[$i] = "auction.php?no=";
}
}
}
}
You can probably tell what i’m trying to do. As mentioned all the “image” rows are blank so i should be getting “$images[i] = “uploads/default.png” for all i up to 32. But nothing is showing in my html.
Just wondered if somebody could point out an error in my code. Or if my set up assumes something wrong. I’m pretty new to php. Thanks a lot in advance. P.S i will translate to mysqli when i can get these basics working.
In your sample code, you have the following:
As
$usernis empty,$array_Length_1is0– your loop is never executed.I’m not sure what your logic behind doing this was/is, so I don’t know a proper way to suggest to fix it, however, if you were to remove the
forloop entirely and store a separate incrementer,$i, the code should work fine.For instance, try updating your code to the following: