Jump to content

Counting ItemID, Displaying Count and Name of Item, Using Dynamic URLs


CloudSex13

Recommended Posts

Hi, thanks for reading.

 

I have two tables in a database: Users, and Items.

 

In the table Items, I have the fields ItemID, ItemName, and ItemType.

 

In the table Users, I have a field called Inventory. The Inventory holds data similar to, for example, "1,2,5,2,2,87,8,43" - and the numbers in that user's inventory represents the ItemID of the item they have.

 

On the page itself, I have links, linking to the corresponding ItemType, for example:

 

example.com/page.php?request=hats

example.com/page.php?request=shows

 

When one of those links is clicked, I'm trying to get the Inventory list from that user (which I've done), extract the ItemID's from that inventory list, organize them accordingly by name as data on the page, and then display a number of how many of the same items are in that user's inventory list. For example:

 

T-Shirt (3) (where ItemID in the above inventory example would be "2")

 

But even trickier, where I really mess myself up, is that next to each outputted item, I'm hoping to have a link that says "view" and would link to something such as: example.com/page.php?request=view, and I could just get that items description.

 

I know, this sounds like a lot, but I'm completely baffled on the part where I could use GET and functions to organize results. I've accomplished the above without dynamic links, but I really need dynamic links now (preference) and below is my current code:

 

function countArray($val, $array) {

$i = 0;

  if (!is_array) {

  return "Uh-oh! An error occurred retrieving your inventory. Don't worry, it will be back shortly.";

  } else {

    foreach ($array as $v) {

      if ($val == $v) {

      $i++;

      }

    }

  return $i;

  }

}

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

$listed = array();

foreach ($playerItems as $item) {

  if (!in_array($item, $listed)) {

  $playerItemsQuery = mysql_query("SELECT * FROM Items WHERE ItemID = '$item' ORDER BY ItemName");

  $totalfood = 1;
  $totaldrinks = 1;
  $totalshirts = 1;
  $totalpants = 1;
  $totalshoes = 1;

    while ($getPlayerItems = mysql_fetch_array($playerItemsQuery)) {

    $itemName = $getPlayerItems['ItemName'];
    $itemType = $getPlayerItems['ItemType'];

      if ($itemType == "Food") {

        if ($totalfood == 1) {

        $food .= "<div class=\"leftSplit\"><img src=\"none\" width=\"35\" height=\"35\" /> ".$itemName." (".countArray($item, $playerItems).") <a href=\"inventory.php?action=equip&itemid=&sid=\" class=\"small\">[Eat]</a></div>";
        $listed[] = $item;
        $totalfood = 2; 

        } else { 

        $food .= "<div class=\"rightSplit\"><img src=\"none\" width=\"35\" height=\"35\" /> ".$itemName." (".countArray($item, $playerItems).") <a href=\"inventory.php?action=equip&itemid=&sid=\" class=\"small\">[Eat]</a></div>";
        $listed[] = $item;
        $totalfood = 1; 

        }
      }
    }
  }
}

 

Any ideas...

 

Anyone? :/

Link to comment
Share on other sites

Normalize your database

http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

 

DO NOT STORE CSV DATA

Create a lookup table or something.  The downsides of your approach would be having to edit the entries if something changes.  You have to change it for EVERY row that contains the value, rather than one pointer row (perhaps incorrect use of language).

Link to comment
Share on other sites

Hey guys,

 

I read that article, and really, I cannot see how I could apply it to what I'm trying to do.

 

All I'm trying to do is associate numbers in a user's Inventory field (i.e. 1,2,3,4,87,) to ItemIDs in the item table, count how many there are, display them as data on a page, but I cannot figure out how to display them across functions, because I have a function with counts the array, but you cannot put a function in a function, right?

 

I just can't quite grasp the knowledge of how this could be done and if there is an easier way. Is there a PHP function on how to count how many of the same string there is in an array?

 

Anyone? :/

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.