PHP/MySQL查询结果 [英] PHP/MySQL Query Results

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

问题描述

查询有问题..我的wscategories存储在线商店的主要类别,而我的wssubcategories表存储具有名为"parentcategory"的外键的表的子类别.我正在尝试列出主要类别,并将子类别放在la下方:

Having an issue with a query..my wscategories stores the main categories of an online store, and my wssubcategories table stores the sub categories of the table with the foreign key called "parentcategory". I'm trying to list the main categories, with the subcategories underneath a la:

顶部

T恤

女人

毛衣

裤子

短裤

牛仔裤

我写的查询/结果如下:

The query/result I wrote is the following:

$query = "SELECT DISTINCT a.categoryname AS maincategory, b.categoryname AS   
smallcategory FROM wscategories a, wssubcategories b WHERE a.SECTION = 'girls' AND  
b.parentcategory = a.id";
      $result = mysql_query($query) or die(mysql_error());
      while ($row = mysql_fetch_array($result))
      {

        echo $row['maincategory'];

        echo "<br/>";
        echo $row['smallcategory'];
        echo "<br/>";
      }

哪个返回:

上衣

T恤

上衣

毛衣

上衣

女人

等等.我希望脚本只显示一次主类别,然后将其下的子类别显示多次.有什么帮助吗?

Etc. I want the script to just display the main category once, and then the subcategories underneath vs. displaying it multiple times. Any help?

推荐答案

尝试类似的方法

// note we need to order by maincategory for this to work
//
$query = "SELECT DISTINCT a.categoryname AS maincategory, b.categoryname AS   
smallcategory FROM wscategories a, wssubcategories b WHERE a.SECTION = 'girls' AND  
b.parentcategory = a.id
ORDER BY maincategory ASC,
smallcategory ASC
";
      $result = mysql_query($query) or die(mysql_error());


      // keep track of previous maincategory
      $previous_maincategory = NULL;

      while ($row = mysql_fetch_assoc($result))
      {

        // if maincategory has changed from previouscategory then display it
        if ($previous_maincategory != $row['maincategory']) {
            echo $row['maincategory'];
        }

        echo "<br/>";
        echo $row['smallcategory'];
        echo "<br/>";

        // record what the previous category was
        $previous_maincategory = $row['maincategory'];
      }

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

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