Page 1 of 1

looping through multiple arrays

Posted: Mon Jun 02, 2008 9:20 pm
by kenoli
I am trying to capture the data from a form with several fields in a way that I can update a number of database records, each with several fields, from one form submittal. I can capture the data from one of the form inputs like this:

Code: Select all

<?php
 
if ($_POST['Submit']) {
 
   foreach($_POST['test'] as $key => $value) {
 
   echo $value . '<br/>';
   }
}
?>
 
<form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  <input name="Submit" type="Submit">
</form>
Is there a way I can also capture the data from $_POST['when'] or even more similar field arrays?

I'm sure there is a way to do this but can't see it. I would greatly appreciate help figuring this out.

Thanks,

--Kenoli

Re: looping through multiple arrays

Posted: Mon Jun 02, 2008 11:07 pm
by RobertGonzalez
What is not working for you the way it is now?

PS You may want to use isset() when checking array values.

PPS You might also not want to check a form buttom for your form submission. What happens if the button doesn't send? Try look at the $_SERVER['HTTP_REQUEST_METHOD'] being equal (==) to 'POST'

Re: looping through multiple arrays

Posted: Tue Jun 03, 2008 12:43 am
by kenoli
Everah wrote:
>>What is not working for you the way it is now?

I am only looping through one of the arrays. I don't know how to loop through both of them.

I want to be able to do something like:

foreach . . . {

$query = "UPDATE table SET column1 = '$_POST['test']', column2 = '$_POST['help']' WHERE id = $_POST['id']";

}

But I can't figure out how to do a foreach (or any other loop) with multiple arrays.

Thanks,

--Kenoli

Re: looping through multiple arrays

Posted: Tue Jun 03, 2008 12:53 am
by RobertGonzalez

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $testcount = count($_POST['test']);
  $whencount = count($_POST['when']);
 
  for ($i = 0; $i < $testcount; $i++) {
    // Do what you need to with $_POST['test'][$i]
  }
 
  for ($i = 0; $i < $whencount; $i++) {
    // Do what you need to with $_POST['when'][$i]
  }
}
?>
If you are certain that the arrays will be the same size you can handle both test and when processes in the same loop.

Re: looping through multiple arrays

Posted: Tue Jun 03, 2008 7:58 pm
by kenoli
Everah -- Thanks for your attention and patience. Your solution makes a lot sense for accessing the data from each array. I'm not sure if it gets me what I need. I'm still feeling a bit clueless.

I'm trying to update a database with a query made up of elements from each array combined into one query.

Something like:

Take the first element of the first array, give it the formatting it needs for the query, add the same from the next array and so on with other arrays, update that record and then increment up to the next element and do the same.

The code you set up would mean looping all the way through the first array and then looping all the way through the next, etc.

I guess I could put the data from the first loop into an array with the appropriate formatting for the query, the same with the others and then somehow concatenate the elements of all the arrays and then loop through the resulting array, making the database update with each element.

Or, update the first column of the first record, the second column of the first record, the third column of the first record, etc. and then on the do the same thing with each subsequent record and columns associated with it. This is a lot of updates.

Both of these approaches seem so roundabout.

Am I missing something?

Maybe I should have asked the question differently.

Like: If one has an html form with data in it from a bunch of database records, how would one allow a user to go through the form, make edits on a number of records and then post the whole thing to be updated by one script? What would the script look like?

Am I completely out in bozo land?

Thanks,

--Kenoli

Re: looping through multiple arrays

Posted: Tue Jun 03, 2008 11:03 pm
by RobertGonzalez
That may have more to do with the way you set your form up.

Have you tried posting (as in an HTTP POST request) the form you posted in your original post and var_dump($_POST) as the post?

<sidebar>
Could I say post any more than I did in that last sentence?
</sidebar>