Hey guys,

I need a bit of help...
I have the following....

<input name="title[1]" type="text" id="title[1]" size="30">
<textarea name="desc[1]" id="desc[1]" cols="50" rows="5"></textarea>
<textarea name="keywords[1]" id="keywords[1]" cols="50" rows="5"></textarea>
<input name="title[2]" type="text" id="title[2]" size="30">
<textarea name="desc[2]" id="desc[2]" cols="50" rows="5"></textarea>
<textarea name="keywords[2]" id="keywords[2]" cols="50" rows="5"></textarea>

Thing I want to happen is it to loop through and update each item in a table row where the id = array key...

but im not sure how I would do it using 1 query as id have you use a foreach... can someone with a better brain then me explain how they would go about it please?


6 Years
Discussion Span
Last Post by phper

So you have an array with your data? And you just want to create something like the above out of that array - try this:

$data[] = array('title' => 'Apple', 'desc' => 'green', 'keyword' => 'round, green');
$data[] = array('title' => 'Banana', 'desc' => 'yellow', 'keyword' => 'long, yellow');
$data[] = array('title' => 'Orange', 'desc' => 'Orange', 'keyword' => 'round, Orange');

$item_number = 1;

foreach($data as $current_data){

$current_item_number = $item_number++;

echo '<input name="title['.$current_item_number.']" type="text" id="title['.$current_item_number.']" size="30" value="'.$current_data['title'].'">
<textarea name="desc['.$current_item_number.']" id="desc['.$current_item_number.']" cols="50" rows="5">'.$current_data['desc'].'</textarea>
<textarea name="keywords['.$current_item_number.']" id="keywords['.$current_item_number.']" cols="50" rows="5">'.$current_data['keyword'].'</textarea>


Thanks for your comment back... not quite...

When the form is submitted I will have 3 arrays... like...

$title = array('1' => 'Home', '2' => 'About us');
$desc = array('1' => 'Homepage', '2' => 'About us page');
$keywords = array('1' => 'home, name, bla', '2' => 'about us, company info, bla, bla');

Then I need to loop through each and get something like this...

UPDATE table SET `title` = '$title',`desc` = '$desc',`keywords` = '$keywords' WHERE id = 'ARRAY_KEY'...

I hope you understand now.



Yeah the way you're storing in the array is much more complicated to get the data back out of. If you have one array called $dataset (for example) and you store each of the keys in that. You will still be allocated an array key.

If you do:

$data[] = array('title' => 'Apple', 'desc' => 'green', 'keyword' => 'round, green');
$data[] = array('title' => 'Banana', 'desc' => 'yellow', 'keyword' => 'long, yellow');
$data[] = array('title' => 'Orange', 'desc' => 'Orange', 'keyword' => 'round, Orange');

echo '<pre>';
echo '</pre>';

You will see each dataset combined by key.

Edited by phper: Forgot to add on the final part :-)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.