分组列出结果

发布于 2024-11-19 12:15:00 字数 525 浏览 0 评论 0原文

我在 php 中对 mysql 结果进行“排序”时遇到问题。

这是我拥有的数据库

id¦ value  ¦ category
---------------------------
1 ¦ Value1 ¦ category1
2 ¦ Value2 ¦ category2
3 ¦ Value3 ¦ category2
4 ¦ Value4 ¦ category3
5 ¦ Value5 ¦ category1
6 ¦ Value6 ¦ category1


我想做的是在php中获得如下所示的结果:

Value1

  • Value5
  • Value6category2
  • Value2

Value3category3

  • Value4
  • category1


任何

帮助都是有用的 谢谢

I'm having trouble 'sorting' my mysql results in php.

Here's the database I have

id¦ value  ¦ category
---------------------------
1 ¦ Value1 ¦ category1
2 ¦ Value2 ¦ category2
3 ¦ Value3 ¦ category2
4 ¦ Value4 ¦ category3
5 ¦ Value5 ¦ category1
6 ¦ Value6 ¦ category1

What I'm trying to do, is to get a result in php looking like this:

category1

  • Value1
  • Value5
  • Value6

category2

  • Value2
  • Value3

category3

  • Value4

Any help is useful. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抚你发端 2024-11-26 12:15:00
SELECT id, value, category FROM table;

然后在PHP中

while($row = mysql_fetch_assoc($result)) {
    $myArray[$row['category']][] = $row['value'];
}
SELECT id, value, category FROM table;

Then in PHP

while($row = mysql_fetch_assoc($result)) {
    $myArray[$row['category']][] = $row['value'];
}
掀纱窥君容 2024-11-26 12:15:00

SQL:

$SQL = 'SELECT * FROM theTable SORT BY category ASC';

PHP:

$results = mysql_fetch....($SQL);
$record_table = array();

// Create the array for the list
foreach($results as $row)
{
   $record_table[$row['category']][] = $row['value'];
}

// Output the HTML list
foreach($record_table as $key => $val)
{
    echo '<ul id="' . $key . '">';

    foreach($val as $v)
    {
        echo '<li>' . $v . '</li>';
    }

    echo '</ul>';
}

SQL:

$SQL = 'SELECT * FROM theTable SORT BY category ASC';

PHP:

$results = mysql_fetch....($SQL);
$record_table = array();

// Create the array for the list
foreach($results as $row)
{
   $record_table[$row['category']][] = $row['value'];
}

// Output the HTML list
foreach($record_table as $key => $val)
{
    echo '<ul id="' . $key . '">';

    foreach($val as $v)
    {
        echo '<li>' . $v . '</li>';
    }

    echo '</ul>';
}
沉溺在你眼里的海 2024-11-26 12:15:00

循环遍历结果集,在初始数组中为遇到的每个新类别创建数组键,并将值数据推送到其中。

如果类别的键存在,则跳过键创建阶段并仅添加数据。

然后,您可以循环生成的数组并输出每个类别(键)的所有数据(值数组)。

Loop through the result-set, create array key within your initial array for each new category you encounter and push value data into it.

If key for category exists, skip the key creation stage and just add the data.

Then you can loop through generated array and output all the data (array of values) per category (key).

小姐丶请自重 2024-11-26 12:15:00

使用 GROUP_CONCAT 你会获取输出将以 , 分隔的字符串形式

      SELECT GROUP_CONCAT(value) AS valueList FROM tableName GROUP BY category

您还可以使用 GROUP_CONCAT() 的某种格式。像

SELECT GROUP_CONCAT( value SEPARATOR ‘-’ )

上面的查询将使用 - 而不是 ,

更改顺序和缩写

SELECT GROUP_CONCAT( value ORDER BY Category DESC )

,然后通过脚本语言重新排列,如果使用 php,则尝试使用 explode()

注意: GROUP_CONCAT () 忽略 NULL价值观。

use GROUP_CONCAT and you will get the output will be in string separated by ,

      SELECT GROUP_CONCAT(value) AS valueList FROM tableName GROUP BY category

You can also use some format of GROUP_CONCAT(). Like

SELECT GROUP_CONCAT( value SEPARATOR ‘-’ )

above query will use - instead of ,

To change the order and shorting

SELECT GROUP_CONCAT( value ORDER BY Category DESC )

and then re arrange by ur scripting language, if using php then try with explode()

Note: GROUP_CONCAT() ignores NULL values.

離人涙 2024-11-26 12:15:00

感谢大家的帮助。我将混合使用 AlienWebguy 和 jchavannes 的代码,该代码工作完美,正是我正在寻找的。

代码如下所示:

SQL:

$SQL = "SELECT * FROM table_name";
$results = mysql_query($SQL);

PHP:

while($row = mysql_fetch_assoc($results)) {
    $myArray[$row['category']][] = $row['value'];   
}

foreach($myArray as $key => $val)
{
    echo $key;
    echo '<ul>';

    foreach($val as $v)
    {
        echo '<li>' . $v . '</li>';
    }

    echo '</ul>';
}

thanks everyone for the help. I'm going to use a mix of AlienWebguy's and jchavannes's code which is working perfect, just what i was searching for.

the code looks like this:

SQL:

$SQL = "SELECT * FROM table_name";
$results = mysql_query($SQL);

PHP:

while($row = mysql_fetch_assoc($results)) {
    $myArray[$row['category']][] = $row['value'];   
}

foreach($myArray as $key => $val)
{
    echo $key;
    echo '<ul>';

    foreach($val as $v)
    {
        echo '<li>' . $v . '</li>';
    }

    echo '</ul>';
}
鱼忆七猫命九 2024-11-26 12:15:00

这是一种仅用一个循环即可完成的解决方案。

$currentCategory = null;
$closeTag = "";

//Using MySQLi since mysql_* functions are obsoletes
$mysqli = new mysqli($host, $username, $password, $dbname);

/**
 * Here I use an ORDER BY to group the result by categories
 */
$selectStmt = $mysqli->prepare("SELECT id, value, category FROM table_name ORDER BY category ASC");
$selectStmt->execute();        
$selectStmt->bind_result($id, $value, $category);
while($selectStmt->fetch()) {
    if ($currentCategory != $category) {
        /**
         * In the first iteration this variable will be empty, so the <ul> tag 
         * will not be closed
         */
        echo $closeTag; //Close the last category list
        echo $category;
        echo '<ul>';            
        $closeTag = '</ul>';
        $currentCategory = $category;
    }
    echo '<li>' . $value . '</li>';
}
echo '</ul>';

Here is a solution done with just one loop.

$currentCategory = null;
$closeTag = "";

//Using MySQLi since mysql_* functions are obsoletes
$mysqli = new mysqli($host, $username, $password, $dbname);

/**
 * Here I use an ORDER BY to group the result by categories
 */
$selectStmt = $mysqli->prepare("SELECT id, value, category FROM table_name ORDER BY category ASC");
$selectStmt->execute();        
$selectStmt->bind_result($id, $value, $category);
while($selectStmt->fetch()) {
    if ($currentCategory != $category) {
        /**
         * In the first iteration this variable will be empty, so the <ul> tag 
         * will not be closed
         */
        echo $closeTag; //Close the last category list
        echo $category;
        echo '<ul>';            
        $closeTag = '</ul>';
        $currentCategory = $category;
    }
    echo '<li>' . $value . '</li>';
}
echo '</ul>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文