[英]How to display a table using CodeIgniter?
Just stared learning CodeIgniter (using v3.1.7). 刚刚开始学习CodeIgniter(使用v3.1.7)。 I have this table called training .
我有这张桌子叫做训练 。
I followed the steps described in this tutorial : 我遵循了本教程中描述的步骤:
First, create a model called training_model.php (put it in /application/models ) 首先,创建一个名为training_model.php的模型(将其放在/ application / models中 )
<?php
class training_model extends CI_Model {
function get_all_trainings(){
$q = $this->db->get('training');
if($q->num_rows() > 0){
foreach ($q->result() as $row){
$data[] = $row;
}
return $data;
}
}
?>
Then create it's controller (training.php), put in /application/controllers . 然后创建它的控制器(training.php),放入/ application / controllers中 。
<?php
if(!defined("BASEPATH")) exit("No direct script access allowed");
class training extends CI_Controller
{
public function index()
{
$this->load->model("training_model");
$data["trainings"]=$this->users_model->get_all_trainings();
$this->load->view("users_view", $data);
}
}
Last one is the view (training_view.php), put it into /application/views 最后一个是视图(training_view.php),将其放入/ application / views
<?php
if (!empty($trainings)){
foreach ($trainings as $t){
echo $t->eventName .' '. $t->eventDescription.' '.$t->eventDate.' '.$t->eventVenue;
}
}
?>
How to access the view from the browser, so I can see the database's content? 如何从浏览器访问视图,以便可以看到数据库的内容?
If your site was example.com
then the url 如果您的网站是
example.com
则网址
http://example.com/training
should work. 应该管用。
You need to implement an .htaccess
file for the above to work. 您需要实现一个
.htaccess
文件,以上内容才能正常工作。 Without .htaccess
you would need this url 没有
.htaccess
您将需要此URL
http://example.com/index.php/training
Read about removing the index.php file from the URL HERE . 阅读有关从URL HERE中删除index.php文件的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.