CodeIgniter Forums

Full Version: [resolved]Function in controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]isabelle[/eluser]
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:

Code:
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,

CONTROLLER

Code:
class Fiche extends Controller{
        function fiche(){
            parent::Controller();
            $this->load->helper('url', 'database');
        }
        function index(){
            $id_website=$this->uri->segment(3);
            
            $this->load->model('fiche_model');
            $data['query']=$this->fiche_model->get_fiche($id_website);
            
            $this->load->view('fiche_view', $data);
        }
        
        function explode_term($terms){
            $list = explode(",", $terms);
            echo count($list);
        }    
    }

VIEW
Code:
<?php
        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>';
        endforeach;
    ?&gt;

El Forum

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

El Forum

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

El Forum

[eluser]Yash[/eluser]
Unless u use that function globally u can create function in view.Or use model approach for best results.

El Forum

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

El Forum

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

El Forum

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

Regards

El Forum

[eluser]isabelle[/eluser]
Excellent, big thank for your reply.
I'm gonna make my first library so Smile

El Forum

[eluser]John_Betong[/eluser]
Hi Isabelle,

Try this in your view:

Code:
function show_row($row) {
  $list = explode(",", $row->tags);
  $cnt     = count($list);
?&gt;

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

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

El Forum

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