CloudSex13 Posted May 12, 2009 Share Posted May 12, 2009 I came up with a script (below) that matches values in a given array to items in a table in a database, displays all the items on the page (only once per item), and what I'm trying to do currently is count how many of the items are the same in the array and display it next to that item. I'm not sure as to why this script will not count and display correctly the amounts or items, but I was wondering if anyone could give me some pointers or assistance to help me do so. Thank you kindly if so. $inventory = array("1,2,3,2,3,2,3,5,7,87677"); $items = explode(",", $inventory); $listed = array(); foreach ($items as $item) { if (!in_array($item, $listed)) { $query = mysql_query("SELECT * FROM Items WHERE ItemID='$item' AND ItemType='Box' ORDER BY ItemName"); $itemamount = array_count_values($items); while ($getquery = mysql_fetch_array($query)) { $itemname = $getquery['ItemName']; $itemid = $getquery['ItemID']; foreach ($itemamount as $theitemname => $theitemamount) { $allitems .= "<div class=\"item\">".$itemname." (".$theitemamount.")</strong>"; $listed[$theitemname] = $item; } } Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/ Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 1. $inventory is already of type array, so you shouldn't need to explode it. 2. Why is the element in your array a string? If you take out the quotes, the explode lines, and update the variables in the foreach loop, it should be fine. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832342 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Thanks for the reply! Actually, $inventory is a value from a field in a table on a database holding the following: "1,2,1,3,4,2,2,4,2," just like that. Updating my question now, I would have to keep explode there. Would everything you suggested still be wrong, or? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832656 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 1. Read up on DB normalization. DB CSV values fail! 2. Don't put that string in an array. Let $inventory be "1,2,1,..." and then explode should be fine. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832664 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 I did read up on DB normalization. That cannot work for what I'm trying to accomplish. Thank you, though. That string is not being put into an array. The foreach statement is the only thing not working... I can't seem to grasp my head around it. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832682 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Either you don't know DB normalization or you are not comfortable with SQL. But it will work with whatever you want to accomplish. $inventory = array("1,2,3,2,3,2,3,5,7,87677"); Well I see a string in an array. Can you take the array part out? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832690 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Oh, no, ignore that. $inventory is what the users Inventory is stored in the database. That field would have a value of "1,2,3,2,3,2,3,5,7,87677," As far as the database normalization goes, the article on it showed how to use it to link in a search. I have to store data that is consistent and maybe have a small portion removed or added in it. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832701 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Well you can remove rows in a DB table. Same deal. Anyways, what do you mean by ignore that. All I'm saying is if you remove the array() around that string, your foreach loop should be fine. I don't understand the problem here. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832705 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 I don't want rows removed in a table. It's a users inventory stored as one field in a table on a datase - how else could that be stored? I don't get it. Here's the actual code: $query = mysql_query("SELECT * FROM Users WHERE Username='$cookie' LIMIT 1"); $getquery = mysql_fetch_array($query); $inventory = $getquery['Inventory']; Does that make anymore sense, or? :/ Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832719 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Okay forget the DB thing I mentioned. Anyways, can you post what var_dump($inventory); outputs? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832723 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Outputs this: string(36) "3,3,3,1,1,1,1,1,2,2,2,2,2,2,2,1,2,3," Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832732 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 $query = mysql_query("SELECT * FROM Users WHERE Username='$cookie' LIMIT 1"); $getquery = mysql_fetch_array($query); $inventory = $getquery['Inventory']; $items = explode(",", $inventory); $listed = array(); foreach ($items as $item) { if (!in_array($item, $listed)) { $query = mysql_query("SELECT * FROM Items WHERE ItemID='$item' AND ItemType='Box' ORDER BY ItemName"); $itemamount = array_count_values($items); while ($getquery = mysql_fetch_array($query)) { $itemname = $getquery['ItemName']; $itemid = $getquery['ItemID']; foreach ($itemamount as $theitemname => $theitemamount) { $allitems .= "<div class=\"item\">".$itemname." (".$theitemamount.")</strong>"; $listed[$theitemname] = $item; } } Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832736 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 For some reason, that's giving an "Parse error: syntax error, unexpected $end" Hm? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832751 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Oh, I just copy and pasted. Put two } at the end. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832754 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Oh, gotcha. That outputs something like the following: SHIRT (2) SHIRT (6) SHIRT (8 ) SHIRT (1) PANTS (2) PANTS (6) PANTS (8 ) PANTS (1) SHIRT (2) SHIRT (6) SHIRT (8 ) And then it continues off screen - I'm baffled at this point. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832758 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 LMAO! That is awesome. Sorry, so what should the display look like? Also please post the updated code. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832773 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 lol, I know, right? In comparison to what it's displaying now: BOX1 (2) BOX1 (6) BOX1 (8 ) BOX1 (1) BOX2 (2) BOX2 (6) BOX2 (8 ) BOX2 (1) I want it to display: BOX1 (Actual amount of this box) BOX2 (Actual amount of this box) I changed: $listed[$theitemname] = $item; To: $listed[] = $item; And now it only displays 4 BOX1's and 4BOX2's for a total of 8 outputs, where it should only be 2, one for each box, and the number of that box next to it instead of so much output it flows off the screen. Updated code: $query = mysql_query("SELECT * FROM Users WHERE Username='$cookie' LIMIT 1"); $getquery = mysql_fetch_array($query); $inventory = $getquery['Inventory']; $items = explode(",", $inventory); $listed = array(); foreach ($items as $item) { if (!in_array($item, $listed)) { $query = mysql_query("SELECT * FROM Items WHERE ItemID='$item' AND ItemType='Box' ORDER BY ItemName"); $itemamount = array_count_values($items); while ($getquery = mysql_fetch_array($query)) { $itemname = $getquery['ItemName']; $itemid = $getquery['ItemID']; foreach ($itemamount as $theitemname => $theitemamount) { $allitems .= "<div class=\"item\">".$itemname." (".$theitemamount.")</strong>"; $listed[] = $item; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832786 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 I think there's a better way to get what you want. I gotta head home from work so I'll come up with something when I get home. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832793 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 No worries either way - I appreciate the dedicated assistance, man. Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832800 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Not tested, but try this - $query = mysql_query("SELECT Inventory FROM Users WHERE Username='$cookie' LIMIT 1"); $getquery = mysql_fetch_assoc($query); $inventory = substr($getquery['Inventory'], 0, -1); $inventory = explode(',',$inventory); $inventory = array_count_values($inventory); $item_names = array(); $query = mysql("SELECT ItemID, ItemName FROM Items WHERE ItemID IN (" . $inventory . ") AND ItemType='Box'"); while ($row = mysql_fetch_assoc($query)) { $item_names[$row['ItemID']] = $row['ItemName']; } foreach ($inventory as $key => $count) { echo '<div class="item">' . $item_names[$key] . ' (' . $count . ')</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832823 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 That seems to output nothing. Is there a way I could speak with you over MSN, Skype, or AOL about this? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832842 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 That seems to output nothing. I could be messing up the SQL. $query = mysql_query("SELECT Inventory FROM Users WHERE Username='$cookie' LIMIT 1"); $getquery = mysql_fetch_assoc($query); $inventory = substr($getquery['Inventory'], 0, -1); $inventory = explode(',',$inventory); $inventory = array_count_values($inventory); $item_names = array(); $sql = "SELECT ItemID, ItemName FROM Items WHERE ItemID IN (" . $inventory . ") AND ItemType='Box'"; $query = mysql_query($sql) or die(mysql_error() . ' @ ' . $sql); while ($row = mysql_fetch_assoc($query)) { $item_names[$row['ItemID']] = $row['ItemName']; } var_dump($item_names); var_dump($inventory); foreach ($inventory as $key => $count) { echo '<div class="item">' . $item_names[$key] . ' (' . $count . ')</div>'; } Any output? Is there a way I could speak with you over MSN, Skype, or AOL about this? Not currently. I'm not signed into any of those. Is this something that can't be said through the forums or PM? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832845 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Yeah, I PMed you but you never replied back. Error: Unknown column 'Array' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832849 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 I edited my post. You may have gotten to it a bit earlier. Can you copy and paste once more and tell me the error now? Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832850 Share on other sites More sharing options...
CloudSex13 Posted May 12, 2009 Author Share Posted May 12, 2009 Same error keeps appearing, man: Unknown column 'Array' in 'where clause' D: Quote Link to comment https://forums.phpfreaks.com/topic/157787-solved-count-and-match-values-in-an-array-to-a-database-table/#findComment-832856 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.