doukou1718 2011-11-22 16:30
浏览 21
已采纳

无法使用PHP正确生成html表

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!

  • 写回答

2条回答 默认 最新

  • dongpu8935 2011-11-22 16:51
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?