简体   繁体   English

如何使用GROUP_CONCAT MySQL用PHP循环行

[英]How to loop rows with PHP using GROUP_CONCAT MySQL

I am triyng to loop the products coming from different categories as shown below: 我正在尝试循环展示来自不同类别的产品,如下所示:

 $sql = query("SELECT c.name AS 'cat_name', c.image AS 'image', 
                GROUP_CONCAT(DISTINCT p.title ORDER BY p.title DESC SEPARATOR ' ') AS 'product_name' 
                FROM products p 
                INNER JOIN categories c 
                ON p.category_id = c.id 
                GROUP BY p.category_id");
  confirm($sql);

  while($category = mysqli_fetch_array($sql)) {
  $content = <<<DELIMITER


       <li class="col-md-3 col-sm-6">
                <ul>
                  <li class="dropdown-header">{$category['cat_name']}</li>
                  <li><a title="Title" href="#">{$category['product_name']}</a></li>                    

                </ul>
              </li>



 DELIMITER;

 echo $content;
 }

The script above produces following result: 上面的脚本产生以下结果:

Category name 分类名称
Product 1 Product 2... 产品1产品2 ...

I would like to have the products listed like this: 我希望列出这样的产品:

Category name 分类名称
Product 1 产品1
Product 2 产品2
... ...

What I am doing wrong? 我做错了什么?

The function GROUP_CONCAT with get you a response of only one line for each category. 函数GROUP_CONCAT的作用是,每个类别的响应只有一行。 It will concatenate all your lines with the same category in only one. 它将所有的行与同一类别连接在一起。

You have two possibilities : 您有两种可能性:

  1. Using GROUP_CONCAT : each line will contain your Category with all your products concatenated. 使用GROUP_CONCAT :每行将包含您的类别以及所有已串联的产品。 Then you should split the concatenation of your "Product name's list" to display them correctly. 然后,应拆分“产品名称列表”的串联以正确显示它们。

  2. Do not use GROUP_CONCAT : you group your result by the category name. 不要使用GROUP_CONCAT :您将结果按类别名称分组。 In your loop, you check when the category name change and then change your html accordingly. 在循环中,检查类别名称何时更改,然后相应地更改html。


Edit: 编辑:

Your variable $category['product_name'] contains the names of your products concatenated by the function GROUP_CONCAT using the delimiter space . 您的变量$category['product_name']包含使用分隔符space由功能GROUP_CONCAT连接的产品名称。 If you write 如果你写

echo $category['product_name']

it will print 它将打印

Product 1 Product 2...

You can use split() (obsolete) or explode() to separate the products names. 您可以使用split() (过时)或explode()来分隔产品名称。 And then, you will need to iterate over the array explode() returns to get each Product. 然后,您将需要遍历数组explode()返回以获取每个Product。

But if your products contains spaces in their names, using GROUP_CONCAT with space as delimiter and split with space as delimiter, you'll get too much results. 但是,如果您的产品名称中包含空格,则使用GROUP_CONCAT并将空格作为定界符,并使用空格作为定界符进行拆分,您将获得太多的结果。 I advise you tu use another delimiter that can't be use in the product name. 我建议您使用产品名称中不能使用的另一个分隔符。

You could use this instead: 您可以改用以下方法:

SELECT DISTINCT c.name cat_name
              , c.image
              , p.title product_name
           FROM products p   
           JOIN categories c 
             ON p.category_id = c.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM