Welcome Guest, Not a member yet? Register   Sign In
[resolved]Function in controller

Hy everybody,

On my way to learn code igniter and the mvc coding, i have my really first problem.

I don't paste the model, but this one gets the datas of a website.
In that datas there are the tags, the keywords in fact. (example: http://leweb2.be/Mandellia.html)
These ones are separated by commas and i need to explode them via a function made for that.
I tried to define it in the controller but when i call that function in the view, i have that error:

Fatal error: Call to undefined function explode_term() in C:\wamp\www\eclipse\annuaire\system\application\views\fiche_view.php on line 56

Is that the moment when i need to create my own libraries?

Thanks in advance for your help,


class Fiche extends Controller{
        function fiche(){
            $this->load->helper('url', 'database');
        function index(){
            $this->load->view('fiche_view', $data);
        function explode_term($terms){
            $list = explode(",", $terms);
            echo count($list);

        foreach($query->result() as $row):
            echo '<h1>'.$row->app_name.'</h1>';
            echo '<p><span class="pink">Description :'.$row->app_short_description.'</span><br /><br />';
            echo '<span class="pink">Url :</span> <a href="'.$row->app_url.'" title="'.$row->app_short_description.'">'.$row->app_url.'</a><br /><br />';
            echo '<span class="pink">Description :</span><small>'.$row->app_description.'</small></p><br />';
            echo '<small><span class="pink">Mots-clé :</span><a href="" title="">'.explode_term($row->tags);'</a> |</small></div>';

This is not right way to do this.I'm also new to this.

What u should do is create function in view not in controller.

Controller functions create global URL link ..like ur function will create yoursitepath/index.php/explode_term so use these functions more precisely.

That what I thought.

But i don't think creating a function in the view is a nice way of working.
I continue searching, thanks for your reply.

Unless u use that function globally u can create function in view.Or use model approach for best results.

I wonder if should not user an helper...
I'm trying to do that, i'll let u know.

You can solve this 3 ways.

- create a helper class with that function and autoload it.
benefits : you can use the function without class reference
- create a library with that function an autoload it
benefits : you can use it anywhere in your code
- add the function to your model and when you return the data you can do a first visual data manipulation using the function

I prefer to use helpers for such things, you can load the helper on your controller and the function become available on your view.

You has also the plugins, by definition a plugin is a single function an a helper a set of functions.

Try this way and check the results.


Excellent, big thank for your reply.
I'm gonna make my first library so Smile

Hi Isabelle,

Try this in your view:

function show_row($row) {
  $list = explode(",", $row->tags);
  $cnt     = count($list);

  <h1>&lt;?= $row->app_name ?&gt;</h1>
      <span class="pink">
          Description :             &lt;?= $row->app_short_description    ?&gt;
    <span class="pink">
        Url :
    <a     href    =    "&lt;?= $row->app_url ?&gt;"
        title    =    "&lt;?= $row->app_short_description ?&gt;"
        &lt;?= $row->app_url ?&gt;
    <span class="pink">
        Description :
        &lt;?= $row->app_description    ?&gt;
        </small></p><br />
      <span class="pink">
        Mots-clé :
      <a href="" title="">
         &lt;?= $cnt ?&gt;
      </a> |
  </p>&lt;!-- </div> --&gt;
}// endfunc show_row($row)  

  foreach($query->result() as $row):
  echo '</div>';

thanks, sure it works, but i prefer working with helpers or library, that's a better way of coding.
Thanks anyway for your help.

Theme © iAndrew 2016 - Forum software by © MyBB