0

I am making a load more data function when pressing a button via Codeigniter

I got this problem in-front of me can't solve it maybe the problem is the array doesn't passed to the view !! Can you help ?

Javascript

<script type="text/javascript">
$(document).ready(function(){
var num_ads = <?=$num_ads?>;
var loaded_ads = 0;
    $("#more_button").click(function(){
        loaded_ads += 10;
        $.get("e3lanat/get_ads/" + loaded_ads, function(data){
            $("#Ebda2").append(data);

        });

        if(loaded_ads >= num_ads - 10)
        {
            $("#more_button").hide();
            //alert('hide');
        }
    })
})
</script>

View

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

Controller

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

Thanks in Advance :)

Hesham Watany
  • 300
  • 3
  • 20

2 Answers2

0

You are trying to load the view instead of returning data.

In Controller:

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']);
}

In View Javascript:

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

Now you need to iterate over the JSON and append your data to the view. See how to Loop through JSON object List and appending data to the view will go something like this:

$("#Ebda2").append('<div class="water"> ...every thing inside... </div>');
Community
  • 1
  • 1
Scorpion
  • 396
  • 2
  • 14
0

change this

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

to

 <?php foreach ($latest_messages as $ads)
Hesham Watany
  • 300
  • 3
  • 20