我正在尝试使用 codeigniter 创建一个网站。我在数据库中有两个表。这些表是 Category(Category Name, Category_Id) 和 Item(Item_Name, Category_id)。现在,在我看来,我想在不同类别的不同表格中显示项目。
到目前为止,我已经想出了这个,但显然它不起作用。模型中的代码是这样的。
$query = 'select category_id as id, category_name as name from category;';
    $result = $this->db->query($query);
    $data['category'] = $result->result_array();
    foreach($data['category'] as $d)
    {
        $query = "select item_name as name from item where item_catagory_id = '{$d['id']}';";
        $result = $this->db->query($query);
        $data["{$d['id']}"] = $result->result_array();
    }
    $this->load->view('view', $data);
视图中的代码是这样的。
<?php foreach($category as $c):?>
            <h2><?php echo $c['name']; ?></h2>
            <table>
                <tr>
                    <td>Item Name</td>
                </tr>
                <?php foreach($c['id'] as $d): ?>
                <tr>
                    <td><?php echo $d['name'];?></td>
                </tr>
                <?php endforeach;?>
            </table>
        <?php endforeach;?>
但它说“为 foreach() 提供的参数无效”。它还说错误在这一行:
<?php foreach($c['id'] as $d): ?>