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