尝试创建商店,但我无法弄清楚如何以正确的方式从我的数据库中检索信息。
这是获取商店信息的PHP,点击了:
require_once "core/init.php";
include "includes/head.php";
$na = $_SESSION['name'];
$sql = "SELECT DISTINCT aisle FROM aisle WHERE brand ='$na' ";
$pquery = $db->query($sql);
$pql = "SELECT * FROM aisle WHERE brand ='$na'";
$squery = $db->query($pql);
我试过的循环:
<?php while($prod = mysqli_fetch_assoc($squery)): ?>
<div id="<?php echo $prod['aisle']?>" class="container">
<h3><?php echo $prod['aisle']?></h3>
<form action="" method="post">
<button>
<img src="fd.png" style="width: 95%" />
<h4><?php echo $prod['product']?></h4>
<h5>$<?php echo $prod['price']?></h5>
</button>
</form>
</div>
<?php endwhile;?>
数据库
║ id ║ brand ║ aisle ║ product ║ price
╠════╬═════════╬═════════╬═════════╬═══════
║ 1 ║ brand1 ║ snacks ║ Bitz ║ 3
║ 2 ║ brand2 ║ drinks ║ Water ║ 1.20
║ 3 ║ brand3 ║ snack ║ Rugi ║ 3.05
║ 4 ║ brand4 ║ sides ║ Fries ║ 3.95
答案 0 :(得分:0)
类似的东西,但要确保你通过过道订购
<form action="" method="post">
<?php
$current_aisle = 'None' #start with NONE
while($prod = mysqli_fetch_assoc($squery)):
if $current_aisle != $prod['aisle'] { ?> <!-- check if same aisle, if not then build header -->
<div id="<?php echo $prod['aisle']?>" class="container">
<h3><?php echo $prod['aisle']?></h3>
<?php $current_aisle = $prod['aisle']; } ?> <!-- reassign current aisle so the next iteration won't create the header -->
<button>
<img src="fd.png" style="width: 95%" />
<h4><?php echo $prod['product']?></h4>
<h5>$<?php echo $prod['price']?></h5>
</button>
</div>
<?php endwhile;?>
</form>
答案 1 :(得分:0)
您可以使用SQL做一些奇特的事情,以便更轻松地使用它。
例如,您可以使用以下查询:
SELECT aisle, GROUP_CONCAT(CONCAT(product,',',price)) as product
FROM stock
GROUP BY aisle
ORDER BY aisle, product ASC
<子> Here's the fiddle 子>