PHP错误未定义变量:data - 使用Codeigniter加载更多数据

时间:2015-09-18 14:36:27

标签: javascript php codeigniter

通过Codeigniter按下按钮时,我正在加载更多数据功能

我在我面前遇到了这个问题 无法解决它 也许问题是数组没有传递给视图!! 你能帮忙吗?

的Javascript

<div id="Ebda2">
<?php foreach ($data['latest_messages'] as $ads)

    {
echo $ads->current_price;

   echo '<div class="water">';
echo 'div class="eye-top">';
echo '<a href="#"><img class="img-responsive" src="public/images/9.jpg" alt=""></a>';

echo '</div>';
echo '<div class="title-blue">';
echo '<h4><a href="#">';
echo $ads->name;
echo "</a></h4>";
echo "<p>";
echo $ads->description;

echo'</p>';

echo '</div>';

}       
?>



</div>
<div id="more_button">
more
</div>

查看

function __construct(){
    parent::__construct();
                $this->load->model('ads_model');

}

public function index(){
            if($this->session->userdata('logged_in'))
{
    $this->load->model('ads_model');
$data['num_messages'] = $this->ads_model->num_ads();
$data['latest_messages'] = $this->ads_model->get_ads();
$this->load->view('e3lanat_view', $data);
}
else
{
 redirect('signin', 'refresh');
 }
    }
public function insert_rows()
{
$i = 0;
while($i < 50)
{
    $i++;
    $data = array('name' => 'name ' . $i, 'description' => 'description ' . $i,
                'user_id' => 9, 'cat_id' => '1',
                'current_owner_id' => 10, 'cat_id' => '1',

                'initial_price'=> $i+10,
                'current_price'=> $i+50,
                'increase_price'=> $i+30
                );
    $this->db->insert('ads', $data);
    echo $i. '<br />';
}
}


public function get_ads($offset)
{
$this->load->model('ads_model');
$data['latest_messages'] = $this->ads_model->get_ads($offset);
$this->load->view('e3lanat_view/get_ads', $data);
}


**Model**

class ads_model extends CI_Model {
public function __construct()
{
parent::__construct();
}



function get_ads($offset=0)
{
$this->db->order_by('ads_id', 'desc');
$query = $this->db->get('ads', 10, $offset);
return $query->result();
}

function num_ads()
{
$query = $this->db->count_all_results('ads');
return $query;
}
}

控制器

{{1}}

提前致谢:)

2 个答案:

答案 0 :(得分:0)

您正在尝试加载视图而不是返回数据。

在控制器中:

       root GET    /                                       dashboard#index
       help GET    /help(.:format)                         static_pages#help
      about GET    /about(.:format)                        static_pages#about
    contact GET    /contact(.:format)                      static_pages#contact
  customers GET    /customers(.:format)                    customers#index
            POST   /customers(.:format)                    customers#create
new_customer GET    /customers/new(.:format)                customers#new
edit_customer GET    /customers/:id/edit(.:format)           customers#edit
   customer GET    /customers/:id(.:format)                customers#show
            PATCH  /customers/:id(.:format)                customers#update
            PUT    /customers/:id(.:format)                customers#update
            DELETE /customers/:id(.:format)                customers#destroy
   contacts GET    /contacts(.:format)                     contacts#index
            POST   /contacts(.:format)                     contacts#create
new_contact GET    /contacts/new(.:format)                 contacts#new
edit_contact GET    /contacts/:id/edit(.:format)            contacts#edit
            GET    /contacts/:id(.:format)                 contacts#show
            PATCH  /contacts/:id(.:format)                 contacts#update
            PUT    /contacts/:id(.:format)                 contacts#update
            DELETE /contacts/:id(.:format)                 contacts#destroy

在View Javascript中:

public function get_ads($offset) {
    $this->load->model('ads_model');
    $data['latest_messages'] = $this->ads_model->get_ads($offset);
    return json_encode($data['latest_messages']);
}

现在您需要迭代JSON并将数据附加到视图中。了解如何Loop through JSON object List并向视图添加数据将如下所示:

$.get("e3lanat/get_ads/" + loaded_ads, function(data){
    console.log(data);
});

答案 1 :(得分:0)

更改此

<?php foreach ($data['latest_messages'] as $ads)

 <?php foreach ($latest_messages as $ads)