首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php foreach循环中创建Html表

在php foreach循环中创建Html表
EN

Stack Overflow用户
提问于 2020-06-29 09:07:02
回答 2查看 2.1K关注 0票数 0

我面临一个问题,我在“循环”中创建了一个html表,以便从Mysql数据循环到html表。

但是,当我有超过1行数据时,它不会显示在同一个表中,而是显示在独立表中的每一行数据中。

代码语言:javascript
复制
<?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 }
}
?>

产出:

代码语言:javascript
复制
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

在做了一些研究之后,我尝试使用这种方法,但是它只存储表中的第一行数据,第二行和第二行数据显示表的一侧,而不是显示在表中。

代码语言:javascript
复制
<?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 }
}
?>

产出:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-29 10:03:44

您的表的末尾是内部循环。使表头和页脚在外部循环。

代码语言:javascript
复制
<?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    }    ?>
票数 0
EN

Stack Overflow用户

发布于 2020-06-29 09:14:21

从您的第一个代码示例开始,您可以简单地将表和头帽移出循环。

代码语言:javascript
复制
<?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>";
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62634765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档