CodeIgniter从视图中的控制器检索特定数据


CodeIgniter retrieve specific data from the controller within a view

所以基本上我要做的是从数据库中检索一个类别列表,并将其放入一个名为$data的变量中["类别"]。

在视图中,我通过简单地使用foreach()语句将其列出。到目前为止还不错。不过,有些类别有子类别(并非全部)。现在,我在数据库中添加了一行,上面写着"is_direct"(是否有子类别),它显示类别是否有子分类。

现在,我想列出这些子类别,以防它们存在。

这里是显示标题的方法(其中列出了类别和子类别)(不要介意私有的,应该是私有的)。

private function show_header($template='default',$page_name='default')
{
    // Get the categories.
    $data['categories']     = $this->CI->ws_category->get_categories();
    // Display the header.
    $this->CI->load->view("templates/".$template."/template/header",$data);
}

此方法调用方法get_categories(),即下面的方法(简单的活动记录)。

public function get_categories()
{
    // Prepare the SQL query.
    $this->db->select('*');
    $this->db->from(APP_PREFIX.'categories');
    $this->db->order_by('name_clean');
    // Execute the query.
    $query = $this->db->get();
    // Get the results from the query.
    $result = $query->result_array();
    // Return the array with data.
    return $result;
}

在视图下方,您可以看到子菜单的位置。

<?php
    // Loop through all the categories.
    foreach ($categories as $item):
?>
<li class='nav_link <?php ($item['is_direct'] == '1' ? 'nav_subcats' : ''); ?>'>
    <a href='<?php echo base_url().'category/'.$item['category_id'].'-'.$item['name_clean'].'.html';?>' class='nav_main_link'><?php echo $item['name']; ?></a>
    <?php
        // In case subcategories are present.
        if ($item['is_direct'] != '1')
        {
    ?>
    <div class='nav_submenu'>
        <ul>
            <li><a href='#' class='nav_sub_link'>submenu item</a></li>
        </ul>
    </div>
    <?php
        }
    ?>
</li>
<?php
    // End the loop.
    endforeach;
?>

有人能帮我吗?非常感谢。

我会采取的方法是在模型(或控制器)中构建更完整的数据,这样您就可以在视图中简单地循环使用它(就像您目前正在做的那样)。下面我写了一个可能的解决方案。我已经修改了您的get_categories()方法来查询子类别。然后,在视图中,我们检查一个类别是否有任何子类别,并根据需要输出它们。

public function get_categories_and_subcategories()
{
    // Prepare the SQL query.
    $this->db->select('*');
    $this->db->from(APP_PREFIX.'categories');
    $this->db->order_by('name_clean');
    // Execute the query.
    $query = $this->db->get();
    // Get the results from the query.
    $result = $query->result_array();
    // Get subcategories
    for ($i = 0; $i < count($result); $i++) {
        // Check if subcategory
        if ($result[$i]['is_direct']) {
            // Query for subcategories - made up for demonstrational purposes
            $query = $this->db->query("SELECT * FROM subcategories WHERE parent_id='".$result[$i]['id']."'");
            // Add subcategories to category item
            $result[$i]['subcategories'] = $query->result_array();
        } else {
            $result[$i]['subcategories'] = array();
        }
    }
    // Return the array with data.
    return $result;
}

然后在视图中:

<li class='nav_link <?php ($item['is_direct'] == '1' ? 'nav_subcats' : ''); ?>'>
    <a href='<?php echo base_url().'category/'.$item['category_id'].' '.$item['name_clean'].'.html';?>' class='nav_main_link'><?php echo $item['name']; ?></a>
    <?php if (count($item['subcategories']) > 0): ?>
    <div class='nav_submenu'>
        <ul>
            <foreach ($item['subcategories'] as $subcategory): ?>
            <li><a href='<?php echo $subcategory['url']; ?>' class='nav_sub_link'><?php echo $subcategory['name']; ?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <?php endif; ?>
</li>

请记住,我还没有测试过这段代码,但您应该能够在应用程序中采用总体思想。