I am using MySQL/PHP
and have a search results page that pulls in data from several tables using 2 joins. I get all the data just fine. For example, there are 3 currencies for a particular record (Store), and I am pulling them from my currencies table. However, I can only display the first currency in my results (Euro). I want the results field for currency to display all currencies from the currencies table for the selected Store and echo like (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']; ?>
Consider the following:
Dataset
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 |
+--------+----------+
Code
<?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);
?>
Outputs:
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
)
)
Hopefully, it shouldn't be too much of a stretch for you to spit this out as comma delimited string, or a bit of JSON, or whatever.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.