dachshund Posted December 11, 2011 Share Posted December 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/ Share on other sites More sharing options...
-Karl- Posted December 11, 2011 Share Posted December 11, 2011 <?php $string = "12s1"; $sep = explode('s', $string); $id = $sep[0]; $size = $sep[1]; echo $id . ' ' . $size; ?> That's a very simple example of how explode works, you'd have to tweak it to your liking though. Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296794 Share on other sites More sharing options...
SergeiSS Posted December 11, 2011 Share Posted December 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296801 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296813 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Try it $str = '1s1,12s2,16s3,28s1'; $data = preg_split("#[s,]#", $str); $data = array_chunk($data, 2); echo '<pre>'.(print_r($data, 1)).'</pre>'; After, you'll get multi-array, with elements id and size Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296814 Share on other sites More sharing options...
-Karl- Posted December 11, 2011 Share Posted December 11, 2011 Something like this, perhaps? $string = "1s1,12s2,16s3,28s1"; $sep = explode(',', $string); $i = -1; while($i < count($sep)) { $exp = explode('s', $sep[$i]); echo $exp[0] . ' ' . $exp[1] . '<br />'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296815 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 $str = '1s1,12s2,16s3,28s1'; $data = preg_split("#[s,]#", $str); $data = array_chunk($data, 2); foreach($data as $key => $val) echo 'ID ' . $val[0] . '; Size: ' . $val[1] . '<br/>'; Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296816 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296817 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 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/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296818 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296822 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Try replace it $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; To that $contents[] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296828 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296832 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Write in body of 'foreach' print_r($qtv); And if You wrote while ($rows = mysql_fetch_array($result)) { extract($row); ?> You must replace extract($row); to extract($rows); Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296838 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296839 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Tell me, what shows to You print_r($qtv); // Or print_r($rows); And where or what, outputs to screen the 'array' ? Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296842 Share on other sites More sharing options...
-Karl- Posted December 11, 2011 Share Posted December 11, 2011 Tell me, what shows to You print_r($qtv); // Or print_r($rows); And where or what, outputs to screen the 'array' ? Think you mean print_r($qty); Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296843 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 i have <?php echo print_r($qty) ?> as the value in the input of the view_basket_qty div at the bottom of the code above. that is the part that echos array. it also kills the rest of the loop. thanks Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296847 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 This is wrong <?php echo print_r($qty) ?> You must write just <?php print_r($qty); ?> Or this <?php echo print_r($qty, true); ?> Last two examples is same. Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296848 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 i thought that, so tried it, but it is still the same Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296849 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296853 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 weird. now it's completely blank and not showing any items and I can't get it to show them again… Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296889 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 Try write it outside of the 'input' element <?php echo '<pre>' . print_r($qty, true) . '</pre>'; ?> What will on screen ? Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296892 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296894 Share on other sites More sharing options...
Winstons Posted December 11, 2011 Share Posted December 11, 2011 asurfaceinbetween Show all code from your php file. Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296897 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Author Share Posted December 11, 2011 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 '£'; echo $itemtotal; $total += $rows['price'] * $qty; ?> </div> <div class="clear"></div> </li> <?php } } ?> </ul> </div> Quote Link to comment https://forums.phpfreaks.com/topic/252937-separating-size-from-idsize-number/#findComment-1296906 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.