Ok I am getting a little lost, but really appreciate everyone's help. Here is some code that may clarify things:
PHP Code:
updateSpec ( $_POST [ 'id1' ], $_POST [ 'desc1' ], $_POST [ 'value1' ], $_POST [ 'num1' ])
updateSpec ( $_POST [ 'id2' ], $_POST [ 'desc2' ], $_POST [ 'value2' ], $_POST [ 'num2' ])
updateSpec ( $_POST [ 'id3' ], $_POST [ 'desc3' ], $_POST [ 'value3' ], $_POST [ 'num3' ])
This function updates a spec block for a product. Each product may have a different number of specs, so I am using
PHP Code:
$count = mysql_num_rows
in a query to get the number of rows, and then want to execute a loop $count times.
I could just cut and paste the code 50 times and use (if(isset()), but that doesn't seem the most efficient may.
I know I need to run some kind of loop, but not sure how to code exactly. Hope this clarifies and any help is greatly appreciated.
Where you are creating your form...
Code:
while($spec=mysql_fetch_array($sql)) { ?>
<tr>
<td width="10%" valign="top"><input type="text" size="2" value="<?php echo $spec['num']; ?>" name="num[]" ></td>
<td width="30%" valign="top"><input type="text" size="30" value="<?php echo $spec['descriptor']; ?>" name="desc[]" ></td>
<td width="30%" valign="top"><textarea name="value[]" ><?php echo $spec['value']; ?></textarea></td>
<input type="hidden" value="<?php echo $spec['id']; ?>" name="id[]" >
<td width="30%"></td>
</tr>
<?php } ?>
criterion9 I made that change in my HTML form, how to I get the php process page to update all the rows using the function?
You mean the code that was already posted?
PHP Code:
if(isset( $_POST [ 'id' ]) && is_array ( $_POST [ 'id' ])){ $i = 0 ; foreach( $_POST [ 'id' ] as $id ) { updateSpec ( $_POST [ 'id' ][ $i ], $_POST [ 'desc' ][ $i ], $_POST [ 'value' ][ $i ], $_POST [ 'num' ][ $i ]); $i ++; } }
Only minor modifications to include the other _POST vars were made.
Last edited by criterion9; 02-10-2010 at 03:32 PM .
Oh my gosh, that worked as is simply amazing code. So stoked thanks for everyone's help. webdeveloper.com rocks.
Special thanks to criterion9 for staying with me through my stupidness.
Lol. Sometimes it takes a couple looks at someone else's code before it sinks in how simplistic it can be. You might take a look at the array page on php.net to see if that helps a bit with the amazing power of arrays.
Originally Posted by
themonkey40
I have the variable $id and I want to make it $id1, $id2, $id3... How can I code it so that it will make $id1?
The answer to your original question is here:
http://www.php.net/manual/en/languag...s.variable.php
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks