Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a function for my helper from this one line code?
    text
    copied!<p>I have a menu in my view:</p> <pre><code>&lt;ul&gt; &lt;li&gt; &lt;a href="&lt;?php echo base_url(); ?&gt;news" &lt;?php if($nav_item == 'news'): ?&gt;class="active-nav-item"&lt;?php endif; ?&gt;&gt;News about Cars&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="&lt;?php echo base_url(); ?&gt;products" &lt;?php if($nav_item == 'products'): ?&gt;class="active-nav-item"&lt;?php endif; ?&gt;&gt;Latest Products&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a href="&lt;?php echo base_url(); ?&gt;media" &lt;?php if($nav_item == 'media'): ?&gt;class="active-nav-item"&lt;?php endif; ?&gt;&gt;Audio/Video&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>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:</p> <p> <pre><code>class News extends CI_Controller { public function index() { $data['nav_item'] = 'news'; // variable for navigation $data['main_content'] = 'pages/news'; $this-&gt;load-&gt;view('template', $data); } } </code></pre> <p>So, it is working fine, but the code in a link is really long:</p> <pre><code>&lt;a href="&lt;?php echo base_url(); ?&gt;news" &lt;?php if($nav_item == 'news'): ?&gt;class="active-nav-item"&lt;?php endif; ?&gt;&gt;News about Cars&lt;/a&gt; </code></pre> <p>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:</p> <pre><code>&lt;a href="&lt;?php echo base_url(); ?&gt;news" &lt;?php activeitem($navitem,'news'); ?&gt;&gt;News about Cars&lt;/a&gt; </code></pre> <p>So, <code>if $navitem == 'news'</code> it echoes <code>"class='active-nav-item'"</code></p> <p>How to do this function so I can put it in my e.g. myown_helper.php in helpers folder.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload