PHP how to loop through a post array
This is how you would do it:
foreach( $_POST as $stuff ) {
if( is_array( $stuff ) ) {
foreach( $stuff as $thing ) {
echo $thing;
}
} else {
echo $stuff;
}
}
This looks after both variables and arrays passed in $_POST
.
Looping through $_POST array values with PHP
From your code, $_POST['session'][$i]
and $value
would be the same thing, which contains array(0 => 50, 1 => 60)
for the first iteration.
In the first iteration $session
is not defined yet, so you cannot access $session[$i]
.
To get what you want to achieve:
foreach ($_POST['session'] as $i => $value) {
$session = $i; //1
$item = $value[0]; //50, and $value[1] is 60
}
To understand more about what the contents of a variable is, you can print its content using print_r($value)
for example, so you will know what its content and structure look like.
UPDATE
To iterate all the values for each session:
foreach ($_POST['session'] as $session => $value) {
foreach ($value as $item) {
//do whatever you want here with the $session and $item
//over all the iterations, here we will have:
//iteration#1 $session=1, $item=50
//iteration#2 $session=1, $item=60
//iteration#3 $session=2, $item=70
//iteration#4 $session=3, $item=80
}
}
Loop through POST array with PHP
$foreach($array /* or in the case $_POST? */ as $key => $value) {
/// loop...
}
or just
$foreach($array as $value) {
/// loop...
}
Edit: So, to get the output you're expecting, you do have another easy way, if you know the max ahead of time:
$for($i = 0; $i<$max /* if you have it */; $i++) {
echo $_POST["source_name" . $i . "_id"] . " - " .
$_POST["source_code" . $i . "_id"] . " - " .
$_POST["source_level" . $i . "_id"] . "<br>\n";
}
PHP Iterate through $_POST and use values by name
foreach($_POST as $key => $value)
{
if (strstr($key, 'item'))
{
$x = str_replace('item','',$key);
inserttag($value, $x);
}
}
How Do I loop through this POST array?
What you can do is
foreach (Yii::$app->request->post('LoginForm') as $field) {
// some logic
}
Also I see a mistake in compayname. A "n" is missing in the word. But you can access it's value by Yii::$app->request->post('LoginForm')['compayname'];
Loop through POST array for database insert
You're writing to the database outside the loop. So the only value written is the last one that went through the loop. Move your insert into the loop.
if(!empty($_POST['chk1'])) {
foreach($_POST['chk1'] as $check) {
//echo $check;
$sql="INSERT INTO $usertable (library) VALUES ('.$check.')";
mysqli_query($link, $sql);
}
}
What you need to be doing is using prepared statements to sanitize user data. This will also be much more efficient as the query is only sent to the database once:
if (!empty($_POST["chk1"])) {
$stmt = $mysqli->prepare("INSERT INTO $usertable (library) VALUES ?");
$stmt->bind_param("s", $check);
foreach($_POST['chk1'] as $check) {
$stmt->execute();
}
$stmt->close();
}
How to submit all the values of this form as an array and loop through when posting to SQL db?
Your form will post 2 arrays that contain the values of serial
and imei
from your form. After you've verified that they have the same number of entries you just need to loop through one of them to extract the values from both arrays and insert them into your database.
Since your question did not include anything related to your database connection I can only show you the variables that you will use to insert the data into your DB. Use PDO and prepare the insert before entering the loop (here's a good reference to get you going with PDO and prepare).
$serial_no = $_POST['serial_no'];
$imei = $_POST['imei'];
$entrycount = count($serial_no);
for ($loop = 1;$loop <= $entrycount; $loop++) {
//
// The values from your form will be in the following variables:
//
//
// $serial_no[$entrycount]
// $imei[$entrycount]
//
// The variables shown above are what you will have to insert into your DB.
//
}
Related Topics
Object of Class Mysqli_Result Could Not Be Converted to String
Redirect That Same Page from Where Id Is Coming
Change Language of Site With a HTML Button
How to Replace Newline or \R\N With <Br/>
Regex: Number Range Excluding Specific Number
Easiest Way to Replace All Characters in Even Positions in a String.
Creating Advanced Search Query With PHP Mysqli
How to Get the Path to the Laravel Storage Folder
Telegram Bots - How to Create Reply Menus With Icons Like Mypokerbot
In Laravel How to Get Data in One Query from 3 Tables
Laravel - How to Join 2 Tables from Different Db Connection
Getting Check Box Values as Checked from Database Codeigniter
Running a Python Script from PHP
How to Use a Findby Method With Comparative Criteria
Codeigniter Session Is Not Working on PHP 7
How Does PHP 'Foreach' Actually Work
Are Global Variables in PHP Considered Bad Practice? If So, Why