douquanzhan0315 2018-03-07 19:41
浏览 40
已采纳

订购表格中的数据

I have three tables created by uploaded data from the previous file. I would like to insert data in defined places, so that each service is a separate record in the table and has a separate quantity and amount assigned. My php function:

function listService()
{
    $service_chcecked = $_POST['service_chcecked'];
    $quantity = $_POST['quantity_chcecked'];
    $net_price = $_POST['net_price_chcecked'];
    for ($x = 0; $x < count($service_chcecked); ++$x) {
        echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
        for ($y = 0; $y < count($net_price); ++$y) {
            echo '<td>price:' . $net_price[$y] . '</td>';
            for ($z = 0; $z < count($quantity); ++$z) {
                echo '<td>quantity:' . $quantity[$z] . '</td>';
            };
        };
        echo '</tr>';
    }
}

And my html place:

  <div class='services'>
  <table>
    <tr>
      <th><span>NO.</span></th>
      <th><span>Service name</span></th>
      <th><span>Net price</span></th>
      <th><span>Quantity</span></th>
    </tr>
        <?php listServiceName(); ?>
    </table>
</div>

Now it displays to me this way, with repeated data at the end :/ broken table

  • 写回答

2条回答 默认 最新

  • 普通网友 2018-03-07 19:57
    关注

    That's because you are nesting your for loops. You create the <tr> and the "id" and "name" <td> for each item in service_chcecked. Then you create a "price" <td> for each service_chcecked * net_price_chcecked. And your 3rd nested loop creates a "quantity" column for each service_chcecked * net_price_chcecked * quantity_chcecked. That's why you end up with that broken table. It depends on how you receive the POST data, but if your three arrays always have the same length, you could do it all in one loop:

    function listService()
    {
        $service_chcecked = $_POST['service_chcecked'];
        $quantity = $_POST['quantity_chcecked'];
        $net_price = $_POST['net_price_chcecked'];
        for ($x = 0; $x < count($service_chcecked); ++$x) {
            echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
            echo '<td>price:' . $net_price[$x] . '</td>';
            echo '<td>quantity:' . $quantity[$x] . '</td>';
            echo '</tr>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧