mysql_fetch_array - 分组结果 [英] mysql_fetch_array - group results

查看:33
本文介绍了mysql_fetch_array - 分组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内容管理模型:

public static function getSiteLinks($page)
{
    $query="SELECT * FROM cms_links WHERE page = '{$page}'";

    return mysql_query($query);
}

cms_links 表有以下内容:id, page, section_sub, title, link

在我看来,我有:

$links=CMS::getSiteLinks('home-page');

while($link = mysql_fetch_array($links))
{
    echo $link['section_sub'];
    echo $link['title'];
}

section_sub 字段定义链接所属的组".我想输出上述查询的结果,按 section_sub 分组.例如,在每组之后,插入一个换行符,然后下一组开始.

The field section_sub defines what 'group' the link belongs to. I want to output the results from the above query, grouped by section_sub. So for example after each group, a line break is inserted and the next group begins.

推荐答案

试试这个:

按section_sub排列结果:

Arrange the result by section_sub:

$query="SELECT * FROM cms_links WHERE page = '{$page}' ORDER BY section_sub";
...
$prevsub = '';
while($link = mysql_fetch_array($links))
{
    echo $link['section_sub'];
    echo $link['title'];

    if($prevsub != '' && $prevsub != $link['section_sub'])
        echo '<br/>';
    $prevsub =  $link['section_sub'];
}

这篇关于mysql_fetch_array - 分组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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