简体   繁体   中英

Codeigniter:query results into associative array

I have two tables in database

1.main_category fields (id,main_name);

2.sub_category fields(id,main_id, sub_name)

Here main_id is used for connecting two tables from this i want to get the result like the following array

Array
(
    [CCTV] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [main_id] => 4
                    [name] => first
                )

            [1] => Array
                (
                    [id] => 3
                    [main_id] => 4
                    [name] => second
                )

            [2] => Array
                (
                    [id] => 4
                    [main_id] => 4
                    [name] => second
                )

        )

    [Security Camera] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [main_id] => 5
                    [name] => first
                )

            [1] => Array
                (
                    [id] => 6
                    [main_id] => 5
                    [name] => second
                )

            [2] => Array
                (
                    [id] => 7
                    [main_id] => 5
                    [name] => second
                )

        )

)

Here the key of the array are main_name field which is from the main_category table and the associative array for each key contains the rows which matches the condition

where main_category.id=sub_category.main_id

I want db query to achieve the above result.is it possible with a join query?

This is the structure of the join query you can change it in your own requirement.

function myfun($id){
  $query = "select main_cat.*, sub_cat.* from main_category main_cat
  Join sub_category sub_cat 
  ON main_cat.id = sub_cat.main_id
  where main_cat.id = $id";
  $data = $this->db->query($query);
  return $data->result_array();
}

Hope this will help to you.

try to understand the query and make alteration as required.main_cat as table 1 and sub_category as table two . already you have gave half solution sub_category.employee_id = main_cat.employee_id

  $this->db->select("main_cat.id,main_cat.main_id,main_cat.name,sub_category.id,sub_category.main_id,sub_category.sub_name");
  $this->db->from('main_cat');
  $this->db->join('sub_category', 'sub_category.employee_id = main_cat.employee_id');
  $this->db->where('id', $id);
  $query = $this->db->get();
  return $query->result();

Hello You use codeignitor so use codeignitor mysql query structure it's best practise in future

$this->db->select("main_cat.id,main_cat.main_id,main_cat.name,sub_category.id,sub_category.main_id,sub_category.sub_name");
  $this->db->from('main_cat');
  $this->db->join_using('sub_category', 'employee_id');
  $this->db->where(tablename.'id', $id);
  $query = $this->db->get();
  return $query->result();

employee_id is common in both table so use join_using other wise you use join like this

$this->db->join('sub_category', 'sub_category.employee_id = main_cat.employee_id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM