我有一个表格,显示不同的文章(即具有不同颜色和大小的搜索者)。我只需要显示具有特定颜色和尺寸的单篇文章。我是否需要对我的查询进行过滤器(如果,是哪一个?还是我需要另一个相关表?
这是我的代码:
<?php
$id_prodotto=$_GET['cod_prodotto'];
$conn=mysql_connect("xxx", "xxx", "xxx");
$db=mysql_select_db("my_database", $conn);
$risultato=mysql_query("
select distinct nome_articolo
, foto
from prodotti
where id_prodotto_generico = '$id_prodotto'
order
by nome_articolo
", $conn);
/*apro tabella*/
echo "
<center>
<table class='tabella_categorie'>
<tr>
<th class='categoria'>DESCRIZIONE</th>
<th class='dettaglio'>DETTAGLIO</th>
</tr>";
while ($riga=mysql_fetch_array($risultato)){
$id_prodotto=$riga[id];
$foto=$riga[foto];
echo "<tr>
<td class='id_contenuto'><img src='../negozio/admin/images/$foto'></td>
<td class='categoria_contenuto'>$riga[nome_articolo]</td>
<td class='dettaglio_contenuto'><a href='scheda_prodotto.php?cod_prodotto=$id_prodotto'><img src='lente.gif' class='link_immagine'></a> Visualizza</td>
</tr>";
}
echo "</table>
</center>";
mysql_close($conn);
?>
此代码显示一个列表,该列表不会重复具有相同名称的产品。我想通过单击单个产品来获得另一个列表,其中显示了所选产品的所有颜色和尺寸。
表结构为:id,nome_articolo,克拉特画(细节),摄影,蜥蜴,意大利面条(大小),彩色,匡蒂塔
提前感谢您的帮助。
您需要另一个以$nome_articolo
作为参数的页面。
例如,使用类似 href="similar_products.php?nome_articolo=$riga[nome_articolo]
然后做"SELECT * FROM prodotti WHERE nome_articolo = ".$_GET['nome_articolo']
此语句正在从表中查找不同的记录集。如果要获取特定类别中的列表,请使用以下内容:
编辑:看到您的评论并再次阅读,您将希望按照奥列格·杜巴斯的建议进行操作。您的查询应从某个类别中搜索产品 ID:
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
// Search for name, order by size or color
$sql = "select * from `prodotti` where nome_articolo = :nome_articolo order by taglia ASC";
// Use a prepared statement to help safeguard injection
$query = $conn->prepare($sql);
$query->execute(array(':nome_articolo'=>$nome_articolo));
// Loop results
while($risultato = $query->fetch(PDO::FETCH_ASSOC)) {
print_r($risultato);
}
我认为您应该使用另一个相关的表格而不是使用单个表格,这是因为裤子有不同的颜色,每种颜色都有很多尺寸。这种方法是为了消除冗余。如果您使用相关表格,则在编辑数据(例如颜色或大小)时无需更改每个字段。您只需要编辑相关表上的数据
给你:
table_the_articles
+-----------------+---------+----------+---------+
| id | article_id | photo | color_id | size_id |
+----+------------+---------+----------+---------+
| 1 | 1 | photo_A | 1 | 1 |
| 2 | 1 | photo_A | 1 | 2 |
| 3 | 2 | photo_C | 2 | 1 |
+----+------------+---------+----------+---------+
table_article
+----------------+
| id | article |
+----+-----------+
| 1 | Trouser A |
| 2 | Trouser B |
+----+-----------+
table_color
+------------+
| id | color |
+----+-------+
| 1 | Black |
| 2 | White |
+----+-------+
table_size
+-----------+
| id | size |
+----+------+
| 1 | S |
| 2 | M |
| 3 | L |
+----+------+
article_id
字段作为外键table_the_articles
与table_article
上的id
字段相关,然后table_the_articles
上作为外键的color_id
字段与table_color
上的id
字段相关,size_id
字段作为外键table_the_articles
与table_size
上的id
字段相关。不要忘记在这些表上使用innodb
引擎。
可以使用此查询显示具有所有可用颜色和大小的特定文章:
SELECT name, foto FROM ((table_the_articles a JOIN table_article b ON (a.article_id=b.id)) JOIN table_color c ON (a.color_id=c.id)) JOIN table_size d ON (a.size_id = d.id) WHERE a.article_id='$id_prodotto' ORDER BY a.color_id, a.size_id
因为产品有很多尺寸,如(S,M,L,XL,XXL)和颜色(红色,绿色,蓝色等)
所以最好的方法是你需要3个相关的表,让我们假设产品表,product_detail_size表
产品表
- product_id
- product_name
- product_title
- product_content
- product_photo
- 等
Product_size表
- product_size_id
- product_id
- 大小
Product_color表
- product_color_id
- product_id
- 颜色
然后要显示您想要的内容,只需将代码放如下
<?php
// pick value of product_id parameter in url
$product_id=$_GET['product_id'];
$conn=mysql_connect("xxx", "xxx", "xxx");
$db=mysql_select_db("my_database", $conn);
$result=mysql_query("
select * from product where product_id = '$product_id'
", $conn);
// from this you only get 1 product record from product table , like product_title, product_content, etc.
// get size of spesific product id
$size_result = mysql_query("select * from product_size where product_id = '$product_id'",$conn);
// get color of spesific product id
$color_result = mysql_query("select * from product_color where product_id = '$product_id'",$conn);
// then show the result like this
while ($product=mysql_fetch_array($result)){
$product_id=$product[product_id];
$image=$product[product_photo];
$title=$product[product_title];
$content=$product[product_content];
// from this you only get product detail without color and size
echo "
title is : $title<br>
content is : $content<br>
image is : $image<br>
";
// then show the size
while($product_size = mysql_fetch_array($result_size)){
$size = $product_size[size];
echo "size is : $size<br>";
}
// then show the color
while($product_color = mysql_fetch_array($result_color)){
$color = $product_size[color];
echo "color is : $color<br>";
}
}
?>
希望你能将我的原型代码翻译成你的真实代码。