从具有相同ID的单独表中选择数据

时间:2017-05-17 08:53:10

标签: php mysqli

我有2个表: mb_category mb_items ,我正在尝试从mb_items中选择与mb_category中分配的类别相同的ID。< / p>

mb_category 有2行:id和category mb_items 有4行:id,item_name,item_price和mb_category_id

我想要做的是选择mb_items中的每个项目及其对应的类别ID,并在正确的下拉列表中回显它们。

这是我目前所拥有的:

<ul class="nav">
    <?php
        $category = $db->read('mb_category', '*', '', 'ORDER BY id ASC');

    //  echo $category["count"] . "<br />";
    //  print_r($category);
                foreach ($category["results"] as $row) {
    ?>
    <li class="button-dropdown">
        <a href="javascript:void(0)" class="dropdown-toggle">
            <?php echo $row->category; ?> <span>▼</span>
                </a>
                    <ul class="dropdown-menu">
                        <?php
                            $cid = $db->read('mb_category', 'id', '', '');

                            $item = $db->read('mb_items', '*', 'mb_category_id="$cid"', '');

//                          var_dump($item);
//                          echo $item["count"] . "<br />";
//                          print_r($item);

                            foreach ($item["results"] as $irow) {
                                ?>
                                <li>
                                    <a href="#">
                                        <?php
                                        echo $irow->item_name;
                                        ?>
                                    </a>
                                </li>
                                <?php
                            }
                            ?>
                        </ul>
                    </li>
                    <?php
                }
                ?>

这是我的SELECT:

public function read($table_name, $cols, $where = NULL, $limit = NULL) {
        //GET $cols FROM $table_name WHERE $where returns 0 for false and fetch_object() for true
        $query = "SELECT " . $cols . " FROM " . $table_name;

        if ($where) {
            $query .= " WHERE " . $where;
        }

        if ($limit) {
            $query .= " " . $limit;
        }

        //echo $query;

        $sql = $this->query($query);

        if ($sql) {
            $data = array();
            $data['count'] = $sql->num_rows;
            while ($row = $sql->fetch_object()) {
                $data['results'][] = $row;
            }
            return $data;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我可以建议你做两件事(我没有测试我给你的代码,只是为了向你展示这个想法)。

1)你做的请求太多了,你只需要一个:

SELECT c.id AS category_id, c.category AS category_name, i.id AS item_id, i.item_name
FROM mb_category c
LEFT JOIN mb_items i ON c.id = i.mb_category_id

2)格式化您的数据并使用它来显示您的列表:

<?php
// format data
$categories = [];
foreach ($category["results"] as $row) {
    if (empty($categories[$row[category_id]])) {
        $categories[$row[category_id]] = [
            'id'    => $row[category_id],
            'name'  => $row['category_name'],
            'items' => [],
        ];
    }

    if (!empty($categories[$row[item_id]])) {
        $categories[$row[category_id]]['items'][] = [
            'id'   => $row['item_id'],
            'name' => $row['item_name'],
        ];
    }
}
?>

<ul class="nav">
    <?php
    // display data
    foreach ($categories as $categoryId => $category) {
    ?>
        <li class="button-dropdown">
            <a href="javascript:void(0)" class="dropdown-toggle">
                <?php echo $category['name']; ?> <span>▼</span>
            </a>
            <ul class="dropdown-menu">
                <?php foreach ($category['items'] as $item) { ?>
                    <li>
                        <a href="#">
                            <?php
                            echo $item['name'];
                            ?>
                        </a>
                    </li>
                <?php } ?>
            </ul>
        </li>
    <?php } ?>
</ul>