I have the following query:
$myquery = mysql_query("SELECT p.name, GROUP_CONCAT(m.name) as products
FROM access as pm
INNER JOIN seller as p on p.id = pm.id_seller
INNER JOIN products as m on m.id = pm.id_product
WHERE p.id = '".$_COOKIE['id_seller']."'");
Which returns me this array:
Array (
[0] => Seller Name [name] => Seller Name
[1] => Product1 [products] => Product1
);
How should I query the database correctly? Thanking in consideration that I have 3 tables (products, access and seller). What I'd like to achieve is to attribute (access) one or more products to a seller.
And how my drop-down list populate code would look like?
<?php
echo'<select name="productaccess">';
foreach($myquery as $product) {
echo'<option value="'.$product.'">'.$product.'</option>';
}
echo'</select>';
?>
Thanks!
If you want the products for just one seller, you don't need grouping at all. Just list the names of the products:
$myquery = mysql_query("SELECT m.name
FROM access as pm
INNER JOIN seller as p on p.id = pm.id_seller
INNER JOIN products as m on m.id = pm.id_product
WHERE p.id = '".$_COOKIE['id_seller']."'");
Now $myquery
is only a resource identifier, not the results, so you get the latter with mysql_fetch_array()
like this:
while($row=mysql_fetch_array($myquery)) {
echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
GROUP_CONCAT is used with a GROUP BY clause, which is not present. Add a 'GROUP BY p.name' to your SQL. That will make the above SQL return a single row, with the seller name and the product names, separated by commas. Then burst the product names into an array, which you then feed to the foreach loop.
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.