我面临一个问题,我在“循环”中创建了一个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发布于 2020-06-29 10:03:44
您的表的末尾是内部循环。使表头和页脚在外部循环。
<?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 } ?>发布于 2020-06-29 09:14:21
从您的第一个代码示例开始,您可以简单地将表和头帽移出循环。
<?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>";
}
?>https://stackoverflow.com/questions/62634765
复制相似问题