Mark Alan
Mark Alan

Reputation: 455

dynamic page is not working

I have created a controller named index and I want to develop dynamic controllers like it is a big hurdle to creating page name again and again so i decided to create but i have some confusion about it though how I will developing the pages as I have placed an option in the admin panel like users can create pages though but the big issue is that I am unable to retrive that pages.

Controller

public function index($page = 7) {
        //$page = 7 where 7 is the default page set for home 
        $page_data           = $this->get_data->AllData('pages', $page);
        $data['title']   = $page_data->row()->pagetitle;
        $class               = explode("/", $page_data->row()->template);
        $data['body_class'] = $class[1];

        $this->load->view('includes/header.php', $data);

        if($class[1] == 'home') {
            $this->load->view('templates/slider');
        }

        $this->load->view('templates/navigation.php');

        $page_content   = $page_data->row()->template;
        $this->load->view($page_content, $data);
        $this->load->view('includes/footer.php');
    }

    public function page($pagename) {
        $page_data          = $this->get_data->AllData('pages', $pagename);
        $data['title']      = $page_data->row()->pagetitle;
        $class              = explode("/", $page_data->row()->template);
        $data['body_class'] = $class[1];

        $this->load->view('includes/header.php', $data);

        if($class[1] == 'home') {
            $this->load->view('templates/slider');
        }

        $this->load->view('templates/navigation.php');

        if($class[1] == 'home') {
            $data['slider'] = 'templates/slider';
        }

        $data['content']    = $page_data->row()->template;
        $this->load->view('index', $data);
        $this->load->view('templates/footer-form.php');
        $this->load->view('includes/footer.php');
    }

Model

public function AllData($table, $pageid) {
    $query_data = $this->db->get_where($table, array('pageid' => $pageid));
    return $query_data;
}

My navigation page

 <?php
      $sql_nav = $this->db->get("menu_navigation");
       foreach($sql_nav->result() as $nav) {
                        echo "<li><a href='".base_url()."home/page/".$nav->menu_name."'>".$nav->$menu_name."</a></li>";
                        //output will be http://localhost:90/kwikrepair/home/page/(menu name which is the page name exist in the database)
                    }
                ?>

Now I am trying to send a call to the pages of requested data that is coming from the pages so like page data will be call upon when users click on the navigation and the the page id i want to pass in as a parameter to the index controller that how it will be passed ?

Upvotes: 1

Views: 330

Answers (1)

codefire
codefire

Reputation: 391

This isn't the correct way to develop dynamic pages using Codeigniter. Codeigniter is an MVC framework. If you follow the proper seperation of Model-View-Controller, it is very easy to develop dynamic web pages using codeigniter.

Codeigniter user guide is very easy to follow and has good examples. Please try to do some of those given tutorials.

In codeigniter, the views are loaded in the order they appear in controller. Like:

$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');

So if you want to optionally load a page segment, do in controller:

if($class[1] == 'home') {
    $this->load->view('templates/slider');
}

You don't pass a view inside a variable. Its for passing data that you get from the database model. To pass any data to a view, do:

$data['title'] = "some title" //any data or variable

Then you can pass data to its associated view like:

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

and you will be able to view the title as a $title variable in my_view.

Similarly you can save navigation template in views/templates folder and load it as:

$this->load->view('templates/navigation');

You don't do:

<?php include('templates/navigation.php'); ?>

like with normal php pages. Codeigniter is an MVC web framework. Follow the proper MVC guidelines as described in the codeigniter's excellent user guide documentation.

Default format of Codeigniter URLs is like:

<installation-folder>/index.php/<controller>/<function>

If I have a controller named Welcome.php like:

class Welcome 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 https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function display($Page = 7){
        echo $Page;
    }
}

I can go to the page:

http://127.0.0.1/CI/index.php/welcome/display

and I will be shown 7, as it is the default value assigned to $Page variable if we don't pass it anything.

If we go to:

http://127.0.0.1/CI/index.php/welcome/display/2

$Page variable will get assigned the value 2 and it will display 2.

Here are the comments taken from default controller that comes with Codeigniter installation:

/**
 * 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 https://codeigniter.com/user_guide/general/urls.html
 */

Upvotes: 1

Related Questions