Jump to content

Finding duplicate items and creating quantity


dachshund

Recommended Posts

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 '&pound'; echo $itemtotal;
					$total += $rows['price'] * $qty;
					?>
					</div>
					<div class="clear"></div>
				</li>
				<?php
				}
				}
				?>
			</ul>
		</div>

		<?php
		}
		?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

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.