1

I have passed a value from one page to another in array. I can extract first two vars by explode but I can't get the third value which is formed in an array out.

This is my array:

$user_rate=$_POST['user_rate'];//15000|ss|Array
list($total,$promo,$rate)=explode("|",$user_rate);

So I get:

$total=15000;
$promo=ss;
$rate=Array;

While Array comes from the previous page by looping the daily rate and put in an array. And I need to know what each rate is, so I wrote:

foreach($rate as $val){
echo "$val<br>";
}

But it shows nothing. How can I get this?

Updated : This is the code before the var is sent.

echo "
<tr>
<td align=\"right\" colspan=\"3\">Total</td>
<td align=\"right\"><label for=\"promo_3\"><input type=\"radio\" name=\"user_rate\" id=\"promo_3\" value=\"$ss_total|ss|$cost_ss\" />&nbsp;<u><b>".number_format($ss_total)."</b></u></label></td>
<td align=\"right\"><label for=\"promo_1\"><input type=\"radio\" name=\"user_rate\" id=\"promo_1\" value=\"$net_total|nc|$cost_nr\" checked/>&nbsp;<u><b>".number_format($net_total)."</b></u></label></td>
</tr>";

AND THIS FORMAT OF VALUE CANNOT CHANGE BECAUSE I NEED IT FOR A LATER JAVASCRIPT EXTRACT

While $cost_ss and $cost_nr are derived from database query looping.

while($rec=mysql_fetch_array($result)){
$cost_ss[]=$rec['rate_ss'];
$cost_nr[]=$rec['rate_nr'];
}
4
  • how do you think the string expands to a PHP array? use serialization/json Commented May 29, 2013 at 15:01
  • do var_dump($rate); and see what is actually in that variable. My guess it's a string. Commented May 29, 2013 at 15:03
  • you output $cost_ss like a string although it is an array... if you do echo $array; you will get the string "Array". and thats what happens here. please use serialize($cost_ss) and later do unserialize($data[2]) of your exploded data. Commented May 29, 2013 at 15:34
  • This is how I've tested, but no luck. $promo=explode("|",$_POST['user_rate']); foreach(unserialize($promo[2]) as $key=>$val){ echo "$key=$val<br />"; } Commented May 29, 2013 at 15:38

6 Answers 6

2

this probably be a wrong $user_rate=$_POST['user_rate'];//15000|ss|Array

you can use serialize / unserialize for array

Sign up to request clarification or add additional context in comments.

Comments

1

You could serialize your array parameters before you pass it. Then unserialize them on the 2nd page.

$rate = serialize($rate);

On your 2nd page before you run it through the loop:

$rate = unserialize($rate);

Comments

1

you can encode the values on the page as

implode("|", array($total, $promo, implode(",",$rate));

Then decode it with

list($total, $promo, $rates) = explode("|", $_POST["user_rate"]);
$rates = explode(",", $rates);

1 Comment

I can't change the format of value for the input it needs to be in : <input type="hidden" name="user_rate" value="$ss_total|ss|$cost_ss" /> because I need it for a later javascript extract string.
1

Using serialize is definitely the key to getting this to work, but base64_encode() is the ideal way to make it less obfuscated. When you only use serialize, then you get semicolons, commas, double quotes, single quotes, and several other characters that could cause problems when you pass them through to your script that reassembles it.

For instance, after serializing an array, you will get this, like Sidux pointed out

a:3:{i:0;s:4:"toto";i:1;s:4:"titi";i:2;a:2:{i:0;s:4:"coco";i:1;s:4:"cici";}}

When you add base64_encode to the mix you will get an easier value to work with.

YTozOntpOjA7czo0OiJ0b3RvIjtpOjE7czo0OiJ0aXRpIjtpOjI7YToyOntpOjA7czo0OiJjb2NvIjtpOjE7czo0OiJjaWNpIjt9fQ==

With this, you can easily send/receive your saved array without any trimming, adding of slashes, etcetera.

Assuming you send your data this way, your new code will look like this

$user_rate=$_POST['user_rate'];//15000|ss|Array
list($total,$promo,$rate)=explode("|",$user_rate);
$rate = unserialize( base64_decode($rate) );

Now, $rate is a functional array

9 Comments

Zane, I've tried this but still not work. The think I wonder is why I can't see anything out of "$rate"???
you should receive an error at least when you put $rate into the foreach, because it expects an Array. If you get nothing at all, it means you either have errors set to not display, or $rate actually is a blank array.
Zane, Before the array is sent. I tried "foreach" it and it shows every rate correctly.
This is how I tried : foreach($cost_ss as $key=>$val){echo "$key=$val,";} and it shows : 0=2400,1=2400,2=2400,3=2400,4=2400,5=2400,6=2400,7=2400,8=2400,9=2400,10=2400,11=2400,12=2400,13=2400,
Can you provide the actual code you are using to send the data to the other script. Is it AJAX? Are you submitting a form? In other words, how are your POSTing the data?
|
1

ok, many answers and no luck... i'll try it again:

you have an array $rate before you submit your form:

foreach($rate as $val){
   echo "$val<br>";
}

and you want to add $ss_total and the string "ss" and submit it so you need to:

$newarray = array('rate'=>$rate, 'type'=>'ss', 'ss_total'=>$ss_total); 
// now you have a 2-dimensional array ($newarray)

// the next step is to prepare it for form-submitting (serialize and base64_encode):
$stringvalue = base64_encode(serialize($newarray));    

// ok, now you are able to submit it as radio-field-value:
echo '<input type="radio" name="user_rate" id="promo_3" value="'.$stringvalue.'" />';

when the form is submitted you get a serialized and encoded string in $_POST['user_rate'], you can do an echo if you'd like to see how it looks.

Now you need to base64_decode and unserialize:

$arr = unserialize(base64_decode($_POST['user_rate']));

and you have now full access to your array:

echo $arr['type']."<br />";

echo $arr['ss_total']."<br />";
echo $arr['rate']['index_of_rate']; // i dont know the keys of rate array...

access $arr in javascript:

echo "<script>";
echo "var jsarr = ".json_encode($arr).";\n";
echo "alert(jsarr.ss_total);\n";
echo "</script>";

I hope you get it now.

7 Comments

ok i got it... it works but your user_rate itself seems to be incorrect. The string Array is not what you want. So you need to fix it one step earlier, before your submit of post-data.
Steven, I've tried to extract the array before it send by using "foreach" and it works. Any idea?
yes but we need the code which converts this array to the following string: 15000|ss|Array. I think you should use the serialize stuff instead.
I've updated my code of sending the variables. Please re-check.
Thanks for the tried but the format of "value" cannot be changed because I need it for a later javascript. $(document).ready(function(){ $("input[type=radio][name=promo_code]").click(function(){ var arr = this.value.split('|'); $("#total").val(arr[0]); $("#promo_type").val(arr[1]); }); });
|
0

Hi i think that i understand you probleme, when you passe and array from as string try to "serialize" it and "unserialize" it in the other page, for exemple :

$array = serialize(array('toto', 'titi', array('coco', 'cici')));

will give you somthing like this

a:3:{i:0;s:4:"toto";i:1;s:4:"titi";i:2;a:2:{i:0;s:4:"coco";i:1;s:4:"cici";}}

and if you useunserialize($array); it will give you your array back

5 Comments

Sidux, you mean that I need to rewrite my way of sending array into: <input type="hidden" name="user_rate" value="<?=array(serialize(array('15000','ss',array($rate_array))?>" />
You can use it like this or you can send it by ajax, there is also json_encode and json_decode
Sidux, I can't change the format of value for this input. Because I still need it for a javascript extract on page as well.
You still can create an other hidden input, or use ajax
Yeah, thanks for your help. I've re-designed the way of sending values. Now problem solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.