carbonxps Posted April 29, 2012 Share Posted April 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2012 Share Posted April 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341521 Share on other sites More sharing options...
carbonxps Posted April 29, 2012 Author Share Posted April 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341529 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2012 Share Posted April 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341533 Share on other sites More sharing options...
carbonxps Posted April 29, 2012 Author Share Posted April 29, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341541 Share on other sites More sharing options...
carbonxps Posted April 29, 2012 Author Share Posted April 29, 2012 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) "" } } Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341567 Share on other sites More sharing options...
Zane Posted April 29, 2012 Share Posted April 29, 2012 You have urldecode it before you unserialize it. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341583 Share on other sites More sharing options...
carbonxps Posted April 30, 2012 Author Share Posted April 30, 2012 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.. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341633 Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2012 Share Posted April 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341637 Share on other sites More sharing options...
carbonxps Posted April 30, 2012 Author Share Posted April 30, 2012 Thanks so much, that's perfect..... Unfortunately I can't use sessions as this is going to link to other applications on different servers. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/261799-passing-array-via-url-get/#findComment-1341645 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.