PHP MySQL显示按公共字段分组的多行 [英] PHP MySQL display multiple rows grouped by common fields

查看:208
本文介绍了PHP MySQL显示按公共字段分组的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何列出COMPANY的CATEGORY和BRAND,布局看起来类似于这个:

公司1




  • 类别1:

    品牌X

    品牌Y

    品牌Z


  • 类别2

    品牌A

    品牌B

    品牌C


  • 类别3

    品牌A

    品牌X




我不熟悉足够用PHP + MySQL来查找正确的SEARCH和PHP输出以实现这一目的。



我的表看起来与此类似:

 
公司| CATEGORY |品牌
--------------------------------
公司1 |类别2 |品牌A
公司1 |类别2 |品牌B
公司1 |类别2 |品牌C
公司1 |类别1 |品牌X
公司1 |类别1 |品牌Y
公司1 |类别1 |品牌Z
公司1 |类别3 |品牌A
公司1 |类别3 |品牌X


解决方案

 <?php 
$ result = mysql_query(
SELECT
*
FROM
some_table
ORDER BY
公司,
类别,
品牌

或trigger_error('Query failed in'.__FILE__。
'on line'。__LINE__。'。'。mysql_error(),E_USER_ERROR);
if(mysql_num_rows($ result)){
$ companies = array();
while($ row = mysql_fetch_assoc($ result)){
$ companies [$ row ['company']] [$ row ['category']] [] = $ row ['brand'] ;
}

foreach($ company $ company => $ categories){
echo'< h2>'。 htmlentities($ company,ENT_COMPAT,'UTF-8')。'< / h2>';
echo'< ul>';
foreach($ categories AS $ category => $ brands){
echo'< li>'。 htmlentities($ category,ENT_COMPAT,'UTF-8');
foreach($品牌AS $品牌){
echo'< br>< em>'。 htmlentities($ brand,ENT_COMPAT,'UTF-8')。'< / em>';
}
echo'< br>& nbsp;< / li>';
}
echo'< / ul>';
}
}

jsbin


I'm trying to figure out how to list COMPANY 's CATEGORY 's and BRAND 's, where the layout would look similar to this:

COMPANY 1

  • CATEGORY 1:
    BRAND X
    BRAND Y
    BRAND Z

  • CATEGORY 2
    BRAND A
    BRAND B
    BRAND C

  • CATEGORY 3
    BRAND A
    BRAND X

I'm not familiar enough with PHP + MySQL to find the right SEARCH and PHP output in order to achieve this.

My table looks similar to this:

COMPANY   | CATEGORY   | BRAND    
--------------------------------
Company 1 | Category 2 | Brand A  
Company 1 | Category 2 | Brand B  
Company 1 | Category 2 | Brand C  
Company 1 | Category 1 | Brand X  
Company 1 | Category 1 | Brand Y  
Company 1 | Category 1 | Brand Z  
Company 1 | Category 3 | Brand A  
Company 1 | Category 3 | Brand X  

解决方案

<?php
$result = mysql_query("
  SELECT
    *
  FROM
    some_table
  ORDER BY
    company,
    category,
    brand
") 
or trigger_error('Query failed in '. __FILE__ .
   ' on line '. __LINE__ .'. '. mysql_error(), E_USER_ERROR);
if (mysql_num_rows($result)) {
  $companies = array();
  while ($row = mysql_fetch_assoc($result)) {
    $companies[$row['company']][$row['category']][] = $row['brand'];
  }

  foreach ($companies AS $company => $categories) {
    echo '<h2>'. htmlentities($company, ENT_COMPAT, 'UTF-8') .'</h2>';
    echo '<ul>';
    foreach ($categories AS $category => $brands) {
      echo '<li>'. htmlentities($category, ENT_COMPAT, 'UTF-8');
      foreach ($brands AS $brand) {
        echo '<br><em>'. htmlentities($brand, ENT_COMPAT, 'UTF-8') .'</em>';
      }
      echo '<br>&nbsp;</li>';
    }
    echo '</ul>';
  }
}

jsbin

这篇关于PHP MySQL显示按公共字段分组的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆