marcus Posted December 1, 2007 Share Posted December 1, 2007 I am creating a "market," and the problem I have now is that, if the user has more than one of the same item in their inventory, all of them will be added, instead of the ones selected. <?php if($act == 'add'){ if(!$_POST['submit']){ $sql = "SELECT * FROM `user_items` WHERE `uid`='".$_COOKIE['uid']."' AND `place`='inventory'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "<br>You do not have any items in your inventory!"; }else { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" class=\"forum\">\n"; echo "<form method=\"post\" action=\"/users/shops.php?act=add\">\n"; echo "<tr><td colspan=\"5\" align=\"center\" class=\"forum_title\">Add Items to Your Shop</td></tr>\n"; echo "<tr>\n"; $x=1; while($row = mysql_fetch_assoc($res)){ $item_name = itemInfo($row['item_id'],'name'); $trade = itemInfo($row['item_id'],'trade'); $image = "/images/items/" . itemInfo($row['item_id'],'folder') . "/" . itemInfo($row['item_id'],'image'); echo "<td align=\"center\" valign=\"top\"><img src=\"".$image."\" alt=\"".$item_name."\" title=\"".$item_name."\"><br><b>".$item_name."</b>"; if($trade > 0){ echo "<br><input type=\"checkbox\" name=\"items[]\" value=\"".$row['id']."\">\n"; } echo "</td>\n"; if($x == '5'){ echo "</tr><tr>\n"; $x=0; } $x++; } echo "</tr>\n"; echo "<tr><td colspan=\"5\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Add the Selected Items\" class=\"forum_submit\"></td></tr>\n"; echo "</form></table>\n"; } }else { $items = $_POST['items']; if(count($items) > 0){ foreach($items AS $item){ $item = protect($item); $sql = "SELECT * FROM `user_items` WHERE `place`='inventory' AND `id`='".$item."'"; $res = mysql_query($sql) or die(mysql_error()); $error = (mysql_num_rows($res) == 0) ? "1" : "0"; } if($error == '1'){ echo "There was an error processing your request!\n"; }else { foreach($items AS $items1){ $item1 = protect($item1); $sql2 = "SELECT * FROM `user_items` WHERE `place`='inventory' AND `id`='".$items1."'"; $res2 = mysql_query($sql2) or die(mysql_error()); $row = mysql_fetch_assoc($res2); $sql3 = "SELECT * FROM `user_items` WHERE `item_id`='".$row['item_id']."' AND `uid`='".$_COOKIE['uid']."' AND `place`='inventory'"; $res3 = mysql_query($sql3) or die(mysql_error()); $row2 = mysql_fetch_assoc($res3); $itema = array(); echo mysql_num_rows($res3); if(mysql_num_rows($res3) > 1){ $itema[$items1]['quantity'] = mysql_num_rows($res3); $itema[$items1]['item_id'] = $row['item_id']; }else { $itema[$items1]['quantity'] = '1'; $itema[$items1]['item_id'] = $row['item_id']; } } echo "<pre>\n"; print_r($itema); echo "</pre>\n"; /* foreach($itema AS $item2){ $sql4 = "INSERT INTO `user_shop_items` (`uid`,`item_id`,`quantity`,`price`) VALUES('".$_COOKIE['uid']."','".$itema[$item2]['item_id']."','".$itema[$item2]['quantity']."','0');"; $res4 = mysql_query($sql4) or die(mysql_error()); $sql5 = "DELETE FROM `user_items` WHERE `id`='".$item2."' AND `place`='inventory' AND `uid`='".$_COOKIE['uid']."'"; $res5 = mysql_query($sql5) or die(mysql_error()); } */ echo count($items) . " have been added to your shop! <a href=\"/users/shop.php?act=stock\">Click here</a> to edit your stock, or <a href=\"/users/shops.php?act=add\">click here</a> to add more items.\n"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted December 2, 2007 Author Share Posted December 2, 2007 Forget that code. I've replaced it with a new code. $items = $_POST['items']; if(count($items) > 0){ foreach($items AS $item){ $item = protect($item); $sql = "SELECT * FROM `user_items` WHERE `place`='inventory' AND `id`='".$item."'"; $res = mysql_query($sql) or die(mysql_error()); $error = (mysql_num_rows($res) == 0) ? "1" : "0"; } if($error == '1'){ echo "There was an error processing your request!\n"; }else { foreach($items AS $item1){ $sql2 = "SELECT * FROM `user_items` WHERE `id`='".$item1."' AND `place`='inventory'"; $res2 = mysql_query($sql2) or die(mysql_error()); $row = mysql_fetch_assoc($res2); $item_id = $row['item_id']; $sql3 = "INSERT INTO `user_shop_items` (`uid`,`item_id`,`quantity`,`price`) VALUES('".$_COOKIE['uid']."','".$item_id."','1','0')"; $res3 = mysql_query($sql3) or die(mysql_error()); $sql4 = "DELETE FROM `user_items` WHERE `id`='".$item1."' AND `place`='inventory' AND `uid`='".$_COOKIE['uid']."'"; $res4 = mysql_query($sql4) or die(mysql_error()); } $sql5 = "SELECT * FROM `user_shop_items` WHERE `uid`='".$_COOKIE['uid']."'"; $res5 = mysql_query($sql5) or die(mysql_error()); while($row2 = mysql_fetch_assoc($res5)){ } echo count($items) . " items have been added to your shop! <a href=\"/users/shop.php?act=stock\">Click here</a> to edit your stock, or <a href=\"/users/shops.php?act=add\">click here</a> to add more items.\n"; } } Inside that while statement, how could I go about checking for multiple items in the table, and if there are, group them together, and delete one of them. id uid item_id quantity price 24 1 88 1 0 23 1 87 1 0 25 1 88 1 0 How to group 24 and 25? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 2, 2007 Share Posted December 2, 2007 I don't understand what your trying to do...Are you saying instead of showing the item more than once, if they have more than one of that item, you want to display just one of them, and how much they have of it? ITEM 1 ITEM2 ITEM 3 1 in stock 4 in stock 5 in stock Kinda like that? Or am I completely off? Quote Link to comment Share on other sites More sharing options...
marcus Posted December 2, 2007 Author Share Posted December 2, 2007 When adding, each item has it's own row. So right after adding them, select the duplicates, update the duplicates and add the amount to ONE row that is the same item, then delete all but one row of the duplicates. So say: itemid 23 has a 4 quantity of item 6 itemid 24 has a 1 quantity of item 6 So select duplicates, the last duplicate would be itemid 24, so take that quantity, add it to itemid 23, you'll have 5 quantity for itemid 23, then delete itemid 24 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.