Jump to content

Recommended Posts

Hi there,

 

In a string for a shopping cart I am storing both the item id and the item size selected in a number like this 12s1 where 12 is the id and 1 is the size.

 

How can I separate these from one another again? I'm guessing it's to do with expand() ?

 

Thanks,

 

Jack

Link to comment
https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/
Share on other sites

asurfaceinbetween - you may also change a way to store your values and then use JSON funcions.

 

// initial data: store in array
$a=array( 12=> 'abc', 'cfd' );

// encode info
$b=json_encode( $a );

// echo or save into DB as a single string
echo $b.'<br>';

// restore from DB and then decode
$c=json_decode( $b, true );

// you have you initial data!
print_r($c);

 

This way is more convenient because you may store a complex object. For example, you may store 2 or 3 dimentional arrays. If you use explode you would make a lot of code.

thanks guys, will this work to seperate the commas as well.

 

if there's more than 1 item it is currently just the ids, like - 1,12,16,28

 

now with the size it will be - 1s1,12s2,16s3,28s1

 

the current code I have is:

 

$basket = $_SESSION['basket'];
			if ($basket) {
			$items = explode(',',$basket);
			$contents = array();
			foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
			}

sorry, i don't really understand these.

 

would you be able to edit my current version so i can understand what's what a little better?

 


$basket = $_SESSION['basket'];
			if ($basket) {
			$items = explode(',',$basket);
			$contents = array();
			foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
			}

Do you mean it ?

$basket = $_SESSION['basket'];
if ($basket) {
    $data = preg_split("#[s,]#", $basket);
    $data = array_chunk($data, 2);

    foreach($data as $key => $val)
        echo 'ID ' . $val[0] . '; Size: ' . $val[1] . '<br/>';
}

sorry, but then how do i condense the duplicates?

 

so for example if someone selects 1s2,1s2

 

in my current code it would show up as one item (id 1) with quantity at 2.

 

the full code is:

 


<?php
			$basket = $_SESSION['basket'];
			if ($basket) {
			$items = explode(',',$basket);
			$contents = array();
			foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
			}
			echo '<form action="basket.php?action=update" method="post">';
			echo '<table>';
			foreach ($contents 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($row);
			?>

thanks! almost there. 1 last thing. $qty is now being echoed out as "array".

 

any ideas why? and thank you for your time. this has been messing with my head.

 

<?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($row);
			?>

still no luck. it still says "array".

 

here's what i have

 


<?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 print_r($qty); ?>" size="3" maxlength="3" class="view_basket_qty" />
					</div>

Hmm...

Try replace it

<input type="text" name="qty<?php echo $id; ?>" value="<?php echo print_r($qty); ?>" size="3" maxlength="3" class="view_basket_qty" />

To this

<input type="text" name="qty<?php echo $id; ?>" value="<?php echo '<pre>' . print_r($qty, true) . '</pre>'; ?>" size="3" maxlength="3" class="view_basket_qty" />

What you see on the screen ?

still blank. this is my php

 

<?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($row);
			?>

here's the relevant stuff. the rest is unrelated.

 



function showBasket() {
?>
<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($row);
			?>

				<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>

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.