aebstract Posted February 2, 2009 Share Posted February 2, 2009 When I explode by ; I am having a slight issue. If I have 5;1 1/2 and I explode by ;, I end up with 5; and 1 1/2. I want the ; to go away completely. Not sure why it isn't? function showCart2() { $cart = $_SESSION['cart']; echo "$cart"; $var = explode(',',$cart); $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } Quote Link to comment Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 Can you echo $var[$i] in the beginning of your for-loop? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 It displays the number.. 5;1 1/2 why? Quote Link to comment Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 Just checking. Sometimes you think it's displaying that, but it really isn't. Your code should be fine if $var[$i] is that number. I ran it here and it came out ok. Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 Like I said, id is being set to 5; and the size is setting as 1 1/2 I need the ; gone. Hrm.. With the posted code, I'm getting this echoed: 11;5 5/8ID is 11; and Size is 5 5/8 Quote Link to comment Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 Can you give me the result of echo "$cart"; ? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 I just posted it: cart is this one = 11;5 5/8 and the other echo is = ID is 11; and Size is 5 5/8 Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 That really shouldn't be happening, but a quick fix for the issue.... <?php if(substr($id, -1) == ';') $id = substr($id,0,-1); Quote Link to comment Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 I just posted it: You edited your post, you mean. cart is this one = 11;5 5/8 and the other echo is = ID is 11; and Size is 5 5/8 Here's the code I tried: $cart = "11;5 5/8"; $var = explode(',',$cart); $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } The result looks fine: ID is 11 and Size is 5 5/8 If you really can't get the explode to work properly, you can str_replace(';', '', $id); Quote Link to comment Share on other sites More sharing options...
.josh Posted February 2, 2009 Share Posted February 2, 2009 The bug is happening somewhere before that explode. $string = "5;1 1/2"; $string = explode(";",$string); print_r($string); as expected, produces: Array ( [0] => 5 [1] => 1 1/2 ) This explode is coming from $var being exploded at a ',' which ultimately comes from $cart. You have echo "$cart"; up there at the beginning; what does that echo? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 when $cart is echoed it displays: 11;5 5/8 then the rest of the script runs and the second set of echos sends: ID is 11; and Size is 5 5/8 which should be: ID is 11 and Size is 5 5/8 Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 Use this code; function showCart2() { $cart = $_SESSION['cart']; echo "$cart"; $var = explode(',',$cart); echo '<pre>'; var_dump($var); echo '</pre>'; $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } Then COPY & PASTE what you see on screen Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 First off, I've been copy and pasting.. no need to bold it and make me feel as if you're talking down of me.. 11;5 5/8 array(1) { [0]=> string(12) "11;5 5/8" } ID is 11; and Size is 5 5/8 Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 Well I'll be honest that's really strange... I can't think of anything other than using Snart's suggestion.... If you really can't get the explode to work properly, you can str_replace(';', '', $id); Quote Link to comment Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 The following code was copy/pasted, I only changed the source of $cart and it works fine: function showCart2() { $cart = "11;5 5/8"; echo "$cart"; $var = explode(',',$cart); echo '<pre>'; var_dump($var); echo '</pre>'; $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } showCart2(); However, I do notice that the dump shows a length of 12 for that string while it should be 8. Sot sure why, though. EDIT: I mean, it shows 12 for you, but it correctly shows 8 on my screen. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 2, 2009 Share Posted February 2, 2009 I just copy and pasted your code and changed the session var to "11;5 5/8" since that's what you say $cart displays. Your code displays: 11;5 5/8ID is 11 and Size is 5 5/8 bolded part is from the $cart echo, so disregard that. It's echoing out $id and $size just fine. Snart said the same thing. At this point in time, I suggest you make sure you're running the right script. Perhaps you didn't save your most recent version, or accidentally uploaded it into some other directory or editing some old version etc... so make sure you are running the right script in the right location. Quote Link to comment Share on other sites More sharing options...
.josh Posted February 2, 2009 Share Posted February 2, 2009 However, I do notice that the dump shows a length of 12 for that string while it should be 8. Sot sure why, though. EDIT: I mean, it shows 12 for you, but it correctly shows 8 on my screen. right click > view source and look at the var_dump etc.. maybe there is some kind of or some other of that type that snuck in there? Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 that is strange, just to give it a try, pass the variable to the function rather than using the $_SESSION inside the function; function showCart2($cart) { #$cart = "11;5 5/8"; echo "$cart"; $var = explode(',',$cart); echo '<pre>'; var_dump($var); echo '</pre>'; $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } showCart2("11;5 5/8"); Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 that is strange, just to give it a try, pass the variable to the function rather than using the $_SESSION inside the function; function showCart2($cart) { #$cart = "11;5 5/8"; echo "$cart"; $var = explode(',',$cart); echo '<pre>'; var_dump($var); echo '</pre>'; $cnt = count($var); for ($i=0; $i<$cnt; $i++) { list($id, $size) = explode(";", $var[$i]); if (isset($size)) { echo "ID is $id and Size is $size <br />"; } else { echo "ID is $id and there is no size! <br />"; } } } showCart2("11;5 5/8"); This worked fine. Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 ok, instead of this; showCart2("11;5 5/8"); use your session showCart2($_SESSION['cart']); Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 I logged out on my website and logged back in and added the same item with a different second value of 4.2, just so you know: 11;4.2 array(1) { [0]=> string(10) "11;4.2" } ID is 11; and Size is 4.2 Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 so it's only an issue when it comes from a session, ok this may seem like a lot of effort but just to make sure there's no code in there or strange characters where do you set $_SESSION['cart'] and can you echo it before it gets set? (i.e. echo the var assign to $_SESSION['cart']). Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 2, 2009 Author Share Posted February 2, 2009 Right now it is getting set with: <?php $product_cat = $_GET['product_cat']; $checknum = 1; $result = mysql_query("SELECT * FROM `p_products` WHERE category='$product_cat'") or DIE(mysql_error()); while($r = mysql_fetch_array($result)) { $id = $r['id']; $partnumber = $r['partnumber']; $partname = $r['partname']; $description = $r['description']; $price = $r['price']; $option1 = $r['option1']; $specialid = $id; $postname = image.$id; $postsize = size.$id; if (isset($_POST[$postname])) { $error = 0; if (isset($_POST['hideme'])) { if (empty($_POST[$postsize])){ $error = 1; $errormsg = "You must fill in a Pipe Size for $partname."; } else { $sizenum = $_POST[$postsize]; $specialid = $id.";".$sizenum; } } if ($error == 1){ $errordisplay .= "<div id=\"error_box\"> $errormsg</div>"; } else { $cart = $_SESSION['cart']; if ($cart) { $cart .= ','.$specialid; } else { $cart = $specialid; } $_SESSION['cart'] = $cart; header("Location: index.php?page=cart"); exit; } } if (!empty($option1)){ $clicker = " Pipe Size: <input type=\"text\" size=\"4\" value=\"\" name=\"size$id\" style=\"background-color: #FFFFFF; border: solid 1px #000000; height: 20px; font-size: 10px;\" /><br /> <div class=\"product_image\"> <input type=\"hidden\" name=\"hideme\" value=\"needsize\" /> <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\"> </div>"; } else { $clicker = "<br /><div class=\"product_image\"> <input type=\"image\" value=\"submit$id\" src=\"addtocart.jpg\" width=\"144\" height=\"34\" border=\"0\" alt=\"Add to Cart\" name=\"image$id\"> </div>"; } if( $odd = $checknum%2 ) { $content .= " <form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\"> <div class=\"product_left\"> <div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div> <div class=\"product_info\"> $partname <br /> $partnumber <br /> $$price <br /> $clicker </div> </div></form>"; } else { $content .= "<div class=\"product_right\"> <form action=\"index.php?page=product_page&product_cat=$product_cat\" method=\"post\" name=\"add_product$id\"> <div class=\"product_image\"><img src=\"products/$partnumber-cart.jpg\" class=\"jkimagelarge\" title=\"products/$partnumber.jpg\" height=\"100\"/></div> <div class=\"product_info\"> $partname <br /> $partnumber <br /> $$price <br /> $clicker </div> </div></form>"; } $checknum ++; } ?> But.. it is set correctly, let me add a few items to the cart without the value and then throw one in the middle with it and show you the echo results: 1,9,8,11;4.5,7,8 array(6) { [0]=> string(1) "1" [1]=> string(1) "9" [2]=> string(1) "8" [3]=> string(10) "11;4.5" [4]=> string(1) "7" [5]=> string(1) "8" } ID is 1 and there is no size! ID is 9 and there is no size! ID is 8 and there is no size! ID is 11; and Size is 4.5 ID is 7 and there is no size! ID is 8 and there is no size! You can see it is set correctly, and it's splitting them all correctly but leaving that ;. What section of my code do you think you will need to look at? Not sure if what I posted is all, but it's where it gets set at. Quote Link to comment Share on other sites More sharing options...
premiso Posted February 2, 2009 Share Posted February 2, 2009 That is just messed up. Not sure where it is going wrong... You could try this: <?php list($id, $size) = my_explode(";", "11;4.2"); function my_explode($where, $what) { $exploded = explode($where, $what); if (is_array($exploded)) { foreach ($exploded as $key => $item) { if (strstr($item, $where) !== false) { $exploded[$key] = str_replace($where, "", $item); } } } return $exploded; } ?> And run that and see if it works properly. If so, it seems like maybe your server is doing some strange stuff... EDIT: Fixed the code. Quote Link to comment Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 I think the problem originates from this line; $specialid = $id.";".$sizenum; Try changing it to this for now... $specialid = $id.";".$sizenum; 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.