1

I'm using Codeigniter and Jquery with AJAX method to query data from two tables in my database with select join from table group with table cat.

This is table description

Table1: cat

======c_id======fk_group_id====cat_name===
======1========1==============cat1=======
======2========1==============cat2=======
======3========1==============cat3=======
======4========1==============cat4=======

Table2: cat_group.

======gid=======gname====gstat============
======1========1=========1==============
======2========1=========1==============
======3========1=========1==============
======4========1=========1==============

Here is my Model:

This function will selct from table cat_group join with table Cat on gid = cat.fk_group_id.

public function cat(){

    $this->db->select('cat_group.*, cat.*');
    $this->db->from('cat_group');
    $this->db->join('cat', 'cat.fk_group_id = cat_group.gid','all')->where('cat_group.gstatus = 1');
    return $this->db->get()->result();
}

My function in Controller: I call method cat from Model and echo data as Json_encode to the client browsers

public function select_cat_by_group(){
    $this->load->model("main/slide_cat_m");
    $val = $this->slide_cat_m->cat();
    if(!$val){
        return false;
    }else{
       echo json_encode($val);
    }
}

Jquery Ajax

 $.ajax({
     method: "GET",
     url: '<?PHP echo base_url('main/select_cat_by_group');?>',
     dataType: "Json",
     success: function (data) {
           $.each(data, function (i, val) {
              if(val.fk_group_id = gid){
                    //I want to select all the rows in table cat where  fk_groupd_id = gid of table cat_group but I can't 
              }
           });
     }
 })

My Json

[{"gid":"1","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"1","fk_group_id":"1","cat_name":"fuck1","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"5","gtype":"2","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"5","fk_group_id":"5","cat_name":"cat_name","c_status":"1","type_for_group":"2","img":"9.jpg","src":"asset\/img\/main"},{"gid":"5","gtype":"2","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"6","fk_group_id":"5","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"5","gtype":"2","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"7","fk_group_id":"5","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"5","gtype":"2","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"8","fk_group_id":"5","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"9","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"10","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"15","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"16","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"17","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"18","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"6","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"1","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"19","fk_group_id":"6","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"20","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"21","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"22","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"23","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"24","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"25","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"},{"gid":"7","gtype":"1","g_name":"a","gstatus":"1","gad_stat":"0","gsrc":"asset\/img\/cat\/","gimg":"cat_ads.jpg","c_id":"26","fk_group_id":"7","cat_name":"cat_name","c_status":"1","type_for_group":"1","img":"9.jpg","src":"asset\/img\/main"}]
DMS-KH
  • 2,669
  • 8
  • 43
  • 73
  • why not just passing `gid` also to `php`? and process it by `where()`? – roullie Feb 13 '16 at 09:51
  • all of your fk_group_id has some id number not gid. your if statment if incorrect. – Ilanus Feb 13 '16 at 17:31
  • @ilanhasanov, As my json respond I got data from DB and I want to use javascript to check up about fk_group_id and gid and if fk_groupd_id = gid will list all of those item and append to some html element – DMS-KH Feb 13 '16 at 18:03

1 Answers1

1

You did small mistake in model. However it should be:

$this->db->select('cat_group.*, cat.*');
    $this->db->from('cat');
    $this->db->join('cat_group', 'cat.fk_group_id = cat_group.gid','all')->where('cat_group.gstatus = 1');
    return $this->db->get()->result();

It must return all the rows of cat where fk_groupd_id = gid. Now you should parse the json result.

UPDATE:

$.ajax({
   type: "POST",
   url: 'YOUR_URL',
   dataType:'JSON',
   success: function(data) {

           $.each(data, function(index, element) {
           //console.log(index);
           //alert(element.c_id);               
           console.log(element.c_id);
           console.log(element.cat_name); // here console.log() is used to debug the data.
        });

      }
})

It's up to you now how you will display the data in HTML. You can check this example as how to parse json object and display them into DOM: how to parse json data with jquery / javascript?

Community
  • 1
  • 1
StreetCoder
  • 9,871
  • 9
  • 44
  • 62
  • @StreedCoder, It is work but I want to filter all the children for a parent in Ajax success function – DMS-KH Feb 13 '16 at 15:57
  • I do not understand understand your logic. Do you want to get all the rows from the table `cat` or `cat_group`. If you want from `cat` table, so, use the query I sent you. If you would like to parse the json data, I am updating answer with ajax success. Just now you have to use logic how you want to display data. – StreetCoder Feb 13 '16 at 22:37
  • I can understand how to fetch data from DB and access in ajax or pass to html But I can't understand how to find the conditional as below question http://stackoverflow.com/questions/35384233/how-to-compare-two-value-and-append-it-if-match-any-value-in-jquery-loop I want to check it in Success if gid = 5 I will find all the rows of fk_group_id which contain value 5 and initial to a variable and tr to find another by compare gid with fk_group_id till the loop finish.after finish I will apppend to a div. – DMS-KH Feb 14 '16 at 04:00
  • it's really confusing what you want actually to achieve. My advice for you as you should apply any complex logic at server side and take result via ajax and put them into HTML. Moreover, you can use the `index` of json in `if else` condition. – StreetCoder Feb 14 '16 at 04:27
  • It is possible if I don't change database and used JS to find out Gid and make conditional with FK_group_id as below function var gid = [],test ='',testval='' ; $.each(data, function (i, val) { if (gid.indexOf(val.grId) == -1) { gid.push(val.grId); console.log(val.grId); } }); This I just find grid without duplicate and then I will try to using it to compare with FK_group_id – DMS-KH Feb 14 '16 at 04:36