views:

123

answers:

1

Hello,

I am building a system in which the user can build thre own navigation menu from a selection of catergories, the way it works is that is they click on 'Blog' they they can get an accordion titled 'Blog' and on expanding this they see all the blog titles, however as the moment, my application creates multiple accordions if there are multiple blog entries, so at the moment i have two blog entries and when the user slects 'Blog' they get to accordions, where as they should get one with all the titles in

Controller:

public function category($content_id) {
    //$this->output->enable_profiler(TRUE);
    if (intval($this->uri->segments[4])){
        $content_id = $this->uri->segments[4];
    } else {
        $content_id = $this->uri->segments[7];
    }
    $data['content'] = $this->site_model->get_content($content_id);
    $this->load->view("call", $data);
}

Model:

    public function get_content($content_id) {
    $this->db->select('*');
    $this->db->from ('content');
    $this->db->join('categories', 'categories.category_id = content.categories_category_id', 'left');
    $this->db->where ('categories_category_id', $content_id);

    $query = $this->db->get();
    return $query->result_array();

}

View:

    <?php
if(isset($content)) {
//  var_dump($content);
    foreach($content as $row) {
        echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>";
        echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
    }
}
?>

How can I alter my code so for category the user selects only one accordion gets built put all the categories realted content is placed in that accordion?

Hope this makes sense.

A: 

If I understand correctly you should move the creation of the header out of your loop:

<?php
if(isset($content)) {
    echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>";
    foreach($content as $row) {
        echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
    }
}
?>

If you don't know the category name you could try something like this:

<?php
$first = true;
if(isset($content)) {
    echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>";
    foreach($content as $row) {
        if ($first) {
            echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>";
            $first = false;
        }
        echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>";
    }
}
?>
Jimmy Shelter