2013-08-01 79 views
1

任何人都可以帮助我检索db数据以及如何在html table.is中查看它。如果不是,那么我给出的编码是正确的请说我该如何给予。为了在html表格中查看它。如何从数据库中检索数据并使用Codeigniter将其显示在html表中

控制器

class edit_content extends CI_Controller { 

    function edit_content() 
    { 
     parent::__construct(); 
     $this->load->model('editcontent_model'); 
     $this->load->helper('url'); 
     $this->load->library('acl'); 
     $this->data = $this->editcontent_model->get_contents(); 
    } 
} 

查看

<table> 
    <tr> 
     <th>Content</th> 
    </tr> 

    <?php foreach($this->data as $r): ?> 
     <tr> 
      <tr><?php echo $r['content']; ?> 
     </tr> 
    <?php endforeach; ?> 

<table> 

型号

class editcontent_model extends CI_Model { 
    var $CI; 

    function editcontent_model(){ 
     parent::__construct(); 
    } 

    function get_contents() { 
     $this->db->select('content'); 
     $this->db->from('contents'); 
     $query = $this->db->get(); 
     return $result = $query->result(); 
     $this->load->view('edit_content/edit_content', $result); 
    } 
} 
+0

你的主要问题是,你正试图将数据传递到并从您的**模型加载视图**。相反,你想在你的**控制器**中做到这一点。 – Jeemusu

+0

您使用的是什么版本的Codeigniter?如果它是最新版本,则可能需要重新编写类构造函数。 – Jeemusu

+0

查看完整的解决方案:http://www.c-sharpcorner.com/UploadFile/225740/fetch-data-from-database-and-show-in-tabular-format-in-codei/ – Super

回答

5

试试这个:

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

class edit_content extends CI_Controller { 

    public function __construct(){ 
     parent::__construct(); 
    } 

    function edit_content() 
    { 
     $data = array(); 
     $this->load->model('editcontent_model'); 
     $this->load->helper('url'); 
     $this->load->library('acl'); 
     $data['result'] = $this->editcontent_model->get_contents(); 
     $this->load->view('edit_content/edit_content', $data); 
    } 
} 
?> 
<!-- myview --> 
<table> 
<tr> 
<th>Content</th> 

</tr> 
<?php foreach($result as $r): ?> 
<tr><?php echo $r->content; ?> 

    </tr> 
<?php endforeach; ?> 
</table> 
<?php 
#mymodel 
class editcontent_model extends CI_Model{ 
    function get_contents() { 
     $this->db->select('content'); 
     $this->db->from('contents'); 
     $query = $this->db->get(); 
     return $result = $query->result(); 
    } 
} 
0

在edit_content FUNC年底将这个您的控制器中的内容

$this->load->view('edit_content/edit_content', $result); 

视图的加载应该在控制器中完成。从我看到的,你的模型已经返回结果,所以最后一行没有执行。

+0

$ data ['name' ] = $ this-> acl-> getUsername($ this-> session-> userdata ['user_id']); \t \t $ content ['result'] = $ this-> editcontent_model-> get_contents(); $ this-> load-> view('edit_content/edit_content',$ data,$ content);上面有代码,它只查看$ data,如果我先给$ content,那么只有$ content正在查看如何查看两个 – user2419839

0

在edit_content功能

$result=$this->editcontent_model->get_contents(); 
$this->load->view('edit_content/edit_content', $result); 

,并在您看来做到这一点,访问内容,如:

<?php foreach($result as $result): ?> 
    <tr> 
<tr><?php echo $result[0]->content; ?> 

</tr> 
<?php endforeach; ?> 

你必须这样做,怎么一回事,因为在$结果是一个关联数组不是一个简单的一。 希望这对你有所帮助。

+0

$ data ['name'] = $ this-> acl-> getUsername($ this-> session-> userdata ['user_id']); $ content ['result'] = $ this-> editcontent_model-> get_contents(); $ this-> load-> view('edit_content/edit_content',$ data,$ content);我有上面的代码,它只查看$ data,如果我先给$ content,那么只有$ content正在查看如何同时查看 – user2419839

相关问题