CodeIgniter Forums
Setting active class and selecting specific row from DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Setting active class and selecting specific row from DB (/thread-27292.html)



Setting active class and selecting specific row from DB - El Forum - 02-06-2010

[eluser]Unknown[/eluser]
Hello,
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):
Code:
function getNavi()
    {
        $query = $this->db->get('content');
        
        if ($query->num_rows() == 0)
        {
            echo "Datenbank leer...";    
        } else {
            return $query->result();
        }
    }
And the view:
Code:
<?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.


Setting active class and selecting specific row from DB - El Forum - 02-06-2010

[eluser]richzilla[/eluser]
Codeigniter doesn't use the GET array. Your best to use the function
Code:
$this->uri->segment(segment_number)
What this does is return the value of the segment number you entered, for example with the url
Code:
www.mysite.com/index.php/admin/posts/view/123

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:

Code:
$data['page_id'] = 'page_id';
$this->load->view('my_view',$data);

Then with jquery (or normal javascript) have a function similar to this:
Code:
$("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.


Setting active class and selecting specific row from DB - El Forum - 02-06-2010

[eluser]Unknown[/eluser]
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.


Setting active class and selecting specific row from DB - El Forum - 02-06-2010

[eluser]richzilla[/eluser]
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.