Search code examples
phpcodeignitercodeigniter-2

function inside a view in codeigniter


i'm a newbie in ci, can anyone help me or give some suggestions in my code and best practice? i have a foreach statement that require to use a function for displaying a huge data. here's my sample code of view. Thank you in advance

 foreach($data->result() as $d){

    if($d->condi){
       $sample =   myfunction($d->value1, $d->value2, $value->3);
    } 
    else{ 
         $d->otherdisplay;
            if($d->condition2){
            $sample =   myfunction($d->value1, $d->value2, $value->3);
            }
    }
}
function myfunction($a,$b,$c){
       /*do something;*/

}

Solution

  • it depends , anyway these kind of methods should be in controllers or in libraries, it depends if your method can be called from browser or not.

    If you want to use some method that you need to makesome routine action use a

    library or an helper
    

    If you want to use a web page method use that in controller

     Model = SQL and db stuffs
    
     Controller = all the php stuffs + data returned from Model
    
     View = html/js + data returned from Controller
    

    Check How to create Own libraries in CI