how to fetch data using foreach - php

<input name="auth[]" type="text" id=" " value="<?php echo $cite_aut[$i]; ?>">
Above are text fields in html. Now how to get text fields values in a array variable in another php page.
I tried it by using foreach loop (foreach($val as $_POST['author'])... but it fetch 1 extra value for $val, if there are 5 text boxes then it is fetching 6 values, 6th value is "array".
can someone explain how to do it?

Should be:
foreach($_POST['auth'] as $key => $val) {
......
}

TRY
foreach($_POST['auth'] as $key => $val) {
echo $val;
}

Related

Passing all values into one variable then split them [duplicate]

This question already has answers here:
Passing an array using an HTML form hidden element
(8 answers)
Closed 3 years ago.
Hi so for the website I am designing I have been asked to use mal's eCommerce for the payment gateway. I have been trying to set up the remote call function they have so that once a purchase has been made it will make a call back to my website to run a script and sen email confirmation of their order.
This however has proved extremely frustrating as the session variables I have stored do not get passed through with the remote call, only certain values that they allow.
I tried storing them all in an array and putting them in one of the values I can change called sd:
$cart =array($_SESSION["date"], $_SESSION["name"], $_SESSION["email"], $_SESSION["number"], $_SESSION["address"], $_SESSION["town"], $_SESSION["postcode"],
$_SESSION["county"], $_SESSION["cake_type"], $_SESSION["collection"], $_SESSION["icingColour"], $_SESSION["trimColour"], $_SESSION["filling"], $_SESSION["wording"],
$_SESSION["cakePhoto"], $_SESSION["price"], $_SESSION["photo"] );
<input type="hidden" name="sd" value="<?php echo $cart?>">
Then in my script I tried:
$result = $_POST['sd'];
echo $result[0];
to test it
but the remote call just passed the value as "Array" so echo $result[0] just returned "A", the values was just passed as Array and none of the values in the array got passed with it.
So now I'm trying to store all my session variables in the sd value like this:
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val;}?>">
and then I tested it by doing on my script:
$result = $_POST['sd'];
echo $result;
Now this does pass all the values but obviously just as one big value, is there a way I can split them up? Is there a better way with the array I haven't thought of? Any advice would be greatly appreciated because I'm at a complete loss
Edit:
Forgot to add this part:
So after the array has been split I would need to stored the values separately so something like:
$name = first value
$email = second value etc
try changing <input type="hidden" name="sd" value="<?php echo $cart?> to
<input type="hidden" name="sd[]" value="<?php echo $cart?>
or use serialize() then unserialize() the result.
Refer to Passing an array using an HTML form hidden element
You can use php implode and explode function.
The first part is implode.
We use ; as implode delimeter which joins array elements as single string using that delimeter.
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val.';';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';', $_REQUEST["sd"]) as $key) {
echo $key;
echo "<br>";
}
Update : Try This
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $key.';'.$val.';;';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';;', $_REQUEST["sd"]) as $key) {
echo explode(';', $key)[0];
echo " = " ;
echo explode(';', $key)[1];
echo "<br>";
}

printing only required data from session array in th e required format and a

i want to bill the records stored in the session by avoiding unwanted data i am using foreach loop or print_r to print but it is giving conventional structure
<?php
$b = print_r(array_values ($_SESSION), true);
echo $b.'<br/>';
?>
<?php
foreach ($_SESSION as $key => &$value) {
$array[] = $value;
echo $value;
}
?>
now $value is giving the only the data and $b is giving data along with keys but i want to print the data in form of bill i stored different required fields in different session arrays using
array_push($_SESSION['a1'],$conum);
i want to print in form of html table independent of number values stored in the session array guys please help me out
Just store your data in $_SESSION['my_data'], i.e array_push($_SESSION['my_data']['a1'], $conum); then you can print it like this:
foreach ($_SESSION['my_data'] as $key => $value) {
echo "{$key} => {$value}";
}
So do it in this way:
echo '<table>';
foreach ($_SESSION['my_data'] as $key => $value) {
echo "<tr><td>{$key}</td><td>{$value}</td></tr>";
}
echo '</table>';

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

Get the ids and loop through post data

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
}

Use POST variables without calling them specifically

All,
I have a form that has some text inputs, checkboxes, select box and a textarea. I want to submit the form with PHP using a submit button to a page for processing.
That part is fine but what I would like to do is basically get the results into an array so I can do a foreach loop to process the results.
Basically I'm trying to create a dynamic form the submits to my backend processing script and don't want to hard code in the post values like the following:
$var1 = $_POST['var1'];
echo $var1;
$var2 = $_POST['var2'];
echo $var2;
Does anyone know how to go about doing something like this or give any recommendations?
If there're no other data in your POST but these generated elements, just do
foreach( $_POST as $key => $val ) {
// do your job
}
and process what you have. If you want to mix your generated entries with predefined you may want to put these generated in nested array:
<input ... name="generated[fieldname]" />
and then you iterate
foreach( $_POST['generated'] as $key => $val ) {
// do your job
}
Just use array notation:
<input name="vars[]" value="" />
Then you will have something like this as $_POST:
Array ('vars' => Array(
0 => 'val1'
1 => 'val2'
)
)
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val";
}
Actually, the $_POST variable is an array. you just need to extract the array values by using a simple foreach loop. that's it.
I hope this example help you.
foreach($_POST as $field_name => $val)
{
$asig = "\$".$field_name."='".addslashes($val)."';";
eval($asig);
}
After running this script all the POST values will be put in a variable with the same name than the field's name.

Categories