Renlok Posted January 9, 2007 Share Posted January 9, 2007 $items is the items you own, at the moment it will only show one, how would i go about making this script show every itme in the users inventory?[code]echo "Your inventory<p>";$userid = $row['id'];$youritemsrow = mysql_fetch_array(mysql_query("SELECT * FROM item_possesion WHERE userid='$userid'"));$youritemid = $youritemsrow['itemid'];$itemsrow = mysql_fetch_array(mysql_query("SELECT * FROM items WHERE id='$youritemid'"));$realitemid = $itemsrow['itemid'];$itemsdata = mysql_fetch_array(mysql_query("SELECT * FROM itemnames WHERE id='$realitemid'"));$itemname = $itemsdata['name'];$itemimage = $itemsdata['image'];$items .=<<<ITEM <img src"/images/$itemimage" alt="$itemname"><br> $itemnameITEM;echo $items;[/code]thanks in advence Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/ Share on other sites More sharing options...
Philip Posted January 9, 2007 Share Posted January 9, 2007 Try using mysql_fetch_array and a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-156355 Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 ******MODIFIED BECAUSE I SEE YOUR PROBLEM******I started to recode this but meh..the problem lies in the fact that you are only calling in the end to echo 1 item based upon a specific ID.You need to use a foreach loop on your $youritemsrow which holds all the items of the userid.******MODIFIED BECAUSE I SEE YOUR PROBLEM****** Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-156359 Share on other sites More sharing options...
Renlok Posted January 9, 2007 Author Share Posted January 9, 2007 oh right thanks so it sould be something like:[code]echo "Your inventory<p>";$userid = $row['id'];$youritemsrow = mysql_fetch_array(mysql_query("SELECT * FROM item_possesion WHERE userid='$userid'"));$youritemidrow = $youritemsrow['itemid'];foreach ($youritemidrow as $youritemid){$itemsrow = mysql_fetch_array(mysql_query("SELECT * FROM items WHERE id='$youritemid'"));$realitemid = $itemsrow['itemid'];$itemsdata = mysql_fetch_array(mysql_query("SELECT * FROM itemnames WHERE id='$realitemid'"));$itemname = $itemsdata['name'];$itemimage = $itemsdata['image'];$items .=<<<ITEM <img src"/images/$itemimage" alt="$itemname"><br> $itemnameITEM;echo $items;}[/code]one more question if you stick the items in a table how would you put it so theres only four items showing in each row. Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-156631 Share on other sites More sharing options...
Renlok Posted January 9, 2007 Author Share Posted January 9, 2007 ok with what i entered before i get the errorWarning: Invalid argument supplied for foreach() in /home/renlok/public_html/roe/shop/inventory.php on line 8and ive no idea why Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-156635 Share on other sites More sharing options...
HuggieBear Posted January 9, 2007 Share Posted January 9, 2007 OK, that's because $youritemidrow isn't an array. You'll need to use a while loop to loop through $youritemsrow.If you're not sure how then let me know.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-156649 Share on other sites More sharing options...
Renlok Posted January 10, 2007 Author Share Posted January 10, 2007 no idea lol could you show me how :) Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-157172 Share on other sites More sharing options...
HuggieBear Posted January 10, 2007 Share Posted January 10, 2007 OK, first of all don't nest your functions, it not only makes it harder to read, but also harder to debug... Below is a generic example of a while loop as opposed to a foreach loop...[code]<?php// Specify the query and run it against the database$sql = "SELECT * FROM item_possesion WHERE userid = '$userid'";$result = mysql_query($sql) or die("Can't run $sql:<br><br>\n" . mysql_error());// Loop through the resultswhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ // Your code goes here}?>[/code]As for how to integrate the code you already have, I'd say scrap the three queries you have and use joins to get your result set in one hit. If you're not sure about this then google the term "sql join". It will make working with your results easier and place less overhead on your server.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-157199 Share on other sites More sharing options...
Renlok Posted January 10, 2007 Author Share Posted January 10, 2007 thanks but one last question about sql join ive got[code]$itemsql = "SELECT itemnames.name, itemnames.image FROM items,itemnames WHERE items.itemid=itemnames.id"$itemresult = mysql_query($itemsql) or die("Can't run $itemsql:<br><br>\n" . mysql_error());[/code]how to i get the results from it would it just be like with an array? Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-157289 Share on other sites More sharing options...
HuggieBear Posted January 10, 2007 Share Posted January 10, 2007 Exactly like that :)Huggie Quote Link to comment https://forums.phpfreaks.com/topic/33417-php-question/#findComment-157306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.