简体   繁体   English

动态页面不工作

[英]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.我创建了一个名为 index 的控制器,我想开发动态控制器,就像它是一次又一次创建页面名称的一大障碍,所以我决定创建,但我对此有些困惑,尽管我将如何开发我放置的页面管理面板中的一个选项,如用户可以创建页面,但最大的问题是我无法检索这些页面。

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 ?现在我正在尝试向来自页面的请求数据的页面发送一个调用,这样当用户点击导航和我想作为参数传递给索引的页面 id 时将调用页面数据控制器那它怎么会通过?

This isn't the correct way to develop dynamic pages using Codeigniter.这不是使用 Codeigniter 开发动态页面的正确方法。 Codeigniter is an MVC framework. Codeigniter 是一个 MVC 框架。 If you follow the proper seperation of Model-View-Controller, it is very easy to develop dynamic web pages using codeigniter.如果按照模型-视图-控制器的正确分离,使用codeigniter开发动态网页是非常容易的。

Codeigniter user guide is very easy to follow and has good examples. Codeigniter 用户指南非常容易理解并且有很好的例子。 Please try to do some of those given tutorials.请尝试做一些给定的教程。

In codeigniter, the views are loaded in the order they appear in controller.在 codeigniter 中,视图按照它们在控制器中出现的顺序加载。 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.并且您将能够在 my_view 中将标题视为 $title 变量。

Similarly you can save navigation template in views/templates folder and load it as:同样,您可以将导航模板保存在 views/templates 文件夹中并将其加载为:

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

You don't do:你不这样做:

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

like with normal php pages.就像普通的 php 页面一样。 Codeigniter is an MVC web framework. Codeigniter 是一个 MVC Web 框架。 Follow the proper MVC guidelines as described in the codeigniter's excellent user guide documentation.遵循 codeigniter 优秀用户指南文档中描述的正确 MVC 指南。

Default format of Codeigniter URLs is like: Codeigniter URL 的默认格式如下:

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

If I have a controller named Welcome.php like:如果我有一个名为 Welcome.php 的控制器,例如:

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.我将看到 7,因为如果我们不传递任何东西,它是分配给 $Page 变量的默认值。

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. $Page 变量将被分配值 2,它将显示 2。

Here are the comments taken from default controller that comes with Codeigniter installation:以下是来自 Codeigniter 安装附带的默认控制器的评论:

/**
 * 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
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM