编码器和数据库问题


Codeigniter and database issues

对,我是新手,有点涉水不深。好吧,我试着引入一些数据,我得到了一些错误,我对发生了什么有点困惑。

模型代码如下:

 public function get_webstore_sets($limit, $offset) {
        $this->db->select("*");
        $this->db->from('st_posts');
        $this->db->where("type", "webstore");
        $this->db->limit($limit, $offset);
        $this->db->order_by('created', 'DESC');
        $query = $this->db->get();
        $data = $query->result_array();
        return$data;
    }
    public function count_webstore_sets() {
        $this->db->select("*");
        $this->db->from('st_posts');
        $this->db->where("type", "webstore");
        $this->db->order_by('created', 'DESC');
        $query = $this->db->get();
        $count = $query->num_rows();
        return $count;
    }

,这是我在控制器中尝试的:

class Webstore extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model("post");
        $this->load->database();
        $this->load->helper("url");

    }
    public function index() {
        $this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
        $data = array('page_title' => 'Store', 'video_content');       
        $this->layout("pages/webstore", $data);
    }

}

然后把它放到HTML中:

 <?php foreach ($posts as $webstore): ?>

            <div class="col-md-12" style="height: 225px;">
                <div class="col-md-3">
                    <?php
                    $webstore['images'] = explode(".", $webstore['images']);
                    ?>
                    <img class="thumbnail" width="250px" src="<?php echo $video['images'][1]; ?>.gif"/>
                </div>
                <div class="col-md-5">
                    <h3><?php echo $webstore['title']; ?></h3>
                </div>
                <div class="col-md-4 text-center">
                    Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?>
                     <hr>
                     <a href="http://clips4sale.com/64171/10768027" target="_blank" class="btn btn-primary center-block">Select or Exclusive to www.Borderlandbound.com<br> Please Visit Our Clips Store For <br>Many More Ultra-Hot Videos Like This</a>
<hr>
                </div>
            </div>
            <hr>
        <?php endforeach; ?>

我知道连接是正常的,因为网站正在从其他地方引入数据。

,这是我得到的错误:

Severity: Notice
Message: Undefined variable: posts
Filename: pages/webstore.php
Line Number: 39
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/webstore.php
Line Number: 39

我已经空白的文件路径只是以防万一;)修改以上错误

在此代码中:

public function index()
{
    $this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
    $data = array('page_title' => 'Store', 'video_content');       
    $this->layout("pages/webstore", $data);
}

你传递的是$data数组,而不是$this->data数组。此外,$data阵列中的'video_content'元素应该有它的键,以便您接近它。还是钥匙本身失去了价值?

改为:

public function index() {
    $this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
    $this->data['page_title'] = 'Store';
    $this->layout("pages/webstore", $this->data);
}

现在,您应该能够在视图文件中获得$posts变量。只需检查value/key 'video_content'要做什么

您正在调用没有参数的get_webstore_sets(),因此要么填写一些参数(限制,偏移)或定义函数如下:

public function get_webstore_sets($limit=null, $offset=null) ...

我建议在Webstore类的index()函数中给出参数。