I have the following 2 tables:
-- members --
|member_ID|nick_name|
---------------------
| 1 | Jack |
---------------------
| 2 | Jill |
---------------------
-- shares --
|member_ID|nick_name|percent_owner|
-----------------------------------
| 1 | Jack | 75 |
-----------------------------------
| 2 | Jill | 25 |
-----------------------------------
My first php page will query the members table and create an html table just fine. In the html table, I added a blank text box to manually enter the percent owner value. This is what that table looks like:
<table>
<?php foreach ($members as $members) : ?>
<tr>
<td><?php echo $members['member_ID']; ?></td>
<td><?php echo $members['nick_name']; ?></td>
<td><input type="text" name="percent_owner[]" value="" size="3"></td>
</tr>
<?php endforeach; ?>
</table>
Now, what I am trying to do is send the new information to another page to create another html table with the member_ID, nick_name, and percent owner. This where my problem lies. Here is the 2nd html table:
<table>
<?php foreach ($members as $member) : ?>
<?php foreach ($_POST['percent_owner'] as $value) : ?>
<tr>
<td><?php echo $member['member_ID']; ?></td>
<td><?php echo $member['nick_name']; ?></td>
<td><?php echo $value; ?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
I don’t believe I am organizing my foreach statements properly. Either I get only the first percent_owner value in every row, or I get both in each row, but cannot get it right. Once I have this figured out, I will then enter it all into the shares table but that will come later. I need to tackle this first.
thank you!
You need to be able to match the data entered to the row it is applied. One way to do it is to add a hidden value to each row with the member id parallel to the percent owner. Theoretically they will both end up with the same index in their respective arrays and you just need to match that up. But that is an ugly and unreliable way to do it.
Another way you can handle this (definitely the better of the two ways) is to set the name of the user input field for percent owner to be percent_owner_[member id] when your building the form field in the table. Then when you submit the form you can just iterate through the POST variables looking for the sub-string of “percent_owner_” and when you identify those inputs you extract the member id and the user-entered value and save it to an indexed array. Then you use the extracted id to query the DB for other member information.
Example form fields:
Then on submit you would process it like this: