php - Passing multi-Array from Model to a Controller -
i have controller array $added_tabs. pass array function model. in model if print results works fine, if return $row controller again, show 1 record.
any ideas?
in controller have this:
$this->load->model('check_info/check_post_day'); $admin_post_day = $this->check_post_day->check_post($added_tabs); echo "<pre>"; print_r($admin_post_day); echo "</pre>";
in model have this:
class check_post_day extends ci_model { function check_post($data) { foreach ($data $info) { $row = array(); $query = $this->db->query(" select admin_post_day.page_id, admin_post_day.admin_id, admin_post_day.hour_post, admin_post_day.discount, admin_post_day.url, admin_post_day.message_post, admin_post_day.voucher, admin_post_day.date_saved, fb_pages.page_id, fb_pages.id_fb_pages, fb_pages_admin.access_token admin_post_day inner join fb_pages on admin_post_day.page_id=fb_pages.page_id inner join fb_pages_admin on fb_pages.id_fb_pages=fb_pages_admin.id_fb_pages admin_post_day.page_id " . $info['page_id'] . " order admin_post_day.date_saved desc limit 1 "); foreach ($query->result_array() $row) { $info = $row; } if ( !empty( $query ) ) { return $row; } else { return false; } } } }
//upate
class check_post_day extends ci_model { function check_post($data) { foreach ($data $info) { $row = array(); $query = $this->db->query(" select admin_post_day.page_id, admin_post_day.admin_id, admin_post_day.hour_post, admin_post_day.discount, admin_post_day.url, admin_post_day.message_post, admin_post_day.voucher, admin_post_day.date_saved, fb_pages.page_id, fb_pages.id_fb_pages, fb_pages_admin.access_token admin_post_day inner join fb_pages on admin_post_day.page_id=fb_pages.page_id inner join fb_pages_admin on fb_pages.id_fb_pages=fb_pages_admin.id_fb_pages admin_post_day.page_id " . $info['page_id'] . " order admin_post_day.date_saved desc limit 1 "); if($query->num_rows() > 0){ $rows[] = $query->result_array(); } } return $rows; } }
of course , use return
in loop , right after first run - returns output.
change:
if ( !empty( $query ) ) { return $row; } else { return false; }
to:
if ( !empty( $query ) ) { $rows[] = $row; }
and after loop ends:
if ( count($rows) > 0 ) { return $rows; } else { return false; }
update please notice you're wasting resources using second loop. it's bad practice in opinion.
foreach ($query->result_array() $row) { $info = $row; }
consider adding output of result_array
function , handle instead.
and according @rockethazmat 's comment,
empty($query) doesn't think does.
you should change condition to:
if( count($row) > 0)
update 2 instead of second loop , condition:
if($query->num_rows() > 0){ $rows[] = $query->result_array(); }
please notice if result_array
returns array $rows multi-dimensional array.
Comments
Post a Comment