Jump to content

[SOLVED] slight explode issue


aebstract

Recommended Posts

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 />";
}

}
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.