[英]Display Table From Database
On my database in codeigniter I have a table called store. 在我的codeigniter数据库中,有一个名为store的表。 I would like to be able to display my rows in table format on my view.
我希望能够以表格格式在视图上显示行。 And have it display in order by url
并按网址顺序显示
I have done the model and view but not sure what to put on the controller. 我已经完成了模型和视图,但不确定要在控制器上放什么。
Error 错误
Parse error: syntax error, unexpected '$data' (T_VARIABLE) in C:\Xampp\htdocs\codeigniter\codeigniter-cms\application\modules\admin\controllers\store\store.php on line 61
Model 模型
<?php
class Model_store extends CI_Model {
public function getStores() {
$query = $this->db->query("SELECT DISTINCT * FROM " . $this->db->dbprefix. "store");
return $query->row();
}
}
View Updated 查看更新
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form action="" method="post" enctype="multipart/form-data" >
<?php foreach ($stores as $row) { ;?>
<tr>
<td><?php echo $row['store_id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['url'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</form>
</div>
</div>
Controller Updated 控制器更新
public function index() {
$this->load->model('admin/store/model_store');
$stores = array();
$stores = $this->model_store->getStores();
$data['cancel'] = site_url('admin/dashboard');
return $this->load->view('store/store_list', $data);
}
There few issues are in your code :
1. $data is not declared before use [ $data=array() ]
2. When getStores(); called no store id is been passed which will cause error in model.
3. In View there is no existence of $query so you cant make loop of it to show.
You should use $store of controller $data["store"] which is receiving the data from
model.
Hope you can understand it and make the rectifications to run it properly. Please let me know if need any further help.
Restructured whole of your code. 重组了整个代码。 Kindly check,
好心检查,
Model: 模型:
<?php
class Model_store extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getStores($store_ids)
{
$this->db->select('*');
$this->db->distinct();
$this->db->where_in('store_id', $store_ids);
$query = $this->db->get($this->db->dbprefix."store");
if($query->num_rows > 0)
return $query->result();
return false;
}
}
Controller: 控制器:
class Store extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/store/model_store');
}
public function index()
{
$store_ids = array('1','2','3'); // Pass your store ids here
$data['store'] = $this->model_store->getStores($store_ids);
$data['cancel'] = site_url('admin/dashboard');
$this->load->view('store/store_list', $data);
}
}
View: 视图:
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form action="" method="post" enctype="multipart/form-data" >
<?php foreach ($store as $row) { ?>
<tr>
<td><?php echo $row->store_id; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->url ;?></td>
</tr>
<?php } ?>
</form>
</div>
</div>
What changes did: 做了什么更改:
1) Passed store_ids
from Controller
to Model
. 1)将
store_ids
从Controller
传递给Model
。
2) Used Active Records
instead of Raw SQL Queries
. 2)使用
Active Records
代替Raw SQL Queries
。
3) Returned result()
instead on row()
. 3)返回的
result()
代替row()
。 Difference is result()
returns an array of objects, or an empty array on failure wheareas row()
returns a single result row. 不同之处在于
result()
返回一个对象数组,或者在失败时返回一个空数组row()
返回单个结果行。
4) Syntax error in foreach
loop. 4)
foreach
循环中的语法错误。
In Model: 在模型中:
$this->db->select("*");
if(!empty($store_id))
{
$this->db->where("store_id",$store_id); //if where required at all
}
$this->db->from($this->db->dbprefix."store");
In Controller: 在控制器中:
$data['store'] = $this->model_store->getStores(); // send store id when required
$data['cancel'] = site_url('admin/dashboard');
$this->load->view('store/store_list', $data);
In View: 在视图中:
foreach($store as $st)
{
echo $st["id"];//and print all
}
Hope it will work for you and My answer helps you. 希望它对您有用,我的回答对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.