Store values in an array and pass it in url [duplicate] - php

This question already has answers here:
Passing arrays as url parameter
(11 answers)
Closed 9 years ago.
$deposit=$_POST['amountdeposit'];
$arr= array();
for($i=0;$i<10;$i++)
{
if($arr[$i]=='\0')
{ $arr[$i]= array("$deposit");
}
break;
}
$page= "step2.php?arr=$arr";
header("Location:$page");
?>
what i want to do is each time there's a change in $deposit , this value is stored in $arr[$i] and then it is passed in a url so that i could use GET on that step2.php page.
What I see is just arr=array instead of values :/ please help me.

You want http_query_string. It will do exactly what you want.

A couple of other comments have recommended http_query_string, however I would use serialize along with urlencode.
Replace:
$page= "step2.php?arr=$arr";
with:
$page= "step2.php?arr=" . urlencode(serialize($arr));
Then when you get to step2.php, unserialize(urldecode($_GET['arr'])) will contain your array as you originally built it.

Related

How to use the variable inside some function? [duplicate]

This question already has answers here:
How to pass a variable inside a function?
(1 answer)
How to access array element inside another array [duplicate]
(1 answer)
Closed 5 years ago.
I have an API's Function :
$transaction=$tran[4];
function coinpayments_api_call($cmd, $req = array(),$transaction) {
curl_init($transaction);
}
$transaction variable not passing into function coinpayments_api_call.
function not taking values from out side.
I also make $transaction varible GLOBAL ,but still same problem ,
Please Help
Make your code like this
$transaction=$tran[4];
function coinpayments_api_call($transaction,$cmd, $req = array(),$txnid) {
curl_init($transaction);
}

PHP - auto assign a different name to a variable of variable [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 6 years ago.
Using PHP variable variables with mysqli_fetch_assoc to auto-format variables like $column_name="some_value" (code below):
while ($account_row=mysqli_fetch_assoc($account_results))
{
foreach ($account_row as $key=>$value)
{
$$key=trim(stripslashes($value));
}
}
So if I have a column "username" and row value "someuser", this code creates:
$username="someuser";
However, in some cases I need variable names to be DIFFERENT from column names. For example, I need code to create:
$username_temp="someuser";
How could I do that? Using this code gives an error:
$$key."_temp"=trim(stripslashes($value));
No other ideas in my head.
change $$key."_temp" to ${$key."_temp"} have a look on below solution:
$value = 'test';
$key="someuser";
${$key."_temp"}=$value;
echo $someuser_temp; //output test
Please try this ${$key . "_temp"} = trim(stripslashes($value));

Get json object's value in php if its name contains dots in PHP [duplicate]

This question already has answers here:
php object attribute with dot in name
(5 answers)
Closed 7 years ago.
I have a json like the following
{"root":{
"version":"1",
"lastAlarmID":"123",
"proUser":"1",
"password":"asd123##",
"syncDate":"22-12-2014",
"hello.world":"something"
}
}
After json_decode(), I can get all the values except that of the last one hello.world, since it contain a dot.
$obj->root->hello.world doesn't work. I got solutions for doing it in Javascript but I want a solution in php.
$obj->root->{'hello.world'} will work.
ps: $obj->root->{hello.world} might not work.
b.t.w: Why not use Array? json_decode($json, true) will return Array. Then $a['root']['hello.world'] will always work.
You have two options here:
First option: convert the object to an array, and either access the properties that way, or convert the name to a safe one:
<?php
$array = (array) $obj;
// access the value here
$value = $array['hello.world'];
// assign a safe refernce
$array[hello_world] = &$array['hello.world'];
Second option: use quotes and brakets:
<?php
$value = $obj->root->{'hello.world'};
This is working
echo $test->root->{'hello.world'};
Check example
<?php
$Data='{"root":{
"version":"1",
"lastAlarmID":"123",
"proUser":"1",
"password":"asd123##",
"syncDate":"22-12-2014",
"hello.world":"something"}
}';
$test=json_decode($Data);
print_r($test);
echo $test->root->{'hello.world'};
?>
Output
something
You can use variable variables (http://php.net/manual/en/language.variables.variable.php):
$a = 'hello.world';
$obj->root->$a;

Retrieve POST values in PHP using a dynamic method [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 8 years ago.
I was wondering if it's possible to do something like:
for ($d=0; $d<20; $d++) {
$productname.$d = $_POST['productname'.$d];
$link.$d = $_POST['link'.$d];
$color.$d = $_POST['color'.$d];
$size.$d = $_POST['size'.$d];
$otherinfo.$d = $_POST['otherinfo'.$d];
$no.$d = $_POST['no'.$d];
$other.$d = $_POST['other'.$d];
}
since the code above doesn't work.
What am I doing wrong? Any help is appreciated.
Your $productname.$d statement has no sense.
Use $productname[$d] instead. It is array approach and it is much more preferable.
P.S.: If you really want so many different variables you can use variable variables (pseudocode is below):
$varName = 'productname'.$d;
$$varName = $_POST['productname'.$d];;

Create vars with "$i" value on a for cicle [duplicate]

This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 9 years ago.
<form>
<?php
for($i=1; $i<=3; $i++){
print '<input ... name="'.$until.$i.'">...
print '<input ... name="'.$quantity.$i.'">...
}
?>
</form>
Is it possible with this for:
for($p=1; $p<=3; $p++){
$until.$p = $_POST['until'.$p]; // not required
$quantity.$p = $_POST['quantity'.$p]; // required
}
to create vars that will have these names?
$until1
$quantity1
...
$until3
$quantity3
My solution won't obviously work...
Use an array instead. Then you can have $until[$p] for example.
Use ${'until'.$p} to do exactly want you want.
Array, most definitely. Remember, with PHP you can add elements dynamically. On an aesthetic note, "until" is a reserved word in several languages, as part of program controls (e.g., if-then-else, do-while). Consider using variable names that represent the function and/or type of data.

Categories