On my database in codeigniter I have a table called 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
.
2) Used Active Records
instead of Raw SQL Queries
.
3) Returned result()
instead on row()
. Difference is result()
returns an array of objects, or an empty array on failure wheareas row()
returns a single result row.
4) Syntax error in foreach
loop.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.