【问题标题】:Codeigniter getting values from mysql database and showing in html table [closed]Codeigniter 从 mysql 数据库中获取值并显示在 html 表中[关闭]
【发布时间】:2015-01-15 17:43:55
【问题描述】:
我想知道获取 mysql 数据库的所有行并将它们显示在 html 表中的正确过程是什么。我知道视图用于 html,模型用于数据库插入等,控制器用于视图和模型之间。
模型、视图、控制器的示例很好。试图在表格中获得类似的东西。
Id Firstname Lastname
1 John Doe
2 Mary Moe
3 Julie Dooley
【问题讨论】:
-
正确的做法是自己研究(google it out),解决这个问题的方法太多了(我现在能想到3个)。您需要来自 SO 用户的代码,请自行完成,如果需要帮助,请返回一些代码。见guide。
-
codeigniter 提供了关于ellislab.com的清晰文档
标签:
php
html
mysql
database
codeigniter
【解决方案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>
希望对你有帮助