Jump to content

Passing Array via URL / GET


carbonxps

Recommended Posts

Hi all, new to php and struggling passing an array via url.

 

first page:

 

$str = serialize($cartinfo);
    $strenc = urlencode($str);

<A href="/test/getcart.php?cartinfo=<?PHP echo $strenc; ?>">test</A>

 

Which does add a long string to the URL so I guess to here it's fine.

 

Second Page:

 

<?PHP 

$cartinfo = unserialize($_GET["cartinfo"]); 
    var_dump($cartinfo);

?>

 

However all I get is: bool(false)

 

Any advice would be great, I also tried adding $cartinfo to the session, but this didn't work either. Surely passing variables should be straight forward???

 

Thanks.

Link to comment
Share on other sites

There's nothing wrong with the snippets of code you posted (works for me as expected with some made up data.) Either your actual code is doing something to the value or the data is too long to transfer through a URL (browsers and web servers have limits starting around 2000/4000 characters for the complete URL) or you have some URL rewriting that is not carrying the get parameters or you are redirecting and not carrying the get parameters.

 

How long is the string in $strenc? All you need for a cart is an array of product id's and the quantity for each id.

Link to comment
Share on other sites

Hey thanks for that, I've modified the code to just use product_id:

 

<?PHP
$str = serialize($cart[product_id]);
    $strenc = urlencode($str);
    print $str . "\n";
echo"<BR?>";
    print $strenc . "\n";
?>


<A href="/test/getcart.php?cartinfo=<?PHP echo $strenc; ?>">test</A>

 

<?PHP 
$cartinfo = unserialize($_GET["cartinfo"]); 
    var_dump($cartinfo);
?>

 

The variable is: cartinfo=N%3B

 

But output is NULL

 

Thanks again.

Link to comment
Share on other sites

The N%3B value is a N;, which is (apparently, just tested using serialize(null)) what serialize produces for a null or non-existent value. That implies that the value you are supplying to the serialize function doesn't exist at the time you are calling serialize.

Link to comment
Share on other sites

I think I'm making the array wrong, can somebody tell me how to extract the product_id into a serialize array from the following Array

 

This is the output from var_dump $cart

 

array(3) { ["idx"]=> int(2) [0]=> array(5) { ["quantity"]=> int(4) ["product_id"]=> string(3) "122" ["parent_id"]=> string(3) "122" ["category_id"]=> string(2) "15" ["description"]=> string(0) "" } [1]=> array(5) { ["quantity"]=> int(1) ["product_id"]=> string(2) "77" ["parent_id"]=> string(2) "77" ["category_id"]=> string(2) "19" ["description"]=> string(0) "" } }

 

All I want to do is get the product_id to another page so I can use it in a select. So I want the the variable /array to be 122, 77

 

Obviously there maybe more than two items in the array.

 

Thanks again.

Link to comment
Share on other sites

Sorry the array didn't post correctly:

 

array(3) { ["idx"]=> int(2) [0]=> array(5) { ["quantity"]=> int(4) ["product_id"]=> string(3) "122" ["parent_id"]=> string(3) "122" ["category_id"]=> string(2) "15" ["description"]=> string(0) "" } [1]=> array(5) { ["quantity"]=> int(1) ["product_id"]=> string(2) "77" ["parent_id"]=> string(2) "77" ["category_id"]=> string(2) "19" ["description"]=> string(0) "" } } 

Link to comment
Share on other sites

OK I'm off on a bit of a tangent at the moment....

 

I thought ahead and even if I get the array to the next page I need to extract just the values I need, so I thought it would make more sense to do this first then deal with the array.

 

for($i = 0; $i < $cart["idx"]; $i++) {

               print_r($cart[$i]['product_id']);

			echo "<BR/>";
			$result=implode(',', $cart[$i]) ;
			echo $result;
}

 

The print r command is outputting exactly the values I want, but I can't get this into the results, logic says to me that I need to use:

$result=implode(',', $cart[$i]['product_id']) ;

But this gives an error: Warning: implode() [function.implode]: Invalid arguments

 

I might be going about this all wrong, but basically I have an array called $cart which contains multiple items, all i'm trying to do is extract 1 part of the array the product_id, then I was to pass this value via url to a get variable so I can then filter a recordset based on the contents. All I need to do is have a variable on my results page like id1, id2, id3 etc.

 

I would be very grateful for any advice. Thanks in advance..

Link to comment
Share on other sites

If all you are trying to pass are numbers (in a comma separated list), there's no point in using serialize (the serialized data ends up with a lot of characters that urlencode must convert, which only makes the url longer.)

<?php
$ids = array();
for($i = 0; $i < $cart["idx"]; $i++) {
$ids[] = $cart[$i]['product_id'];
}
?>
<a href="/test/getcart.php?cartinfo=<?php echo urlencode(implode(',',$ids)); ?>">test</a>

 

<?php
if(isset($_GET['cartinfo'])){
    $ids = explode(',',$_GET['cartinfo']);

    var_dump($ids);
}

 

If you eliminate the ['idx'] business from your cart, you can more simply use a foreach loop any time you need to iterate over the cart. You don't need all the extra code to maintain the idx/number of items in the cart.

 

Also, ALL your code will be much simpler if you store the cart in a session variable - $_SESSION['cart'] and solve what ever problem you were having with session variables.

 

Edit: It will also simplify things even more if the cart[xxxx] index is the product_id. You can just get the array_keys to get a list of products in the cart and while iterating over the cart, using a foreach loop, the key is the product_id.

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.