Codeigniter和数据库问题

时间:2015-10-25 15:57:51

标签: php codeigniter

对我来说这是新的,有点超出我的深度。好的,所以我试图引入一些数据,我得到一些错误,我有点困惑,到底是怎么回事。

这是型号代码:

 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

我已将空白文件路径以防万一;)编辑上述错误

2 个答案:

答案 0 :(得分:2)

在此代码中:

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数组。此外,'video_content'数组的$data元素应该具有让您在视图中接近它的关键。或者它本身缺少价值?

将其更改为:

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变量。只需检查您要对值/键'video_content'执行的操作。

答案 1 :(得分:0)

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

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

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