Jump to content

[SOLVED] Query Will Not Fetch And Display Correct Array Variable Association


CloudSex13

Recommended Posts

For some reason, the variable $itemURL outputs only the last item fetched's URL, and displays that one URL for all data returned. I want each item's unique URL displayed at the end of the path instead. Could anyone see a reason as to why this does not work?

 

Something a little less important too that doesn't work is the ORDER BY ItemName code - it will not order the returned data by it's name either ASC or DESC.

 

Any suggestions? :/

 

$query = mysql_query("SELECT Inventory FROM Users WHERE Username='$cookie' LIMIT 1");
$getquery = mysql_fetch_assoc($query);
$inventory = substr($getquery['Inventory'], 0, -1);

$itemnames = array();

$query2 = mysql_query("SELECT * FROM Items WHERE ItemID IN (".$inventory.") AND ItemType='Box' ORDER BY ItemName");

$inventory = explode(",",$inventory);
$inventory= array_count_values($inventory);

while ($row = mysql_fetch_assoc($query2)) {

$itemnames[$row['ItemID']] = $row['ItemName'];
$itemurl = $row['ItemURL'];

}

foreach ($inventory as $key => $count) {

$stuff .= "<div class=\"item\"><img src=\"http://www.example.com/images/".$itemURL."\" /><strong>".$itemnames[$key]." (".$count.")</strong><br /><a href=\"action.php?do=remove&itemid=".$key."\">Remove</a></div>";

}

Link to comment
Share on other sites

You don't really need to go off the foreach loop if you want to sort like that. Just do it in the while loop. Also, it's good to define your variables outside of loops (like $stuff). One of the reason why I use a single quote to start off a PHP string is because I tend to use double quotes around HTML elements and starting a PHP string off with a single quote is helpful to avoid escaping double quotes.

 

Try this. I believe it should work.

$query = mysql_query("SELECT Inventory FROM Users WHERE Username='$cookie' LIMIT 1");
$getquery = mysql_fetch_assoc($query);
$inventory = substr($getquery['Inventory'], 0, -1);

$query2 = mysql_query("SELECT * FROM Items WHERE ItemID IN (".$inventory.") AND ItemType='Box' ORDER BY ItemName");

$inventory = explode(',',$inventory);
$inventory= array_count_values($inventory);

$stuff = '';
while ($row = mysql_fetch_assoc($query2)) {
     $stuff .= '<div class="item"><img src="http://www.example.com/images/"'.$row['ItemURL'].'" /><strong>'.$row['ItemName'].'('.$inventory[$row['ItemID']].')</strong><br /><a href="action.php?do=remove&itemid='.$row['ItemID'].'">Remove</a></div>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.