簡體   English   中英

從數據庫顯示表

[英]Display Table From Database

在我的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);
}
  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. 

重組了整個代碼。 好心檢查,

模型:

<?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_idsController傳遞給Model
2)使用Active Records代替Raw SQL Queries
3)返回的result()代替row() 不同之處在於result()返回一個對象數組,或者在失敗時返回一個空數組row()返回單個結果行。
4) foreach循環中的語法錯誤。

在模型中:

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

希望它對您有用,我的回答對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM