Welcome Guest, Not a member yet? Register   Sign In
How to create a function from this one line code?
#1

[eluser]term25[/eluser]
I have a menu in my view:

!!! THE CODE BELLOW IS MESSED UP !!!
The if conditions are deleted even when used in code.

On stackoverflow it is displayed correctly:
http://stackoverflow.com/questions/11040...-line-code

Code:
<ul>
    
    <li>
         <a href="&lt;?php echo base_url(); ?&gt;news"class="active-nav-item">News about Cars</a>
    </li>
    
    <li>
  <a href="&lt;?php echo base_url(); ?&gt;products"class="active-nav-item">Latest Products</a>
    </li>
    
    <li>
  <a href="&lt;?php echo base_url(); ?&gt;media"class="active-nav-item">Audio/Video</a>
    </li>

</ul>

And I am sending to this view a variable $nav_item from my controller e.g. news (I have controllers for every item in the navigation so: news, products and media controller) like this:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {

public function index()
{
  $data['nav_item'] = 'news'; // variable for navigation
  $data['main_content'] = 'pages/news';
  $this->load->view('template', $data);
}
}

So, it is woking good, but the code in a link is really long:
Code:
<a href="&lt;?php echo base_url(); ?&gt;news"class="active-nav-item">News</a>

I would like to make a function from this where I send only two parameters and it will do other things automatic. E.g. it could look like this:
<a href="&lt;?php echo base_url(); ?&gt;news"class="active-nav-item">News about Cars</a>

So, if $navitem == 'news' it echoes "class='active-nav-item'"

How to do this function so I can put it in my e.g. myown_helper.php in helpers folder.

Thanks in advance.
#2

[eluser]nagata[/eluser]
teh function
Code:
function is_active_item($item,$this) {
if ($this==$item) {
return 'class="active-nav-item"';
}
else {
return 'class="nav-item"';
}
}
use this in nav part:

Code:
<a >myown->is_active_item('news',$navitem);
?&gt;>News</a>
^ Forum fucks this part of code up for some unknown reason
anyways simply use the function like $this->myown->is_active_item('news',$navitem);
#3

[eluser]term25[/eluser]
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB