CodeIgniter Forums
[SOLVED] Remove old result - Ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [SOLVED] Remove old result - Ajax (/showthread.php?tid=70430)



[SOLVED] Remove old result - Ajax - googlemy - 04-08-2018

Hi CI Team,

I very need your help to fix this,

1.How to remove old result when I click another category?  
2.How to show all categories without click "All Categories" ?

Please help me....

View:
PHP Code:
<class="nav-link active" onclick="product_category(''); return false;" id="tab-all" data-toggle="tab" href="#tab-all" role="tab">All Categories</a>
<
class="nav-link active" onclick="product_category('1'); return false;" id="tab-1" data-toggle="tab" href="#tab-1" role="tab">Category 1</a>

<
class="nav-link active" onclick="product_category('2'); return false;" id="tab-2" data-toggle="tab" href="#tab-2" role="tab">Category 2</a>

<
class="nav-link active" onclick="product_category('3'); return false;" id="tab-3" data-toggle="tab" href="#tab-3" role="tab">Category 1</a>



<
div class="row justify-content-center" id="category">
</
div>

<
script>
function 
product_category(id){
    $.
ajax({
        
url"<?php echo base_url('main/getValues/');?>"+id,
        
dataType'json',
        
type"POST",
        
cachefalse,
        
success:function(data){
            var 
len data.results.length;
            for (var 
0ileni++) {
                var 
id  data.results[i].id;
                var 
thumbnail  data.results[i].thumbnail;
                $(
"#category").append("<div class='col'><div class=''><img class='' src='"+site_url+'uploads/products/'+id+'/thumb_'thumbnail +"'></div></div>");
            }
        }
    });
}
</
script


Controller:
PHP Code:
function getValues($id=''){
 
       $data['results'] = $this->products_model->get_product_by_category($id);
 
       $this->output->set_content_type('application/json');
 
       $this->output->set_output(json_encode($data));
 
       return $data;
 
   

Model:
PHP Code:
public function get_product_by_category($id '')
 
   {
 
       $this->db->select('*');
 
       $this->db->where('category'$id);
 
       $products $this->db->get('products')->result();

 
       return $products;
 
   



RE: Remove old result - Ajax - jreklund - 04-08-2018

1. Empty #category before appending data
PHP Code:
var len data.results.length;
$(
"#category").empty();
... 
2. Call product_category on the first run.
Code:
product_category('');
</script>



RE: Remove old result - Ajax - theedo - 04-08-2018

You can remove all elements inside the div using $("#category").empty(); . So when your PHP send data to ajax call, the first thing is
PHP Code:
$("#category").empty();. 


To show all categories you can do it maybe static. So you can do a foreach like that:

PHP Code:
<div class="row justify-content-center" id="category">
<?
php foreach($categories as $category): ?>
elements html etc etc
<?php endforeach; ?>
</div> 


This should work


RE: Remove old result - Ajax - googlemy - 04-08-2018

Thanks you so much jreklund!

also theedo.... thanks

Problem solved!


RE: Remove old result - Ajax - InsiteFX - 04-08-2018

Please edit your forum topic and add [SOLVED] to the Title.