gpt4 book ai didi

php - 将多数组从模型传递到 Controller

转载 作者:行者123 更新时间:2023-11-29 04:46:12 25 4
gpt4 key购买 nike

我有一个带有数组 $added_tabs 的 Controller 。我将此数组传递给函数模型。在我的模型中,如果我打印结果,一切正常,但如果我再次将 $row 返回到我的 Controller ,则只显示 1 条记录。

有什么想法吗?

在我的 Controller 中我有这个:

$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;
}
}
}
}

//更新

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;
}
}

最佳答案

当然,您在循环中使用了 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) doesn't do what you think it does.

您应该将条件更改为:

if( count($row) > 0)

更新 2而不是第二个循环和条件:

if($query->num_rows() > 0){
$rows[] = $query->result_array();
}

请注意,如果 result_array 返回一个数组,$rows 将是一个多维数组。

关于php - 将多数组从模型传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com