我想通过连接从两个表中获取数据,并在可折叠/可折叠的情况下显示类别,在这些之下,我想在与该类别相关的表中显示产品信息。我有代码,但如屏幕截图所示,我一次又一次获取一个类别和表格数据,请帮忙。
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM `wp_orderlist`
JOIN wp_product_category ON
wp_product_category.item_id=wp_orderlist.category
where wp_product_category.item_id=wp_orderlist.category");
foreach ( $result as $print){
?>
<button class="collapsible"> <?php echo $print->category?>/button>
<div class="content">
<table border="1">
<tr>
<th>Item Description</th>
<th>Packing Size</th>
<th>Cart</th>
</tr>
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>'. $print->Item_Description.'</td>';
echo '<td>'. $print->Packing.'</td>';
echo '<td> <input type="submit" value="ADD"> </td>';
echo '</tr>';
} //ending nested loop
echo '</tr> ';
?>
</table>
</div>
<?php }?>//ending main loop
答案 0 :(得分:0)
<?php // I find my solution to my problem
global $wpdb;
$result = $wpdb->get_results ( "SELECT * from wp_product_category ");
foreach ( $result as $print){
$cat = $print->item_id;
?>
<button class="collapsible"><?=$print->category ?><span style="margin-
left:130px;"><?=$print->category ?></span> </button>
<div class="content">
<table border="1">
<tr>
<th>Item_ID</th>
<th>Item Description</th>
<th>Packing Size</th>
<th>Cart</th>
</tr>
<?php
$result1 = $wpdb->get_results ( "SELECT * FROM wp_orderlist where
category_id = $cat ");
foreach ( $result1 as $print1 ) {
echo '<tr>';
echo '<td>'. $print1->item_id.'</td>';
echo '<td>'. $print1->Item_Description.'</td>';
echo '<td>'. $print1->Packing.'</td>';
echo '<td> <form method="post"> <input type="submit" name="add"
href="$print1->item_id" value="ADD"></form> </td>';
echo '</tr>';
}
}
echo '</tr> ';
?>
</table>
</div>
<?php } ?>