I'm pretty new to CI so I'm trying to get my head around everything. At the moment, I'm converting an existing website that I coded procedural to CI (baring in mind I have no experience of OO PHP). I have my main navigation bar which is horizontal. Each main page has an individual class to put a background color on the tab to make the user see what page they are on. How it was originally coded was: home.php $currentclass = 1; about.php $currentclass = 2; PHP: So on, so forth nav.php <a href="home.php" class="<?php echo $currentclass1;?>">Home</a> <a href="about.php" class="<?php echo $currentclass2;?>">About</a> PHP: Now in Code Igniter, this throws up errors as each class is only defined on the page meaning that all other classes are an error other than the page I'm on. In my controller, the way I've found to do it is like so: Controller public function home(){ //Define the navigation classes $this->data['class1'] = '1'; $this->data['class2'] = ''; $this->data['class3'] = ''; $this->load->vars($this->data); $this->load->model('model_get'); $data['results'] = $this->model_get->getData('home'); $this->load->view('site_header'); $this->load->view('site_nav'); $this->load->view('content_home', $data); $this->load->view('site_footer'); } PHP: Nav.php <li><a href="/" title="Home" class="<?php echo $class1;?>">Home</a></li> <li><a href="/about/" title="About" class="<?php echo $class2;?>">About</a></li> PHP: This way does work, but it will mean defining all classes for all links in the navbar for every page. With everything set to 0 other than the active page. Is there a more efficient way of doing this? Thanks
SOLVED: controller: public function home(){ $this->data['pageName'] = 'home'; } public function about(){ $this->data['pageName'] = 'about'; PHP: nav: <li><a href="/" title="Home" class="<?php if ($pageName == 'home'){echo 'current';}?>">Home</a></li> <li><a href="/about/" title="About" class="<?php if ($pageName == 'about'){echo 'current';}?>">About</a></li> PHP: $this->data[''] = ''; will pass the variable named between [''] with the text between '' which I did not know.
Why not do it the simple way (what you call the procedural way) and include the navbar file (write it as a separate file, of course) in each page. Then $currentclass is defined, so you can echo it with no error. Using OOP or a library or classes isn't always the way to go. Sometimes simple is best.
It's because the navbar has individual classes for an active page and because I've coded the website into a template, the navbar is called out the same on each page so I needed to pass the page name through otherwise every page would be active.
Define each page's name on the page itself. $currentpage = 'landing'; Then in the navbar code (assuming it's PHP), use $currentpage to define whether to put the background color on the current tab (the one the code is addressing). This is almost a textbook example of when not to use a class, because procedural code to do what you want is almost trivial. Use a class when it simplifies the code. Stay away from classes when using a class complicates things. OOP makes a lot of things simpler, but for some simple things it's the wrong choice.
I don't understand your logic in what you are saying. Why would naming a page be bad practice in a class? The benefits of naming the page e.g. 'home' works on multiple levels, not just to define a style. For example, my naming structure is /site/home/ /site/about/ so having the keyword 'home' or 'about' is already there, why would I then write procedural code?
I didn't say it would be. I said that using a class for something as trivial as a nav bar is bad practice. To highlight the entry in the nav bar for the current page.
I have in my controller: public function home(){ $this->data['pageName'] = 'home'; $this->load->vars($this->data); $this->load->model('model_get'); $this->load->model('model_home'); //$data['results'] = $this->model_get->getData('home'); $this->load->view('site_header'); $this->load->view('site_nav'); $this->load->view('content_home'/*, $data*/); $this->load->view('site_footer'); } public function about(){ $this->data['pageName'] = 'about'; $this->load->vars($this->data); $this->load->model('model_get'); $this->load->view('site_header'); $this->load->view('site_nav'); $this->load->view('content_about'/*, $data*/); $this->load->view('site_footer'); } PHP: I use 'home' and 'about' etc to pass through to my model to pull the relevant data from the database. In my navigation I have: <div class="menu"> <ul> <li><a href="/site/" title="Home" class="<?php if ($pageName == 'home'){echo 'current';}?>">Home</a></li> <li><a href="/site/about/" title="About" class="<?php if ($pageName == 'about'){echo 'current';}?>">About</a></li> <li><a href="/site/categories/" title="All Categories" class="<?php if ($pageName == 'categories'){echo 'current';}?>">Categories</a></li> <li><a href="/site/publish/" title="Publish" class="<?php if ($pageName == 'publish'){echo 'current';}?>">Publish</a></li> <li><a href="/site/advertise/" title="Advertise" class="<?php if ($pageName == 'advertise'){echo 'current';}?>">Advertise</a></li> <li><a href="/site/contact/" title="Contact Free2Read" class="<?php if ($pageName == 'contact'){echo 'current';}?>">Contact</a></li> </ul> </div> PHP: I have to keep the page names as that is how I pull the data from the database, but you think it would be better practice to have on the individual pages something like: <?php $page = 1;?> PHP: And pass it through the navigation.... somehow (as the website is a template meaning the navigation is called out before the page) using the exact same code above but wasting memory by redeclaring a variable I already have and use? As I say, I'm a beginner and enjoy a good debate.