开发者

How to Pass the array values on query string in php

开发者 https://www.devze.com 2023-03-05 02:53 出处:网络
I need your help., How to pass the array values in query string... <?php foreach ( $Ca开发者_如何转开发rt->getItems() as $order_code=>$quantity ) :

I need your help.,

How to pass the array values in query string...

<?php 
                foreach ( $Ca开发者_如何转开发rt->getItems() as $order_code=>$quantity ) :
                $pname[]= $Cart->getItemName($order_code);
                $item[]= $Cart->getItemPrice($order_code);
                $qty[]=$quantity;
                endforeach;
                ?>
                <form action='expresscheckout.php?amt=<?php echo $total_price; ?>&item_name=<?php echo $pname; ?>&unit=<?php echo $item; ?>&quan=<?php echo $qty; ?>' METHOD='POST'>
<input type='image' name='submit' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' border='0' align='top' alt='Check out with PayPal'/>
</form>

This is my code sample.,for the above sample code i have to pass the query string on my action url that is expresscheckout.php?amt=100.,etc...values are coming under the $pname[],$item[],$qty[].

Expecting output in expresscheckout.php

expresscheckout.php?pname=product1,product2&item=item1,item2&qty=1,2,3....like this....

Thanks in Advance....


To pass the array values you should use [ ]. For ex.

<form method="post" action="path to script">
<input type="checkbox" id="colors[]" value="red" /> Red
<input type="checkbox" id="colors[]" value="blue" /> Blue
<input type="checkbox" id="colors[]" value="green" /> Green
<input type="checkbox" id="colors[]" value="yellow" /> Yellow
</form>

And you need to use like this in PHP

$colors=$_POST['colors']; //takes the data from a post operation...
$query=INSERT INTO colors VALUES('$colors');


Basically, you just need to use PHP's implode function to turn an array into a comma-separated list. i.e. for the $item array:

$item = array('item1', 'item2');
echo implode(',', $item);

Will output:

item1,item2

So, something like this should print a querystring similar to what you want:

echo 'pname='.implode(',', $pname).
     '&item='.implode(',', $item).
     '&qty='.implode(',', $qty);


You need to serialize your array

serializing in php

Generates a storable representation of a value

This is useful for storing or passing PHP values around without losing their type and structure.

To make the serialized string into a PHP value again, use unserialize().

ref :-

http://php.net/manual/en/function.serialize.php

http://www.php.net/manual/en/function.unserialize.php

Example

$my_array= (array('val1', 'val2','val3');

server.php?query_string=serialize($my_array)

in server page

$query_string= unserialize($_REQUEST['query_string']);

Now it will be in array format

I hope this Helps you

0

精彩评论

暂无评论...
验证码 换一张
取 消