获取所有现有行的PHP MVC查询仅返回1行

时间:2017-01-12 18:11:58

标签: php model-view-controller

我有一个工作查询,但它只返回1行 - 我在哪里或者我该怎么做才能返回所有现有行?

型号:

    public function getInfo() {

    $info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");

    if ($info_query->num_rows){
        return array(
            'info_id'       => $info_query->row['info_id'],
            'name'          => $info_query->row['name'],
            'amount'        => $info_query->row['amount'],
            'date_start'    => $info_query->row['date_start'],
            'date_end'      => $info_query->row['date_end'],
            'status'        => $info_query->row['status'],
            'date_added'    => $info_query->row['date_added']
        );
    }

}

控制器:

        $info_options = $this->model_extension_total_info->getInfo();

    if ($info_options){
        $json['info_options'] = array();

        $json['info_options'][] = array(
            'info_id'     => $info_options['info_id'],
            'name'          => $info_options['name'],
            'amount'        => $info_options['amount'],
            'date_start'    => $info_options['date_start'],
            'date_end'      => $info_options['date_end'],
            'status'        => $info_options['status'],
            'date_added'    => $info_options['date_added']
        );
    }

当我在控制器中尝试foreach()时,我仍然只得到一行。:

        foreach ($info_options as $info) {
            var_dump($info_options['name']);exit;
            //var_dump($info_options['name']); results: Warning: Illegal string offset 'name' 
        }

在模型中,当我转储时: $info_query我得到9 - 这是我期待的所有行。

不是特别肯定我错过了什么或做错了。

1 个答案:

答案 0 :(得分:0)

你没有循环结果。不在模型中,也不在控制器中。虽然我不知道你为什么这样做两次。也许我只是不理解你的代码。

型号:

$data = [];    

if ($info_query->num_rows){
    foreach ($info_query->result() as $row) {
        $data[] = array(
            'info_id'       => $row['info_id'],
            'name'          => $row['name'],
            'amount'        => $row['amount'],
            'date_start'    => $row['date_start'],
            'date_end'      => $row['date_end'],
            'status'        => $row['status'],
            'date_added'    => $row['date_added']
        );
    }

    return $data;
}

控制器:

$info_options = $this->model_extension_total_info->getInfo();
$json['info_options'] = array();

if ($info_options){
    foreach ($info_options as $info) {

        $json['info_options'][] = array(
            'info_id'     => $info['info_id'],
            'name'          => $info['name'],
            'amount'        => $info['amount'],
            'date_start'    => $info['date_start'],
            'date_end'      => $info['date_end'],
            'status'        => $info['status'],
            'date_added'    => $info['date_added']
        );
    }
}

从技术上讲,你根本不需要循环。你可以:

public function getInfo() {

    $info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");

    if ($info_query->num_rows){
        return $info_query->result();
    }
    else
    {
        return false;
    }
}

$info_options = $this->model_extension_total_info->getInfo();

// and that's it, just do whatever you need with $info_options