2015-01-15 122 views
-2

我想知道什么是正确的过程来获取mysql数据库的所有行,并在html表中显示它们。我知道该视图用于数据库插入等所用的html,模型,以及视图和模型之间使用的控制器。Codeigniter从mysql数据库获取值并在html表中显示

模型,视图,控制器的例子很好。尝试在桌子上找到类似的东西。

Id Firstname Lastname 
1 John Doe 
2 Mary Moe 
3 Julie Dooley 
+0

正确的做法是研究自己(谷歌它),有解决这个问题的方法太多了(我现在可以考虑3个)。您需要SO用户的代码,请自行完成,如果您需要帮助,请回复一些代码。请参阅[指南](https://ellislab.com/codeigniter/user-guide/overview/mvc.html)。 – Kyslik 2015-01-15 18:19:54

+0

codeigniter在http://ellislab.com/ – user254153 2015-01-15 18:35:55

回答

1

做一个模型来获取记录
让我们假设你的型号是为MyModel

class Mymodel extends CI_Model { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->database(); 
    } 
    function getInfos() 
    { 
     $this->db->select("*");//better select specific columns 
     $this->db->from('YOUR_TABLE_NAME'); 
     $result = $this->db->get()->result(); 
     return $result; 
    } 
} 

现在你的控制器。让我们假设你的控制器名称为myController的

class Mycontroller extends CI_Controller 
{ 
    function __construct() { 
     parent::__construct(); 
     $this->load->model('mymodel'); 
    } 
    public function index() 
    { 


     $data['infos']=$this->mymodel->getInfos(); 
     $this->load->view("myview",$data);//lets assume your view name myview 

    } 

} 

现在你的观点,myveiw.php

<table> 
    <thead> 
     <tr> 
      <th>ID</th> 
      <th>Firstname</th> 
      <th>Lastname</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php if((sizeof($infos))>0){ 
       foreach($infos as $info){ 
       ?> 
        <tr> 
         <td><?php echo $info->Id;?></td> 
         <td><?php echo $info->Firstname;?></td> 
         <td><?php echo $info->Lastname;?></td> 
        </tr> 

       <?php 
       } 
      }else{ ?> 
       <tr><td colspan='3'>Data Not Found</td></tr> 
      <?php } ?> 
    </tbody> 


</table> 

希望这有助于你

+0

上提供了清晰的文档,谢谢它的效果。 – Hash 2015-01-15 18:54:16

相关问题