CloudSex13 Posted May 7, 2009 Share Posted May 7, 2009 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? :/ Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Why do so many people use CSV in one DB cell? o.O Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 7, 2009 Author Share Posted May 7, 2009 It's compact and logical. Need I say more? Would anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 It's *not* logical to me and it's actually going to make your queries that much more of a pain. Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 Well, what would you suggest then? Would anyone have any possible hints or suggestions for me? :/ Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 8, 2009 Share Posted May 8, 2009 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). Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 Yeah, but if I did this, how would each user's inventory be stored if I don't use CSV? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 How far down did you read in that article? Just wondering. Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 Not far, just an immediate question. To the article! Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 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? :/ Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 Argh. Anyonee? :/ I just need some knowledge slapped in my face. Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 8, 2009 Author Share Posted May 8, 2009 Like, would this even be possible to do? Is there actually an array counting function of the same strings contained in an array? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 You can just loop through the array and count them. Quote Link to comment Share on other sites More sharing options...
CloudSex13 Posted May 9, 2009 Author Share Posted May 9, 2009 Is there a certain PHP function that would do that? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 You can try Google-ing, but there isn't one that I know of. I guess you can use array_count_values, but it's a little more than what you need. Quote Link to comment 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.