【问题标题】:Create Html table in php foreach loop在 php foreach 循环中创建 Html 表
【发布时间】:2020-06-29 18:03:44
【问题描述】:

我遇到了一个问题,我在“Php foreach 循环”中创建了一个 html 表,以便从 Mysql 数据循环到 html 表中。
但是当我有超过 1 行数据时,它不会显示在同一个表中,而是显示在独立表中的每一行数据。

<?php
    global $wpdb, $indeed_db;
    $user = wp_get_current_user();
    $userid = $user->ID; 
    $woo_orders = $wpdb->get_results("SELECT * FROM wp8u_wc_order_product_lookup");
    foreach ($woo_orders as $print ){
        $order_id = $print->order_id; 
        $woo_customer_id = $print->customer_id;  
        $woo_customer = $wpdb->get_results("SELECT * FROM wp8u_wc_customer_lookup where customer_id=$woo_customer_id");
        foreach($woo_customer as $print2){
            $current_user_uid = $print2->user_id;
        }
        $date1 = strtotime($print->date_created);
        $date_created = date('Y-m-d H:i:s', $date1);                                                                      
        $amount = $print->product_net_revenue;                                                                      
        if($userid == $current_user_uid){ ?>
                <table>
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>My Customer ID</th>
                            <th>Create Date</th>
                            <th>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><?php echo "$order_id";?></td>
                            <td><?php echo "$woo_customer_id";?></td>
                            <td><?php echo "$date_created";?></td>
                            <td><?php echo "$amount";?></td>
                        </tr>
                    </tbody>
                    </table>
        <?php }
}
?>

输出:

Order ID | My Customer ID |    Create Date         |   Amount   |
126        9                   2020-06-24 13:45:35     3000        <-- 1st row data
Order ID | My Customer ID |    Create Date         |   Amount   |
123        9                   2020-06-22 12:01:14     1000        <-- 2nd row data show in independent table

在做了一些研究后,我尝试使用这种方法,但结果发现表格只存储第一行数据,第二行及之后的数据显示在表格的外侧而不是显示在表格内部

<?php
    global $wpdb, $indeed_db;
    $user = wp_get_current_user();
    $userid = $user->ID; 
    ?>
    <table>
        <thead>
            <tr>
                <th>Order ID</th>
                <th>My ID</th>
                <th>Create Date</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
    <?php
    $woo_orders = $wpdb->get_results("SELECT * FROM wp8u_wc_order_product_lookup");
    foreach ($woo_orders as $print ){
        $order_id = $print->order_id; 
        $woo_customer_id = $print->customer_id;  
        $woo_customer = $wpdb->get_results("SELECT * FROM wp8u_wc_customer_lookup where customer_id=$woo_customer_id");
        foreach($woo_customer as $print2){
            $current_user_uid = $print2->user_id;
        }
        $date1 = strtotime($print->date_created);
        $date_created = date('Y-m-d H:i:s', $date1);                                                                      
        $amount = $print->product_net_revenue;                                                                      
        if($userid == $current_user_uid){ ?>
                <tr>
                    <td><?php echo "$order_id";?></td>
                    <td><?php echo "$woo_customer_id";?></td>
                    <td><?php echo "$date_created";?></td>
                    <td><?php echo "$amount";?></td>
                </tr>
            </tbody>
        </table>
        <?php }
}
?>

输出:

Order ID |  My ID | My Customer ID |    Create Date         |   Amount   | 
126         37          9               2020-06-24 13:45:35     3000       <-- 1st row data         

123 9 2020-06-22 12:01:14 2020-07-22 12:01:14 1000                         <-- 2nd row data show outside of the table

【问题讨论】:

  • SELECT * FROM wp8u_wc_order_product_lookup JOIN wp8u_wc_customer_lookup USING (customer_id)。循环中的选择很糟糕:-)

标签: php html mysql foreach html-table


【解决方案1】:

你的桌子的末端在循环内。在循环之外制作表格页眉和页脚。

<?php
    global $wpdb, $indeed_db;
    $user = wp_get_current_user();
    $userid = $user->ID; ?>
<table>
  <thead>
    <tr>
      <th>Order ID</th>
      <th>My ID</th>
      <th>Create Date</th>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <?php
    $woo_orders = $wpdb->get_results("SELECT * FROM wp8u_wc_order_product_lookup"); foreach ($woo_orders as $print ){ $order_id = $print->order_id; $woo_customer_id = $print->customer_id; $woo_customer = $wpdb->get_results("SELECT * FROM
    wp8u_wc_customer_lookup where customer_id=$woo_customer_id"); foreach($woo_customer as $print2){ $current_user_uid = $print2->user_id; } $date1 = strtotime($print->date_created); $date_created = date('Y-m-d H:i:s', $date1); $amount =
    $print->product_net_revenue; if($userid == $current_user_uid){ ?>
    <tr>
      <td><?php echo "$order_id";?></td>
      <td><?php echo "$woo_customer_id";?></td>
      <td><?php echo "$date_created";?></td>
      <td><?php echo "$amount";?></td>
    </tr>
    <?php } ?>
  </tbody>
</table>
<?php    }    ?>

【讨论】:

    【解决方案2】:

    从您的第一个代码示例开始您可以简单地将表格和头部帽子移到循环之外

    <?php
        global $wpdb, $indeed_db;
        $user = wp_get_current_user();
        $userid = $user->ID; 
        $woo_orders = $wpdb->get_results("SELECT * FROM wp8u_wc_order_product_lookup");
        echo "table>
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>My Customer ID</th>
                                <th>Create Date</th>
                                <th>Amount</th>
                            </tr>
                        </thead>";
    
        foreach ($woo_orders as $print ){
            $order_id = $print->order_id; 
            $woo_customer_id = $print->customer_id;  
            $woo_customer = $wpdb->get_results("SELECT * FROM wp8u_wc_customer_lookup where customer_id=$woo_customer_id");
            foreach($woo_customer as $print2){
                $current_user_uid = $print2->user_id;
            }
            $date1 = strtotime($print->date_created);
            $date_created = date('Y-m-d H:i:s', $date1);                                                                      
            $amount = $print->product_net_revenue;                                                                      
            if($userid == $current_user_uid){ ?>
                    
                        <tbody>
                            <tr>
                                <td><?php echo "$order_id";?></td>
                                <td><?php echo "$woo_customer_id";?></td>
                                <td><?php echo "$date_created";?></td>
                                <td><?php echo "$amount";?></td>
                            </tr>
                        </tbody>
    
            <?php }
            echo "</table>";
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2016-03-24
      • 2013-06-10
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多