Roel
Roel

Reputation: 1472

CodeIgniter retrieve specific data from the controller within a view

So basically what I am trying to do is retrieving a list of categories from the database and placing it in a variable called $data['categories'].

In the view I am listing this up by simply using a foreach() statement. So far so good. Though, some categories have subcategories (not all of them). Now, I added a row in the database saying 'is_direct' (has subcategories or not) which displays whether the category has subcategories or not.

Now, I want to list up those subcategories in case they exist.

Here the method to display the header (where the categories and subcategories will be listed) (Don't mind the private, it should be that).

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);
}

This method calls for the method get_categories(), that's the one below (simple Active Record).

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;
}

And below the view, you can see where the submenu would go.

<?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;

?>

Can somebody help me? Thank you.

Upvotes: 2

Views: 1778

Answers (1)

birderic
birderic

Reputation: 3765

The approach I would take would be to build out more complete data in your model (or controller) so that you can simply loop through it in the view (like you are currently doing). Below I have written a possible solution. I have modified your get_categories() method to query for subcategories as well. Then, in the view, we check to see if a category has any subcategories and outputs them as necessary.

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;
}

Then in the view:

<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>

Keep in mind I haven't tested this code but you should be able to adopt the general idea to your application.

Upvotes: 3

Related Questions