我正在开设餐馆菜单,我希望能够轻松地显示同一产品的不同尺寸的不同价格,例如比萨饼。
由于数据库非常大,我已经简化了信息,所以请原谅我是否有些草率。
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
,而潜艇则为6" and 12"
。
我怎样才能阅读尺寸的顺序呢?除了使我的字段成为ENUM
类型并浏览我的所有条目之外,还有其他方法可以通过$product->size
并正确填写表格吗?
答案 0 :(得分:2)
您必须在size列上有一个order属性,以指定返回数据的顺序。系统根本无法知道什么是正确的,你必须给它正确的方法。
这样做的hackish方法是在size列中将不可见的ascii字符从低到高嵌入,以便引擎以所需的方式对它们进行排序。
更正确的方法是使一个与大小字段相关的订单字段指定给定产品的可用尺寸的大小顺序。然后,您可以使用数据透视表
进行转动答案 1 :(得分:0)
我强烈考虑更恰当地规范你的表结构:
products
product_id - autoincrement primary key
name - varchar holding values like "all dressed"
category_id - foreign key to category table
product_sizes
product_size_id autoincrement primary key
product_id foreign key to products table
product_size - varchar holding values like 'x-small', 'large', '6"', '12"'
product_price - float
sort_order - int holds values you can use for sorting within is product_id grouping
categories
category_id - autoincrement primary key
category_name - varchar folding values like 'pizza', 'subs', etc.
然后查询它们很简单:
SELECT
c.category_name AS category_name,
p.product_name AS product_name,
ps.product_size AS product_size,
ps.product_price AS product_price
FROM
categories AS c
INNER JOIN products AS p
ON c.category_id = p.category_id
INNER JOIN product_sizes AS ps
ON p.product_id = ps.product_id
ORDER BY
c.category_name ASC,
p.product_id ASC,
ps.product_order ASC
在PHP中,您只需构建一个多维数组,其维度为category,product_id和product_size,
$product_array = array();
while($row = [insert your DB fetch mechanism here]) {
$product_array[$row->category_name][$row->product_name][$row->product_size] = $row->product_price;
}
输出您的表格可能如下所示:
<?php
foreach ($product_array as $category => $cat_data) {
// output category heading
?>
<h2><?php echo $category; ?></h2>
<?php
// start a table perhaps
// get all sizes for this category
// this is done in a bit of a short-cut manner and assumes that each product in the category has the same options as the first product in the category
$size_array = array_keys($cat_data[key($cat_data)]);
?>
<table>
<tr>
<th>Flavor<th>
<?php
foreach($size_array as $size) {
?>
<th><?php echo $size; ?></ht>
<?php
}
?>
<th>Price</th>
</tr>
<?php
foreach ($cat_data as $prod_name => $prod_data) {
?>
<tr>
<td><?php echo $prod_name; ?></td>
foreach($prod_data as $size => $price) {
?>
<td><?php echo $price; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
<?php
}
?>