按类别排序php

时间:2013-05-29 08:44:55

标签: php html categories

我遇到了一个问题,我已经困扰了几天...... 我似乎无法按类别进行排序,而不会阻止sub_category多次显示或在页面下方回显其他名称(不属于该类别)。

请帮帮我。

我找到了包含指定user_id的所有项目,从我的表格列表中列出了所有类别,然后是所有类别..我不知道如何将所有表格连接在一起,所以我排序另一种方法 - 不允许类别和子类别重复,但我认为这会导致错误..

这是我的代码:

  $user_id = $_GET['user_id'];



$res = mysql_query("SELECT DISTINCT item_id,category,sub_category FROM list_of_items WHERE user_id = '$user_id' ORDER BY category, sub_category");
while($row = mysql_fetch_array($res))
{
    $item_id = $row['item_id'];
    $category_id = $row['category'];
    $sub_category_id = $row['sub_category'];

        $res_category = mysql_query("SELECT name FROM categories WHERE id = '$category_id'");
        $row_category = mysql_fetch_array($res_category);
        $res_sub_category = mysql_query("SELECT name FROM sub_categories WHERE id = '$sub_category_id'");
        $row_sub_category = mysql_fetch_array($res_sub_category);

        $category = $row_category['name'];
        $sub_category = $row_sub_category['name'];


            $num_res = mysql_query("SELECT item_id FROM list_of_items WHERE item_id = '$item_id' AND user_id = '$user_id'");
            $num_row = mysql_num_rows($num_res);

            $res_find_items = mysql_query("SELECT * FROM all_the_stuff WHERE id = '$item_id'");     
            while($row_find_items = mysql_fetch_array($res_find_items))
            {

                if($category != $current_cat) {

                    $current_cat = $category;
                    echo"<b>$current_cat</b> <br />";

                }

                    if ($sub_category != $current_sub_cat) {

                        $current_sub_cat = $sub_category;
                            echo "<i>$current_sub_cat</i><br />";


                        $name = $row_find_items['name'];
                        $price = $row_find_items['price'];
                        $shop = $row_find_items['shop'];

                        $price = $price * $num_row;

                        echo''.$num_row.'x
                            '.$name.'
                        <div style="float:right">$'.$price.'</div>
                        <hr />
                        ';

                    }
                    else {

                        $name = $row_find_items['name'];
                        $price = $row_find_items['price'];
                        $shop = $row_find_items['shop'];

                        $price = $price * $num_row;

                        echo''.$num_row.'x
                            '.$name.'
                        <div style="float:right">$'.$price.'</div><hr />
                        ';

                    }
                }

}

     ?>

主要的是当我打印


时,无论该子类别中是否有更多项目,它都会创建一条线... :(它与if($ sub_category!=)有关$ current_sub_cat),因为当它打印出下一个项目时,$ sub_category == $ current_sub_cat,这意味着它转到了else语句.... :((请帮我解决这个问题,这真的很烦人

编辑(示例行): list_of_items:

        id      item_id        user_id           category           sub_category
         1          3             1                  1                      1
         2          2             1                  1                      3
         3          1             1                  1                      3
         4          2             1                  2                      2
         5          3             1                  1                      1  

类:

        id        name
        1          Food
        2          Furniture

- 行业标准:

       id         name
        1         Beverage
        2         Chair
        3         Bread
        4         Table

0 个答案:

没有答案