Jump to content

php question


Renlok

Recommended Posts

$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>
$itemname
ITEM;
echo $items;
[/code]

thanks in advence
Link to comment
Share on other sites

******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******
Link to comment
Share on other sites

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>
$itemname
ITEM;
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.
Link to comment
Share on other sites

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 results
while ($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.

Regards
Huggie
Link to comment
Share on other sites

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?
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.