【发布时间】:2020-06-07 23:58:32
【问题描述】:
我有几个字段匹配一个字段。例如,我将字段大小作为属性,但它有许多与之匹配的字段。 我想回应类似的东西: 尺寸: s、m、l、xl、xxl…… 但是当我打印它时,它就像: 尺寸: s 尺寸:米 尺寸:l 尺寸:xl..
如何正确打印? 代码:
<?php
// Create connection
$conn = new mysqli('localhost', 'root','','catalog');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT DISTINCT categories.title AS title_categories , attributes.title AS title_attributes, labels_atr.title AS title_labels_atr
FROM categories
INNER JOIN attributes ON attributes.id=categories.id
INNER JOIN labels_atr ON labels_atr.id_atr=categories.id
GROUP BY labels_atr.title
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
extract($row);
echo "category: ".$title_categories.",attributes: ".$title_attributes."<BR>";
echo "labels_atr: ".$title_labels_atr."<BR>";
}
}
else {
echo "0 results";
}
$conn->close();
?>
输出:
category: Pants,attributes: Size
labels_atr: 32X32
category: Pants,attributes: Size
labels_atr: 32X34
category: Pants,attributes: Size
labels_atr: 32X36
category: Pants,attributes: Size
labels_atr: 34X32
category: Pants,attributes: Size
labels_atr: 34X36
category: Pants,attributes: Size
labels_atr: 36X32
category: Sale,attributes: Size
labels_atr: L
category: Sale,attributes: Size
labels_atr: M
category: Sale,attributes: Size
labels_atr: S
category: Sale,attributes: Size
labels_atr: XL
category: Sale,attributes: Size
labels_atr: XS
category: Sale,attributes: Size
labels_atr: XXL
category: Shirts,attributes: Color
labels_atr: Black
category: Shirts,attributes: Color
labels_atr: Blue
category: Shirts,attributes: Color
labels_atr: Grey
category: Shirts,attributes: Color
labels_atr: Orange
category: Shirts,attributes: Color
labels_atr: Red
category: Shirts,attributes: Color
labels_atr: White
category: Shirts,attributes: Color
labels_atr: Yellow
【问题讨论】:
-
您希望通过查询还是使用 PHP 来完成此操作?
-
它不会对我嘀咕,只要结果会好,我可以做一个html,这样例子的大小就是标题和s,m ,l 等。将是字段和标题
-
在 10 多年的编程生涯中,我从未找到一个令人信服的理由在我的任何项目中使用
extract()。我建议不要在这里 - 没有真正的好处。