要設置您的產品在相同的順序,我會按照他們的數組中的鍵。出於我們的目的,我們將使用multidimensional array,以便我們可以將我們的產品添加到唯一密鑰中(使用示例中的「日期」)。下面你會看到我設置數組,從數據庫中獲取行(通過我們的組密鑰進行排序,以便我們在前端具有一致性),並開始將它們放入其獨特的組中。將產品推入日期數組時,我使用array_merge()結合in_array()和ternary operator在HTML中設置「產品字符串」。
<?php
/* Fetch/Set Kitchen */
$kitchen = array();
$sql = "SELECT * FROM `kitchen` ORDER BY `date`";
$query = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($query)) {
$kitchen[$row['date']][] = array_merge($row, array(
'product_string' => (in_array($row['product_code'], array('A01', 'A02', 'A03', 'A04')) !== FALSE)
? $row['product_code'] . ' - ' . $row['product_name'] . ' (' . $row['size'] . ')'
: $row['product_code'] . ' - ' . $row['product_name']
));
}
?>
爲了保持我們的HTML整潔,從我們的PHP除了可讀,你會看到,我選擇要使用的控制結構的alternative syntax。這有助於通過使用製表符縮進來放置代碼中的任何非常笨拙的大括號。
<?php foreach($kitchen as $date => $items): ?>
<div class="alert alert-info" role="alert" id="<?php echo $date; ?>">
<?php foreach($items as $item): ?>
<h4>Table <?php echo $item['table']; ?></h4>
<h4>Name: <?php echo $item['customer']; ?></h4>
<h4><?php echo $item['product_string']; ?></h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="<?php echo $item['data']; ?>" type="submit">Order Ready</button>
</form>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
上面的參考代碼將輸出HTML類似:
<div class="alert alert-info" role="alert" id="2016-10-21">
<h4>Table Table 1</h4>
<h4>Name: Name 1</h4>
<h4>XXX1 - Product 1 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX1" type="submit">Order Ready</button>
</form>
<h4>Table Table 2</h4>
<h4>Name: Name 2</h4>
<h4>XXX2 - Product 2</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX2" type="submit">Order Ready</button>
</form>
<h4>Table Table 3</h4>
<h4>Name: Name 3</h4>
<h4>XXX3 - Product 3 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX3" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-10-27">
<h4>Table Table 4</h4>
<h4>Name: Name 4</h4>
<h4>XXX4 - Product 4</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX4" type="submit">Order Ready</button>
</form>
<h4>Table Table 5</h4>
<h4>Name: Name 5</h4>
<h4>XXX5 - Product 5 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX5" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-11-06">
...etc.
有實現這一目標的多種方式,但最簡單的將是BY子句添加命令,你的查詢語句。這樣,同一訂單的所有產品將一起列出 – jeff