Progger
Progger

Reputation: 2254

Cannot get foreach to properly generate html table using PHP

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!

Upvotes: 0

Views: 925

Answers (2)

Phil
Phil

Reputation: 380

You need to use a different variable for the iteration variable in the first block of code, one that's not the array.

foreach ($members as $member) 

make it:

<table>
  <?php foreach ($members as $member) : ?>
    <tr>
      <td><?php echo $member['member_ID']; ?></td>
      <td><?php echo $member['nick_name']; ?></td>
      <td><input type="text" name="percent_owner[]" value="" size="3"></td>
    </tr>
  <?php endforeach; ?>
</table>

Update:

You're trying to loop through both % for each row, what you want is the % that is in that row.

<table>
  <?php
   $i = 0;
   $percents = expload(',', $_POST['percent_owner']);
   foreach ($members as $member) : ?>                
   <tr>
     <td><?php echo $member['member_ID']; ?></td>
     <td><?php echo $member['nick_name']; ?></td>
     <td><?php echo $percents[$i++]; ?></td>                 
   </tr>
  <?php endforeach; ?>
</table>

And you'll probably want to change the name of the input in the first block to just "percent_owner". Also, take this code with a grain of salt, as it is untested.

Upvotes: 3

Patrick
Patrick

Reputation: 3440

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:

<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_<?=$members['member_ID']?>" value="" size="3"></td>
  </tr>
  <?php endforeach; ?>
</table>

Then on submit you would process it like this:

<?php
foreach ($_POST AS $input => $value) {
  if (stristr($input, 'percent_owner_')) {
    $memberID = str_replace('percent_owner_', '', $input);
    $res = mysql_fetch_object(mysql_query("SELECT nick_name FROM members WHERE member_ID = '$memberID'"));

    $data[$memberID] = array(
      'memberID' => $memberID,
      'nickName' => $res->nick_name,
      'percentOwner' => $value
    );
  }
}
?>

<table>
  <?php foreach ($data as $member) : ?>
  <tr>
    <td><?php echo $member['memberID']; ?></td>
    <td><?php echo $member['nickName']; ?></td>
    <td><?php echo $member['percentOwner']; ?></td>
   </tr>
  <?php endforeach; ?>
</table>

Upvotes: 1

Related Questions