Welcome Guest, Not a member yet? Register   Sign In
Repeating, my own function, how?
#1

[eluser]Gewa[/eluser]
Hi, I am making a web-site , and I liked so much CI. Thank you guys for this excellent tool
Please advice such thing. In each controller, in constructor place I am putting such code



Code:
class Content extends Controller {



    function Content() {

        parent::Controller();
             $this->load->scaffolding('content');
          
       header("Content-Type: text/html; charset=UTF-8");
         $this->load->helper('date');



                          $lang= $this->config->item('language_abbr');

                          $query=$this->db->query("SELECT  * from `top_menu` ");

                            $data['top_menu']="";






                           if ($query->num_rows() > 0)
                     {
                          foreach ($query->result_array() as $row)
                        {



                             $data['top_menu'] .="<a href=\"/$lang".$row['link']."\" class=\"menusmall\">".$row[$lang]."</a>  |";
                            }
                         }







                     $datestring = "Year: %Y Month: %m Day: %d - %h:%i %a";
                       $time = time();
                       $data['zhamanak']=mdate($datestring, $time);
                     $this->load->view('catalog_header',$data);


                         $data['cat_menu']="";
                     $query=$this->db->query("SELECT  * from `cats` WHERE `parent`='lokale' ORDER BY `order`ASC");

                           if ($query->num_rows() > 0)
                     {
                          foreach ($query->result_array() as $row)
                        {

                                if($row['image']==NULL){
                                    $row['image']="ar2.gif";

                                   }


                             $data['cat_menu'] .="<td height=\"30\" width=\"14%\">
                             <img src=\"/images/".$row['image']."\" align=\"absmiddle\">&nbsp;&nbsp;&nbsp;
                                           </td>
                                  <td height=\"20\" width=\"86%\">
                  <div align=\"left\"><a href=\"/$lang/catalog/category/".$row['alias'] ."\"><span class=\"cat\">".$row[$lang]."</span></a><br>
                  </div>
                </td>  </tr><tr> ";
                            }
                         }


               $this->load->view('catalog_menu',$data);




    }

Which displays Header, Left Peace of web-site with left menu....
Can you tell me the way, how can I make header() function or something like $this->bla-bla->header() that can i put once, instead of this huge code each time?
Where to create this function, and where to put... thanks
#2

[eluser]Pascal Kriete[/eluser]
You would could create a library.

Code:
class Layout {

    var $_CI;
    
    /**
     * Constructor
     *
     * @access    public
     */
    function Layout()
    {
        $this->_CI =& get_instance();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Show Header
     *
     * @access    public
     */        
    function show_header()
    {        
        // Do anything you want in here

        // You will need to use $this->_CI instead of plain old $this
        
        $this->_CI->load->view('whatever');
    }
}

Name that file layout.php and plop it in your application/libraries folder.

To use, autoload 'layout' and in your constructors do:
Code:
$this->layout->show_header();

Obviously you can add functions as your heart desires. Here's some complimentary reading.

Hope that helps.
#3

[eluser]Gewa[/eluser]
thank you SOOOOOOOOOOOOOOOOOOOOOOOOO Much!




Theme © iAndrew 2016 - Forum software by © MyBB