0

i have an array of id's through which i have to fetch data from two tables basically now let break my issue step by step

First step i have to get the id's of whom i want the data, this is the model that returns data of id's

function child_get($id){
        $this->db->select("id");
        $this->db->from('generic_table');
        $this->db->where("parent",$id);
        $query = $this->db->get();
        return $query->result_array();
    }

this return an array of ids in the following format

Array ( [0] => Array ( [id] => 13 ) [1] => Array ( [id] => 14 ) )

i have no issue so far now i have to traverse this array and dynamically add data into array depending on the id's for this i used a foreach loop

$arrOfIds = $data['child1'];
          foreach($arrOfIds as $row) {
             $data['child3']=array
            (
                "id" => $row["id"],
                "path" => $this->Menu->test_maindata($row["id"])
            );
          }

the model used inside foreach

 public function test_maindata($id)
    {
        $this->db->select("path");
        $this->db->from('main_data');
        $this->db->where("f_key",$id);
        $query = $this->db->get();
        return $query->result_array();
    }

now the first issue it overriding the every value of iteration and returning only the last value see below

Array ( [id] => 14 [path] => Array ( [0] => Array ( [path] => almond.jpg ) ) ) 

you can see it skipped the iteration of id 13

secondly can i make this array in the following pattern

Array( [0] => Array( id=> 13, path=>bluebery.jpg) [1]=> Array( id=> 14, path=>almond.jpg));
2
  • why you are not using join functionality of CI framework Commented May 17, 2017 at 12:52
  • i am looking at it but still one issue remains how to i add it on array dynamically as you can see inside the loop it is overriding on every iteration Commented May 17, 2017 at 12:54

1 Answer 1

1
just use this single query and let me know what is output this is giving

    $this->db->select("generic_table.name,main_data.path");
$this->db->from('generic_table');
$this->db->where("generic_table.id",$id);
$this->db->join("main_data","main_data.f_key = generic_table.id");
$query = $this->db->get();
return $query->result_array();

and for loop iteration

$arrOfIds = $data['child1'];
          foreach($arrOfIds as $row) {
             $data['child3'][]=array
            (
                "id" => $row["id"],
                "path" => $this->Menu->test_maindata($row["id"])
            );
          }
Sign up to request clarification or add additional context in comments.

8 Comments

Array ( [id] => 14 [path] => Array ( [0] => Array ( [id] => 16 [path] => almond.jpg ) ) )
the join is somewhere incorrect it is not comparing the id's the id's are 13,14 and it is returning 14,16
how the tables are related to each other?
now you have to php coding to show proper path in view file
alright i got it working it is returning correct array like Array ( [0] => Array ( [name] => Almonds [path] => almond.jpg ) ) if i am using $this->Menu->test_maindata("14");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.