4

I am having this function in my controller.

function get_data($year, $month) {

        $query = $this->db->select('date,name,type')->from('books')->like('date', "$year-$month", 'after')->get();
        $data = array();
        $data2 = array();
        $data3 = array();


        foreach ($query->result() as $row) {
            if ($row->name == 'AA' && $row->type == 'AM') {
                $data[substr($row->date, 8, 2)] = '<div class="aa_am">' . $row->name . ' ' . $row->type . '</div>';
            } else if ($row->name == 'AA' && $row->type == 'PM') {
                $data2[substr($row->date, 8, 2)] = '<div class="aa_pm">' . $row->name . ' ' . $row->type . '</div>';
            } else if ($row->name == 'BB' && $row->type == 'AM') {
                $data3[substr($row->date, 8, 2)] = '<div class="bb_am">' . $row->name . ' ' . $row->type . '</div>';
            } 
        }
        return $data;
    }

I want to retrieve all data which is in $data,$data1 and $data2 at the same time. Is it possible to do it?? If anyone have an idea it would be a help for me.

2
  • return array($data,$data2,$data3); , other option is return array_merge($data,$data1,$data2). array merge have chances of data lose in case of any key is same. Commented Jun 23, 2015 at 4:11
  • Is this some kind of homework or something? because same question was posted with same code Commented Jun 23, 2015 at 5:47

2 Answers 2

2

From your code, I don't know what result you expect by combining all data which is in $data, $data1 and $data2.

but you can try array_merge(array1,array2,array3...) function if you want all data where key starting from 0 and increases by 1 for each value.

Or else you can create another array with all data values like,

return array("data" => $data, "data1" => $data1, "data2" => $data2)

by this way you can achieve what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in following way,

function get_data($year, $month) {

    $query = $this->db->select('date,name,type')->from('books')->like('date', "$year-$month", 'after')->get();
    $data = array();
    $data1 = array();
    $data2 = array();


    foreach ($query->result() as $row) {
        if ($row->name == 'AA' && $row->type == 'AM') {
            $data[substr($row->date, 8, 2)] = '<div class="aa_am">' . $row->name . ' ' . $row->type . '</div>';
        } else if ($row->name == 'AA' && $row->type == 'PM') {
            $data1[substr($row->date, 8, 2)] = '<div class="aa_pm">' . $row->name . ' ' . $row->type . '</div>';
        } else if ($row->name == 'BB' && $row->type == 'AM') {
            $data2[substr($row->date, 8, 2)] = '<div class="bb_am">' . $row->name . ' ' . $row->type . '</div>';
        } 
    }
$return['data']=$data;
$return['data1']=$data1;
$return['data2']=$data2;
    return $data;
}

you can acces in view as varible,

$data['index'], $data1['index'], $data2['index']

if you want to merge them then,

array_merge($data,$data1,$data2);

2 Comments

I think this should work. but it doesn't give me the expected output because the way i am checking condition is wrong. Here if the 1st condition is true other conditions won't be checked. I want all conditions to be checked. any idea to do that?
try to debug it and check how data comes. using print_r($query->result()) and then print_r($row->name)

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.