Welcome Guest, Not a member yet? Register   Sign In
how to make it little more clean
#1

Hey;- >

on each controller function, am loading the SQL data, as you can see below in a code box

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Web extends CI_Controller {



    /**

     * Index Page for this controller.

     *

     * Maps to the following URL

     *         http://example.com/index.php/welcome

     *    - or -  

     *         http://example.com/index.php/welcome/index

     *    - or -

     * Since this controller is set as the default controller in

     * config/routes.php, it's displayed at http://example.com/

     *

     * So any other public methods not prefixed with an underscore will

     * map to /index.php/welcome/<method_name>

     * @see http://codeigniter.com/user_guide/general/urls.html

     */

    public function index()

    {

    $this->home();

    }

    

    public function home()

    {

         $method_name = "home";

        // Model & Controller //

        $this->load->model('model_get');

        $data['content'] = $this->model_get->GetRecords();

        $data['sub_menu'] = $this->model_get->GetChild();

        $data['meta'] = $this->model_get->GetSeo($method_name);

        $this->load->view('main/menu',$data);

        $this->load->view('main/header');

        $this->load->view('main/slider');

        $this->load->view('main/features');

        $this->load->view('main/calltoaction');

        $this->load->view('main/pricingtable');

        $this->load->view('main/statistics');

        

        $this->load->view('main/footer');

    }

    public function hosting()

    {

        $method_name = $this->router->fetch_method();

        // Model & Controller //

        $this->load->model('model_get');

        $data['content'] = $this->model_get->GetRecords();

        $data['sub_menu'] = $this->model_get->GetChild();

        $data['meta'] = $this->model_get->GetSeo($method_name);
        
        

        $this->load->view('main/header');

        $this->load->view('main/menu',$data);

        $this->load->view('main/breadcrumbs');

        $this->load->view('hosting/pricingtable');

        $this->load->view('hosting/tabs');

        $this->load->view('main/footer');

    }

    public function contact()

    {

        $method_name = $this->router->fetch_method();

        // Model & Controller //

        $this->load->model('model_get');

    [b]    $data['content'] = $this->model_get->GetRecords();

        $data['sub_menu'] = $this->model_get->GetChild();

        $data['meta'] = $this->model_get->GetSeo($method_name);[/b]

        $this->load->view('main/header');        

        $this->load->view('main/menu',$data);

        $this->load->view('main/breadcrumbs');

        $this->load->view('contact/contact');

        $this->load->view('main/footer');

    }

        public function domain()

    {

        $method_name = $this->router->fetch_method();

        // Model & Controller //

        $this->load->model('model_get');

        $data['content'] = $this->model_get->GetRecords();

        $data['sub_menu'] = $this->model_get->GetChild();

        $data['meta'] = $this->model_get->GetSeo($method_name);

        $this->load->view('main/header');

        $this->load->view('main/menu',$data);

        $this->load->view('main/breadcrumbs');

        $this->load->view('domain/search');

        $this->load->view('domain/list');
        
        $this->load->view('main/footer');

    }


}

        

/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */

Problem: Is there any way out to fetch the data in index function and shouldn't use the model and function again and again ?
Reply
#2

(This post was last modified: 04-09-2015, 02:22 AM by Athov.)

my example is:
make a MY_Controller in app/core
and in MY_Controller do something like

PHP Code:
class MY_Controller extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct();
    }
    
    public function 
layout($content_name$data = array())
    {
        
$data['main_menu'] = $this->load->view('layout/menu'nulltrue);
        
$data['main_content'] = $this->load->view($content_namenulltrue);
        
$this->load->view('layout/view'$data);
    }



in the controller you do $this->layout('some_view');

layout/view.php is like
Code:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
    <header>
        <h1>Example</h1>
        <nav>
            <?=$main_menu; ?>
        </nav>
    </header>
    <div class="container">
        <?=$main_content; ?>
    </div>
    <footer>
        .....
    </footer>
</body>
</html>
Reply
#3

Explain in details how does it works in few lines??
Reply
#4

(This post was last modified: 04-09-2015, 03:01 AM by Athov.)

the last parameter in $this->load->view() is used to not display the view but return in see this

$data['main_menu'] = $this->load->view('layout/menu', null, true);
// here you get the HTML for the menu that is in app/views/layout/menu.php

$data['main_content'] = $this->load->view($content_name, null, true);
// here you get the HTML for the currently displayed page

and this if you don't know <?=$main_content; ?> is <?php echo $main_content; ?> see this

and app/views/layout/view.php is the template you can make anything in it

also this is an example you can make it in many other ways

PS: i'm not really good at explaining
Reply
#5

Thanks -> but helpful

am not using __contruct() in my site ->

i can load my function in this way

localhost/web/home (web = is a class)

problem: how do I load it in this way -> localhost/home
Reply
#6

(04-09-2015, 02:59 AM)waqaspuri Wrote: Thanks -> but helpful

am not using __contruct() in my site ->

i can load my function in this way

localhost/web/home (web = is a class)

problem: how do I load it in this way -> localhost/home

With a route. See the docs.
PHP Code:
$route['the-requested-url-to-route'] = 'controller/method'

So
PHP Code:
$route['home'] = 'web/home'//send requests for "home" to the "web" controller, "home" method 
Reply
#7

(04-09-2015, 11:15 AM)CroNiX Wrote:
(04-09-2015, 02:59 AM)waqaspuri Wrote: Thanks -> but helpful

am not using __contruct() in my site ->

i can load my function in this way

localhost/web/home (web = is a class)

problem: how do I load it in this way -> localhost/home

With a route. See the docs.

PHP Code:
$route['the-requested-url-to-route'] = 'controller/method'

So

PHP Code:
$route['home'] = 'web/home'//send requests for "home" to the "web" controller, "home" method 
Reply
#8

$route['(:any)'] = 'web/$1'; //send requests for "home" to the "web" controller, "home" method

works perfect
Reply




Theme © iAndrew 2016 - Forum software by © MyBB