dachshund Posted December 11, 2011 Share Posted December 11, 2011 Thanks to the help of user Winstons on another post I've managed to almost finished this bit of code. I thought I would move it over because the other subject doesn't explain the new problem that's occured. Basically it's a shopping cart with contains both the id of selected items and the selected size in a session. Everything is stored like this 12s3,6s1 etc. Where 12 and 6 are the item ids and then 3 and 1 are the selected size of that item. In the code below when I do: <?php echo 'ID ' . $val[0] . '; Size: ' . $val[1] . '<br/>'; ?> Each of the items and their accompanying size is echoed out perfectly. However if there are two of 12s3 is would need to recognise that and change the quantity ($qty) to 2, rather than display the same item twice. Here is my code. Any help would be great. <div id="view_basket"> <ul> <?php $basket = $_SESSION['basket']; if ($basket) { $data = preg_split("#[s,]#", $basket); $data = array_chunk($data, 2); foreach($data as $key => $val) $contents[] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; echo '<form action="basket.php?action=update" method="post">'; echo '<table>'; foreach ($data as $id=>$qty) { $sql = "SELECT * FROM store WHERE id LIKE '$id' AND live LIKE '0'"; $result = mysql_query($sql); while ($rows = mysql_fetch_array($result)) { extract($rows); ?> <li> <div id="view_basket_image"> <img src="<?php echo $rows['indeximage']; ?>" /> </div> <div id="view_basket_title"> <span class="view_basket_brand"><?php echo $rows['brand']; ?> ·</span> <span class="view_basket_description"><?php echo $rows['title']; ?></span> </div> <div id="view_basket_qty"> <input type="text" name="qty<?php echo $id; ?>" value="<?php echo $qty; ?>" size="3" maxlength="3" class="view_basket_qty" /> </div> <div id="view_basket_price"> <span class="view_basket_x">x</span>£<?php echo $rows['price']; ?> <?php /* WORK OUT ITEM WEIGHTS */ $itemweight = $rows['weight'] * $qty; $totalweight += $rows['weight'] * $qty; ?> </div> <div id="view_basket_itemtotal"> <?php $itemtotalprice = $rows['price'] * $qty; $itemtotal = number_format($itemtotalprice, 2, '.', ','); echo '£'; echo $itemtotal; $total += $rows['price'] * $qty; ?> </div> <div class="clear"></div> </li> <?php } } ?> </ul> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252952-finding-duplicate-items-and-creating-quantity/ Share on other sites More sharing options...
Drongo_III Posted December 11, 2011 Share Posted December 11, 2011 Not entirely sure what your array looks like but using $dupes = array_count_values($array); This counts all the occurances of each value and returns an array. The returned array has the original value as the key and the number of occurances as the value. So with a bit of logic you should be able to use those values in your display Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/252952-finding-duplicate-items-and-creating-quantity/#findComment-1296938 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 not sure i entirely understand? sorry! Quote Link to comment https://forums.phpfreaks.com/topic/252952-finding-duplicate-items-and-creating-quantity/#findComment-1296940 Share on other sites More sharing options...
Drongo_III Posted December 12, 2011 Share Posted December 12, 2011 Well that function will count the occurances of each array item. So if you have an array of the basket items you can do a count on the array items. Then if they occur more than once you can set the count as the quantity. Sorry i didn't read through your code that closely but it seemed like a possible solution... not sure i entirely understand? sorry! Quote Link to comment https://forums.phpfreaks.com/topic/252952-finding-duplicate-items-and-creating-quantity/#findComment-1296967 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.