我有一个带有数组$ added_tabs的控制器。我将此数组传递给函数模型。 在我的模型中,如果我打印结果一切正常,但如果我再次将$ row返回到我的控制器,只显示1条记录。
有什么想法吗?
在我的控制器中我有这个:
$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>";
在我的模特中我有这个:
class Check_post_day extends CI_Model {
function check_post($data) {
foreach ($data as $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
FROM 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
where admin_post_day.page_id LIKE " . $info['page_id'] . "
ORDER BY admin_post_day.date_saved DESC LIMIT 1
");
foreach ($query->result_array() as $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 as $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
FROM 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
where admin_post_day.page_id LIKE " . $info['page_id'] . "
ORDER BY admin_post_day.date_saved DESC LIMIT 1
");
if($query->num_rows() > 0){
$rows[] = $query->result_array();
}
}
return $rows;
}
}
答案 0 :(得分:4)
当然,你在循环中使用return
,所以在第一次运行之后 - 它返回输出。
变化:
if ( !empty( $query ) ) {
return $row;
} else {
return false;
}
要:
if ( !empty( $query ) ) {
$rows[] = $row;
}
循环结束后:
if ( count($rows) > 0 ) {
return $rows;
} else {
return false;
}
<强>更新强> 请注意,您使用第二个循环浪费资源。 在我看来这是一个不好的做法。
foreach ($query->result_array() as $row) {
$info = $row;
}
考虑添加result_array
函数的输出并改为处理它。
根据@RocketHazmat的评论,
empty($ query)不会按照您的想法执行。
您应该将条件更改为:
if( count($row) > 0)
更新2 而不是第二个循环和条件:
if($query->num_rows() > 0){
$rows[] = $query->result_array();
}
请注意,如果result_array
返回一个数组,则$ rows将是一个多维数组。