我正在使用MySQL/PHP
并且有一个搜索结果页面,该页面使用2个连接从多个表中提取数据。我得到的所有数据都很好。例如,特定记录(商店)有3种货币,我从我的货币表中提取它们。
但是,我只能在结果中显示第一种货币(欧元)。我希望货币的结果字段显示所选商店的货币表中的所有货币,并像(Euro, Dollars, Yen)
$query = "SELECT * FROM store
//joins part of query
where store.id=1
GROUP BY store.name
";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)){
Name: <?php echo $row['name']; ?>
Banking Currency Options:</b> <?php echo $row['currency']; ?>
//only displays first currency of the three records in the currency table.
//How do I return all 3 results/currencies here, separated by a comma?
//Like "Euro, Dollars, Yen"
//I need some sort of the second loop for currency
Languages:</b> <?php echo $row['language']; ?>
答案 0 :(得分:0)
请考虑以下事项:
数据集
SELECT colors, products FROM my_table ORDER BY colors,products;
+--------+----------+
| colors | products |
+--------+----------+
| Blue | Bag |
| Blue | Belt |
| Blue | Biro |
| Blue | Bucket |
| Blue | Cotton |
| Blue | Pants |
| Blue | Paper |
| Blue | Shoes |
| Yellow | Cap |
| Yellow | Car |
| Yellow | Hat |
| Yellow | Phone |
| Yellow | Pouch |
| Yellow | Sandal |
| Yellow | Shirt |
| Yellow | Socks |
| Yellow | Track |
+--------+----------+
代码
<?php
include('path/to/connection/stateme.nts');
$query = "SELECT colors, products FROM my_table ORDER BY colors,products;";
$result = mysqli_query($conn,$query);
$array = array();
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
}
foreach($array as $v){
$new_array[$v['colors']][] = $v['products'];
}
print_r($new_array);
?>
输出:
Array
(
[Blue] => Array
(
[0] => Bag
[1] => Belt
[2] => Biro
[3] => Bucket
[4] => Cotton
[5] => Pants
[6] => Paper
[7] => Shoes
)
[Yellow] => Array
(
[0] => Cap
[1] => Car
[2] => Hat
[3] => Phone
[4] => Pouch
[5] => Sandal
[6] => Shirt
[7] => Socks
[8] => Track
)
)
希望你以逗号分隔的字符串,或者一些JSON或者其他什么来吐出来,这不应该太过分。