Welcome Guest, Not a member yet? Register   Sign In
Setting active class and selecting specific row from DB

I'm new to CodeIgniter and I'm currently trying to build the first website with it.

I'm pulling my navi and content from a DB.

First, the navi (model):
function getNavi()
        $query = $this->db->get('content');
        if ($query->num_rows() == 0)
            echo "Datenbank leer...";    
        } else {
            return $query->result();
And the view:
<?php foreach($navi as $kategorien): ?>
            <li>&lt;?php echo $kategorien->name; ?&gt;</li>
    &lt;?php endforeach; ?&gt;
This displays all the navi-buttons I have. What I'd like to do is to set a "class="active""for whatever site currently is active.

Also, I'm trying to select only the content-row of the currently active site. I tried with an ?id=1 URI-parameter and then setting $_GET['id'], but this won't work. Any other suggestions?

Also, I searched the forums and Google and didn't happen to find a solution to my 2 little problems.

Codeigniter doesn't use the GET array. Your best to use the function
What this does is return the value of the segment number you entered, for example with the url

admin would be segment(1), posts segment(2) and so on. So after your controller and function names, put any variables for your page as url segments.

In terms of marking selected pages as the current page, javascript is is your best option. Give all of the links in your navigation bar an id that matches the page id you've allocated for every page.

Then in your controller for the page:

$data['page_id'] = 'page_id';

Then with jquery (or normal javascript) have a function similar to this:
$("a#&lt;?php echo page_id; "?&gt;").addClass('current');

This will add the class 'current' to the link thats currently being viewed, and from there you can use css to make it look however you want.

Thanks a lot. With segment numbers, it works perfectly.

I'm not satisfied with the javascript solution though. In CMS-systems it works dynamically without Javascript, so it has to in CodeIgniter aswell.

It is possible to do it on the server but it is tedious, and for something that is simply an aesthetic aid, it is worth the risk of users not having JS enabled IMHO, Having said that there are much better codeigniter users than me, so im sure somebody will have an efficient server side solution. glad i could help a bit.

Theme © iAndrew 2016 - Forum software by © MyBB