从数据库显示表

时间:2014-09-05 10:18:26

标签: php codeigniter

在codeigniter中的数据库中,我有一个名为store的表。我希望能够在我的视图中以表格格式显示我的行。并按网址顺序显示

我已经完成了模型和视图,但不确定要在控制器上放什么。

错误

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

模型

<?php 

class Model_store extends CI_Model {

    public function getStores() {
        $query = $this->db->query("SELECT DISTINCT * FROM " . $this->db->dbprefix. "store");
        return $query->row();
    }

}

查看已更新

  <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>

控制器已更新

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);
}

3 个答案:

答案 0 :(得分:0)

  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. 

答案 1 :(得分:0)

重组整个代码。请检查,

<强>型号:

<?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;
    }
}


控制器:

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);
    }
}


查看:

<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>


做了哪些更改:

1)从store_ids传递ControllerModel 2)使用Active Records代替Raw SQL Queries 3)在result()上返回row()。差异是result()返回一个对象数组,或者一个空数组失败时,row()返回单个结果行。
4)foreach循环中的语法错误。

答案 2 :(得分:0)

在模型中:

$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");


在控制器中:

$data['store'] = $this->model_store->getStores(); // send store id when required
$data['cancel'] = site_url('admin/dashboard');
$this->load->view('store/store_list', $data);


在视图中

foreach($store as $st)
{
     echo $st["id"];//and print all 
}

希望它对您有用,我的回答可以帮助您。