Get the ids and loop through post data - php

I post a form with the same input names+id at the end like that:
<input type="text" name="machine_1">
<input type="text" name="machine_11">
<input type="text" name="machine_23">
How loop through on each of them and get the id's in the loop?
I tried this way, but it will loop a lot without any data and what happens if there is more thank 100 id?
for($i=0; $i<100; $i++){
$_POST["machine"]=$_POST["machine_".$i];
$id=$i;
}

POST is an associate array, so you can loop through everything that's been posted like this:
//$k contains the id
//$v contains the submitted value
foreach($_POST as $k => $v) {
//test if id contains 'machine'
if(stristr($k, 'machine')) {
echo $v;
}
}

You can do this using a foreach loop, like so:
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value';
}

$_POST["machine"]=$_POST["machine_".$i];
here $_POST['machine'] represents noting..how can you assign a value to $_POST['machine']..you havent passed any element having name machine while submitting the form..so first of all you need to check it bro

In your code, you have:
$_POST["machine"]=$_POST["machine_".$i];
That's not the correct way. You want to store the value of $_POST["machine_".$i] to $id and then use it below.
This is probably what you need:
for($i=1; $i<100; $i++){
$id = $_POST["machine_".$i];
echo $id;
}
And if there are more than 100 elements, and you don't know the number of inputs, then you can use a foreach loop, as below:
$i = 1; // counter variable
foreach ($_POST as $key => $input) {
if($key == "machine_".$i) {
$id = $input; // or simply 'echo $input'
echo $id;
}
$i++; // increment counter
}

Related

How to get value attirbute value from php 2d session variable

I am having an php session array like
["cart"]["123"] = "Biscuit"
["cart"]["124"] = "Jam"
If I want to access the 2nd element I will access array_values($_SESSION["cart"])[$i]
where $i runs in for loop. If I want to get the values "123" and "124", how can i achieve it in a for loop with only "cart" and $i..?
foreach($_SESSION['cart'] as $key => $value)
{
echo $key; // your 123 or 124 key
}
This is an associative array easiest and best way is foreach because have to deal with key-value pairs rather than indexes(numbers).
foreach($arr['cart'] as $key => $val){
echo "$key<br/>";
}
I used a variable to hold values($arr)
But you can try for loop also:
$keys = array_keys($arr['cart']);
for ($keyindex = 0; $keyindex < count($keys); $keyindex++) {
$key = $keys[$keyindex];
echo $key."<br>";
}

PHP Get input name

Upon posting a form using _GET I would like to get the input name
See below part of url upon submit
.php?14=0&15=0&16=0&17=0&18=0&19=0
I know how to get the variable E.G:
$14=$_GET["14"];
Which is 0
However is it possible to do this and get the input name (eg 14) and then turn these into variables? (To save the input name to the DB)
To get all the $_GET parameters, you can do:
foreach($_GET as $key => $value){
echo "Key is $key and value is $value<br>";
}
This will output each key (14, 15, 16 etc) and value (0, 0, 0 etc).
To bind a variable name with a variable string, look at variable variables:
foreach($_GET as $key => $value){
$$key = $value;
}
As a result, you will have the following variables with the following values:
$14 = 0;
$15 = 0;
$16 = 0;
// etc...
Alternatively (as you don't necessarily know what the key/value pairs would be), you could create an empty array and add these keys and values to it:
foreach($_GET as $k => $v){
$arr[$k] = $v;
}
The resulting array will be:
$arr[14] = 0;
$arr[15] = 0;
$arr[16] = 0;
// etc...
Solution using single loop (Update):
If you're just using question/answer single time you can do it in single loop like this,
<?php
foreach($_GET as $key => $value){
$question = $key;
$answer = $value;
// Save question and answer accordingly.
}
If you will be using question answer to perform other things, use the following method.
You can get all the keys using array_keys(), where $_GET is an array.
Use it like this,
<?php
$keys=array_keys($_GET);
print_r($keys); // this will print all the keys
foreach($keys as $key) {
// access each key here with $key
}
Update:
You can make a pair of question,answer array and put it in the main array to insert it in the database like this,
<?php
$mainArray=array();
$keys=array_keys($_GET);
foreach($keys as $key) {
// access each key here with $key
$questionAnswerArray=array();
$questionAnswerArray["question"]=$key;
$questionAnswerArray["answer"]=$_GET[$key];
$mainArray[]=$questionAnswerArray;
}
// Now traverse this array to insert the data in database.
foreach($mainArray as $questionanswer) {
echo $questionanswer["question"]; //prints the question
echo $questionanswer["answer"]; // prints the answer.
}

How to create a dynamic array variable

I have these arrays and i want to loop through every array by creating a dynamic variable that put their name in foreach loop automatically something like this
foreach ($activities.$i as &$activity) { //$i = 1,2,3,4..
//code
}
//activities
$activities1 = $_POST["activities1"];
$activities2 = $_POST["activities2"];
$activities3 = $_POST["activities3"];
$activities4 = $_POST["activities4"];
Easier method is to simply use the array naming hack:
<input name="activities[1]" ..>
<input name="activities[2]" ..>
<input name="activities[3]" ..>
which makes $_POST['activities'] you array:
foreach($_POST['activities'] as $i => $value) {
// $i -> 1,2,3,4
}
But if you insist on embedding the index inside the key's name, then:
foreach(range(1,4) as $i) {
foreach($_POST["activities{$i}"] as $value) {
...
}
}

submit form data from a foreach loop

I have a foreach loop with a form inside for each result like so:
foreach($this->results() as $that) {
<form>
<input type="text" name="name[]">
<input type="text" name="this[]">
</form>
}
and so on. My question is how do I each forms data. I understand you can do something like the following:
$_POST['name'][0];
$_POST['name'][1];
etc, but is their a way to get this done without knowng how many forms their will be. I mean like foreach loop the $_POST data and get each form?
Many thanks
foreach ($_POST['name'] as $val) { /* do what you want, want you want with my value */ }
$_POST['name'] is just an array. Use count or any array function you want on it then.
You can do it like this:
foreach ($_POST['name'] as $val => $value) {}
assuming these rows are being generated by a while loop, and the variable is named like this
$val = $row['val'];
And then in your form you'd have something like this:
echo '<input type="hidden" value="'.$val.'" name ="val[]" /><input type="text" name="name['.$val.'] />";
basically the name variable would be identified by the value itself being generated but also appended on a named variable, and then can be fed into your foreach.
foreach ($_POST['name'] as $key => $value) {
echo 'key: '.$key.' Value : '.$value;
}
--------------------------
output key: 0 Value : nameValue1
key: 1 Value : nameValue2
$_POST['name'] is an array, if you get multiple value with key, just get with foreach value

How to get at the value of a specific array ID

I have an array called $fields. If I do this:
foreach ($fields as $id => $field) {
print $id."<br />";
}
I get:
field_track_icon
field_session_number
field_job_title
I know this is totally simple, but my brain just will not come up with the answer, and I can't seem to google the right terms. How can I wrap the foreach in an if statement that's based on which id is being processed?
Pseudocode would be:
if($fields['field_session_number'] == 'S2') {
foreach($fields as $id=>$field) {
print $field->content;
}
}
Basically, I'm working with a single record of data returned in the $fields array. I only want to print out the individual field properties ($field->content or $field->prefix, for example) if the id of the field being processed is S2.
If you want to get element with specific key, you don't have to make for cycle - you just get it.
$field = $fields['field_session_number'];
echo $field->content;
echo $field->prefix;
If ID is not array key, but same as content or prefix, then you need for cycle:
foreach ($fields as $field) {
if ($field->id === 'S2') {
echo $field->content;
}
}

Categories