gpt4 book ai didi

php - Mysql餐厅菜单

转载 作者:行者123 更新时间:2023-11-29 06:42:11 24 4
gpt4 key购买 nike

<分区>

我正在制作一份餐厅菜单,我想轻松地显示同一产品不同尺寸的不同价格,例如比萨饼。

由于数据库很大,我已经简化了信息,所以如果草率,请原谅。

Mysql 表

表:食物

id |     type             | size    | price | category
------------------------------------------------------
1 | all dressed | x-small | 7,65 | pizzas
2 | all dressed | small | 9,65 | pizzas
3 | all dressed | medium | 11,65 | pizzas
4 | all dressed | large | 18,65 | pizzas
5 | pepperoni and cheese | x-small | 6,65 | pizzas
6 | pepperoni and cheese | small | 8,65 | pizzas
7 | pepperoni and cheese | medium | 10,65 | pizzas
8 | pepperoni and cheese | large | 15,65 | pizzas

id int(11)
name varchar(255)
size varchar(60)
price varchar(50)

表:类别

id | name
---|-----------
1 | pizzas
2 | subs
3 | wings

id int(11)
name varchar(255)

想要的布局结果

Pizzas

|------------------------------------------------------------------|
| Flavor x-small small medium large |
|------------------------------------------------------------------|
| all dressed 7,65 9,65 11,65 18,65 |
|------------------------------------------------------------------|
| pepperoni and cheese 6,65 8,65 10,65 16,65 |
|------------------------------------------------------------------|

尝试过的代码

第一个循环主要是为了确保信息得到解析。

----| queries.php |------------
public function my_products()
{
global $db;
$query = "SELECT * FROM foods ORDER BY name, price ASC";
return $db->select($query);
}
public function my_categories()
{
global $db;
$query = "SELECT * FROM categories ORDER BY name ASC";
return $db->select($query);
}

----| page.php |---------------
<?php $products = $query->my_products(); ?>
<?php $categories = $query->my_categories(); ?>

<table>
<?php
foreach($categories as $category)
{
echo "<h3>".$category->name."<h3>";
echo "<table>";

foreach ( $products as $product )
{
if($category->id == $product->category)
{
echo "<tr>";
echo "<td>".$product->name."</td>";
echo "<td>".$product->size."</td>";
echo "<td>".$product->price."</td>";
echo "</tr>";
}
}

echo "</table>";
}
?>
</table>

...返回...

pizzas
| ------------|---------|-------|
| all dressed | x-small | 7,65 |
| all dressed | small | 9,65 |
| ... | | |
|-------------|---------|-------|
subs

wings

现在我已经确认信息已返回,我正在尝试让数据库以特定顺序返回比萨饼的大小,但我不想对这些值进行硬编码,因为名称是尺寸因产品而异(例如,比萨饼为 x-small、small、medium、large,而 subs 为 6"和 12") .

我怎样才能按应有的顺序阅读尺寸的顺序?除了使我的字段成为 ENUM 类型并遍历我的所有条目之外,还有其他方法可以遍历 $product->size 并适本地填写表格吗?

24 4 0