如何从codeigniter中显示内容从数据库,而不从调用控制器从视图? [英] how to display content from database in codeigniter without calling controller from view?

查看:94
本文介绍了如何从codeigniter中显示内容从数据库,而不从调用控制器从视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的codeigniter。我按照视频教程并成功创建了登录注册系统。注册或登录后,用户访问一个名为members.php的页面。



members.php是一个视图。



我的控制器是main.php,模型是model-users.php。



我希望members.php显示/拥有数据库中的内容,它一旦登录或注册?



我在网上的几个资源,说不从控制器的视图。



感谢

解决方案

我认为 CodeIgniter文档实际上很清楚,很容易遵循。



控制器

pre> <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问);

类成员扩展CI_Controller {
function __construct(){
parent :: __ construct();

$ this-> load-> model('members_model');
}

function index(){
$ data ['members'] = $ this-> members_model-> get_members

$ this-> load-> view('header',$ data);
$ this-> load-> view('members');
$ this-> load-> view('footer');
}
}

模型 p>

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

class Members_model extends CI_Model {

function get_members(){
$ query = $ this-> db-> get('members');
return $ query-> result_array();
}
}

查看 p>

 <?php 

print_r






要访问上述页面, a href =http://yoursite.com/index.php/members> http://yoursite.com/index.php/members 。



您可以使用 .htaccess 文件从URI中删除 index.php 段。当您运行控制器时, index()方法会自动调用。


I am new to codeigniter. I followed a video tutorial and successfully created a login registration system. After registration or login the users reach a page called members.php.

members.php is a view.

my controller is main.php and model is model-users.php.

I want members.php to show/have content from the database so that the users can read it once the login or register?

I followed a few resources on the web that said not to call controller from view. How can I accomplice the above without doing so?

Thanks

解决方案

I think the CodeIgniter documentation is actually very clear, and very easy to follow. Nevertheless, here's an example of how you might structure your app.

Controller:

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

class Members extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->model('members_model');
    }

    function index() {
        $data['members'] = $this->members_model->get_members();

        $this->load->view('header', $data);
        $this->load->view('members');
        $this->load->view('footer');
    }
}

Model:

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

class Members_model extends CI_Model {

    function get_members() {
        $query = $this->db->get('members');
        return $query->result_array();
    }
}

View:

<?php

print_r($members);


To access the above page, you would visit http://yoursite.com/index.php/members.

You can use a .htaccess file to remove the index.php segment from the URI. The index() method is automatically called when you run a controller.

这篇关于如何从codeigniter中显示内容从数据库,而不从调用控制器从视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆