get dynamic $_POST data into a for loop - php

for ($i = 0; $i <= ${"ConteggioColonneTabella".$NomeTabella}; $i++) {
${'record'.$i} = addslashes($_POST['record'].$i);
}
Look at
addslashes($_POST['record'].$i);
This isn't correct, i know, but I can't figure out what is the correct syntax for getting POST variables.
================= EDIT ==================
This works outside the loop
$i = 1;
${'record'.$i} = addslashes($_POST[record.$i])
getting only one result
this doens't work inside the loop, getting no results
for ($i = 1; $i <= 6; $i++) {
${'record'.$i} = addslashes($_POST['record'].$i);
}

Related

String sort in alphabetic order in php8

I am trying to find first element(alphabetical order) from array string using loop without inbuilt functions in php8. My code is below but it is not working a intented. What is the error in my code?
eg: string = "This","is","my","apple";
apple need to print
$arraystring= array("This","is","my","apple");
$count = count($arraystring);
for ($i = 0; $i < $count; $i++) {
for ($j = 1; $j < $count; $j++) {
if(strcmp($arraystring[$i], $arraystring[$j])<0){
$temp = $arraystring[$i];
$arraystring[$i] = $arraystring[$j];
$arraystring[$j] = $temp;
}
}
}
my code not worked as intented

mysql query id as variable not working

I want to update my MySQL table. When I type the ID as a number works, but when using a variable instead, it does not work.
What I am trying to do is order elements of an html table by column.
I have e.g. 4 Columns:
$colname = array("Column1", "Column2", "Column3", "Column4");
I get the IDs of the elements already sorted from the URL variable:
$strTaskIds = $_GET["taskIds"];
// for example: $strTaskIds = "3;1;32_4;5_6;36_34;7"
Now I split the string into a 2D-Array and update the MySQL table:
$arrTaskIds = explode("_", $strTaskIds);
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
}
if($conW->query($sql) === TRUE) {
$error = 0;
} else {
$error = 1;
}
}
When I write a number E.G 7 instead of the variable $arrIdsPerCol[$j] it works.
Writing (int)$arrIdsPerCol[$j] does not work either.
The reason i gave you no error message is that there was none. It just looked like the MySQL table is not updating.
After starring at my code for quite a long time a found the problem.
I placed the query() code in the outer loop. But i needed it in the inner loop.
Problem solved:
$arrTaskIds = explode("_", $strTaskIds);
$error = 0;
for($i = 0; $i < count($arrTaskIds); $i++) {
$arrIdsPerCol = explode(";", $arrTaskIds[$i]);
for($j = 0; $j < count($arrIdsPerCol); $j++) {
$sql = "UPDATE tasks SET col='$colname[$i]', rank=$j WHERE id=$arrIdsPerCol[$j]";
if($conW->query($sql) === TRUE) {
} else {
$error = 1;
}
}
}

How to change the value with a variable within a for loop?

I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";

Create arrays with cycles

I have the following code:
for ($i = 1; $i <= $j; $i++)
{
$goods_{$i} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
I hoped that it could make this in first step of the cycle:
$i =1;
$goods_1 = array(
$_POST['goods1_title'],
$_POST['goods1_package'],
$_POST['goods1_nmr']
);
and so on in other steps.
I follow AbraCadaver's sentiments:
Why in the world are you doing this? You are using arrays, keep using them.
As such, I would write the code simply using an Array:
$goods = array();
for ($i = 1; $i <= $j; $i++)
{
// Assign to an index in the already created array,
// but DO NOT create a new variable.
$goods[$i] = array(
// Also make sure these are correct ..
$_POST["goods{$i}_title"],
);
}
If you really want to create dynamic variables - ick! - see variable variables.
Should be
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]
It can be done like this:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["'goods'.$i'_title'"],
$_POST["'goods'.$i.'_package'"],
$_POST["'goods'.$i.'_nmr'"]
);
}
You can read more about this topic in related PHP documentation.
The result of "'goods'.$i'_title'" will be 'goods'.1'_title', in case that you want it to be goods1_title then use following code instead:
for ($i = 1; $i <= $j; $i++)
{
${"goods_$i"} = array(
$_POST["goods{$i}_title"],
$_POST["goods{$i}_package"],
$_POST["goods{$i}_nmr"]
);
}
Another bug might be that in 1 case you use .$i. and in other 2 cases you use .$i without the last ..

For loop extra condition

I have a for loop that goes through a couple of arrays and assigns some values after doing some basic math. My question is: Is there a way to make sure that this for loop ONLY iterates while an additional condition is true?
for ($i = 0; $i < count($ci); $i++) {
$PIDvalue = $ci[$i]["PID"];
$datevalue = $pi[$i]["datetime"];
$pp_tcvalue = $ci[$i]["pp_tc"] - $pi[$i]["pp_tc"];
$pp_trvalue = $ci[$i]["pp_tr"] - $pi[$i]["pp_tr"];
$bp_tcvalue = $ci[$i]["bp_tc"] - $pi[$i]["bp_tc"];
$emailvalue = $ci[$i]["email"];
So I want something like this...
for ($i = 0; $i < count($ci); $i++) {
if($ci[$i]["email"] === $pi[$i]["email"]) {
$PIDvalue = $ci[$i]["PID"];
$datevalue = $pi[$i]["datetime"];
$pp_tcvalue = $ci[$i]["pp_tc"] - $pi[$i]["pp_tc"];
$pp_trvalue = $ci[$i]["pp_tr"] - $pi[$i]["pp_tr"];
$bp_tcvalue = $ci[$i]["bp_tc"] - $pi[$i]["bp_tc"];
$emailvalue = $ci[$i]["email"];
If they don't match, I would assign a value of "0" or something.
You could add an additional condition to the for-loop, making it pretty much unreadable:
for ($i = 0; $i < count($ci), $ci[$i]["email"] === $pi[$i]["email"]; $i++)
but I would rather break the loop:
for ($i = 0; $i < count($ci); $i++) {
if ($ci[$i]["email"] !== $pi[$i]["email"]) break;
$PIDvalue = ...
You can add that in your for condition, like that :
for ($i = 0; $i < count($ci) && $ci[$i]["email"] === $pi[$i]["email"]; $i++) {
When the second condition computes to false, the loop will stop running.

Categories