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

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];;

Related

Only variables should be passed by reference Strict standards on line 138 [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 5 years ago.
usort($qus_with_ans, "qus_sort");
$ary_val = max(array_column($qus_with_ans, 'updated_at'));
$ary_key = array_search($ary_val, array_column($qus_with_ans, 'updated_at'));
$k = $qus_with_ans[$ary_key];
$curnt_sub_id = $k['subject_id'];
$curnt_sub_name = $k['s_name'];
$last_question_key = end(array_keys($qus_with_ans));
We have a error Strict standards: Only variables should be passed by reference on last line of code i can't understand why error comes Please fix my issue
line no. 138 are $last_question_key = end(array_keys($qus_with_ans));
You can't use function return in end function and should convert it to the variable.
$keys=array_keys($qus_with_ans);
$last_question_key = end($keys);
However, use arrya_pop which is pushing-out last element from the array
$last_question_key = array_pop(array_keys($qus_with_ans));
If you anyway don't have intension to use keys elsewhere from performance point of view.

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));

Only variables should be passed by reference in php [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 7 years ago.
I have the following code :
$_SESSION['aParties'] = time();
$aParties[] = $_SESSION['aParties'];
error_log(print_r($aParties,true), 3, "/var/tmp/error.log");
$first = reset(ksort($aParties));
The array aParties is like this :
Array
(
[0] => 1433841062
)
But I get the error :
'Only variables should be passed by reference in the method ksort'
Help me please! Thx in advance
You need to do it as
ksort($aParties);
$first = reset($aParties);
Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.
Check Docs

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

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.

PHP object property from class variable? [duplicate]

This question already has answers here:
PHP Object Variable variables name?
(5 answers)
Closed 9 years ago.
Why does this work
foreach ($items as $i) {
$dataTitle = $this->dataTitle;
$title = $i->$dataTitle;
}
when this doesn't?
foreach ($items as $i) {
$title = $i->$this->dataTitle;
}
Is there a better way to do this?
Try this:
$title = $i->{$this->dataTitle};
Your expression is being parsed as:
$title = ($i->$this)->dataTitle;
$this referes to current object parsed in not obvious order. You need to use {expr} notation, to dynamicly evaluate property name.
Try to use {} around $this->dataTitle:
$title = $i->{$this->dataTitle};
Look at bottom part of last example in variable variables section of manual.

Categories