php 如何在不从视图中调用控制器的情况下在codeigniter中显示数据库中的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14983038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:19:12  来源:igfitidea点击:

how to display content from database in codeigniter without calling controller from view?

phpmysqlcodeigniter

提问by Stacy J

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.

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

members.php is a view.

members.php 是一个视图。

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

我的控制器是 main.php,模型是 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?

我希望 members.php 显示/拥有数据库中的内容,以便用户在登录或注册后可以阅读它?

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

谢谢

回答by Jezen Thomas

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

我认为CodeIgniter 文档实际上非常清晰,并且非常容易理解。不过,这里有一个示例,说明您可以如何构建您的应用程序。

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.

要访问上述页面,您需要访问http://yoursite.com/index.php/members

You can use a .htaccessfile to remove the index.phpsegment from the URI. The index()method is automatically called when you run a controller.

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

回答by Erman Belegu

I don't have any example of the code, but I write one simple example of HTML when you can show name and surname of members. Controller:

我没有任何代码示例,但是当您可以显示成员的姓名时,我编写了一个简单的 HTML 示例。控制器:

function members(){
  $members = $this->model-user->get_list(); //array of members
  $this->load->view('members.php', $members); // load members in view
}

View members.php

查看会员.php

<html>
<body>
    <table>
      <tr>
        <th>Name</th>
        <th>Surname</th>
      </tr>
      <?php foreach($members as $m): ?>
      <tr>
        <tr><?php echo $m['name']; ?>
        <tr><?php echo $m['surname']; ?>
      </tr>
      <?php endforeach; ?>
    <table>
</body>
</html>

Model:

模型:

function get_list() {
   // query in database
   return $query = $this->db->get('members'); //table name members
}

This is the simple example in a short form. I hope that you will understand it.

这是简短形式的简单示例。我希望你能理解。

回答by Tommy Adey

I think i know what you mean, this is definitely not the right way to go about this but if you really must you could call a model from a view like this.
In the view

我想我知道你的意思,这绝对不是解决这个问题的正确方法,但如果你真的必须,你可以从这样的视图调用模型。
在视图中

<div>

    <?php 
    #You could load the model here or autoload it in config->autoload->models(i believe)
    $data = $this->model_name->model_function();?>
    foreach($data as $row): 
           echo '<pre>';
           print_r($row);
           echo '</pre>';
    endforeach; 
    ?>

</div>

回答by Vince Osana

here is the example of my codes:

这是我的代码示例:

views:

意见:

                <?php foreach($ecms as $i){ ?>
                    <tr>
                    <td><?php echo $i->accnum; ?></td>
                    <td><?php echo $i->crdnum; ?></td>
                    <td><?php echo $i->fname; ?></td>
                    <td><?php echo $i->bday; ?></td>
                </tr>
            <?php } ?>

model:

模型:

function masterHide()

{
    $sql =" select * from ecms";
    $query = $this->db->query($sql);
    return($query->num_rows() > 0) ? $query->result(): NULL;
}

controller:

控制器:

    function search() {

//  $this->MasterListAccess();
    $this->load->model('navi_model');
    $query = $this->navi_model->masterHide();
    //check results or returned value of model
    //print_r($query);
    $data['ecms'] = $query;
    $data['main_content'] = 'search';
    $this->load->view('includes/template',$data);
}

回答by Samir Jana

At first connect the database in your project file and connect the model page in your view page and try this code. Go to the view page and try this code

首先连接项目文件中的数据库并连接视图页面中的模型页面并尝试此代码。转到查看页面并尝试此代码

View

看法

20
21
<?php
 $get_data=$this->MyModel->get_table_data();
?>
<table class="table table-bordered">
 <thead>
  <tr>
   <th> Name </th>
   <th> Email </th>
   <th> Phone No </th>
  </tr>
 </thead>
 <tbody>
 <?php foreach($get_data as $val){ ?>
  <tr>
   <td><?php echo $val->name; ?></td>
   <td><?php echo $val->email; ?></td>
   <td><?php echo $val->phone; ?></td>
  </tr>
 <?php } ?>
 </tbody>
</table>

Model

模型

public function get_table_data(){ $qry=$this->db->select('*')->from('Table Name')->get()->result(); return $qry; }