php codeIgniter当数组为空时如何返回null?

时间:2015-06-25 09:38:43

标签: php codeigniter if-statement

我正在检查一些条件语句并最终输出一个值。我得到了正确的输出。但是当$ query为空时我想返回null。否则显示php错误。我想在查询为空时摆脱php错误。我不知道这个。如果有人有想法,那将是一个很大的帮助。

这是我的模型方法。

function get_calendar_data($year, $month) {

        $query = $this->db->select('date_cal,title,type,description,telephone_number,advanced_payment_status')->from('reservations')->like('date_cal', "$year-$month", 'after')->order_by('date_cal', "asc")->get();

        if ($query!='') {
            $content = "";
            $lastDay = -1;
            $index = 0;

            foreach ($query->result() as $row) {

                if ($lastDay != intval(substr($row->date_cal, 8, 2))) {
                    if ($index > 0) {
                        if ($content != '') {
                            $cal_data[$lastDay] = $content;
                            $content = '';
                        }
                    }
                    $index = 0;
                }

                if ($row->title == 'RP' && $row->type == 'AM' && $row->advanced_payment_status=='yes') {
                    $content .= '<div class="rp_am_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'RP' && $row->type == 'AM' && $row->advanced_payment_status=='no') {
                    $content .= '<div class="rp_am_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'RP' && $row->type == 'PM' && $row->advanced_payment_status=='yes') {
                    $content .= '<div class="rp_pm_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'RP' && $row->type == 'PM' && $row->advanced_payment_status=='no') {
                    $content .= '<div class="rp_pm_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'GK' && $row->type == 'AM' && $row->advanced_payment_status=='yes') {
                    $content .= '<div class="gk_am_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'GK' && $row->type == 'AM' && $row->advanced_payment_status=='no') {
                    $content .= '<div class="gk_am_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                } else if ($row->title == 'GK' && $row->type == 'PM' && $row->advanced_payment_status=='yes') {
                    $content .= '<div class="gk_pm_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                }else if ($row->title == 'GK' && $row->type == 'PM' && $row->advanced_payment_status=='no') {
                    $content .= '<div class="gk_pm_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
                }else{
                    $content .='<div class="add"></div>';
                }


                $lastDay = intval(substr($row->date_cal, 8, 2));
                $index++;
            }

            if ($lastDay != -1 && $content != '') {
                $cal_data[$lastDay] = $content;
            }            
            return $cal_data;
        } else if($query==''){            
            return Null;         
        }
    }

当$ query为空时,我收到此php错误。它说明了当$ query不为空时输出的返回值。

error

4 个答案:

答案 0 :(得分:4)

在函数顶部定义$cal_data = NULL;。如果条件不满足,则永远不会定义$cal_data

如果定义为NULL,那么如果条件满足,它将返回正确的数据,而不是NULL

$cal_data = NULL;
... rest of the code ...
if ($lastDay != -1 && $content != '') {
    $cal_data[$lastDay] = $content;
}            
return $cal_data;

答案 1 :(得分:2)

确保始终在try..catch块中编写与DB相关的代码。假设$ db-&gt;选择返回数组,您可以编写以下内容:

try{
    if(isset($query) && empty($query)){
        return null;
    }
    <Code line 1>
    <Code line 2>
    <Code line 3>
    ...
}catch(Exception $e){
    return null; //or what ever you want to return.
}

答案 2 :(得分:1)

One more thing to remember your first try did not work since the query is never returned empty. If i write a query that returns zero rows from the database the $query will be like

CI_DB_mysql_result Object
(
[conn_id] => Resource id #40
[result_id] => Resource id #45
[result_array] => Array
    (
    )

[result_object] => Array
    (
    )

[custom_result_object] => Array
    (
    )

[current_row] => 0
[num_rows] => 0
[row_data] => 
)

The proper way for checking wether the array is empty is by calling the

result()

method for your query. that is

$query->result();

will return an empty array for zero rows returned.

Or else you can try to check wether the

$query[num_rows]>0

Hope it helped.

答案 3 :(得分:0)

用户num_rows()函数用于获取查询中的行数。

function get_calendar_data($ year,$ month){

    $query = $this->db->select('date_cal,title,type,description,telephone_number,advanced_payment_status')->from('reservations')->like('date_cal', "$year-$month", 'after')->order_by('date_cal', "asc")->get();
    $row_num=$query->num_rows();
    if ($row_num>0) {
        $content = "";
        $lastDay = -1;
        $index = 0;

        foreach ($query->result() as $row) {

            if ($lastDay != intval(substr($row->date_cal, 8, 2))) {
                if ($index > 0) {
                    if ($content != '') {
                        $cal_data[$lastDay] = $content;
                        $content = '';
                    }
                }
                $index = 0;
            }

            if ($row->title == 'RP' && $row->type == 'AM' && $row->advanced_payment_status=='yes') {
                $content .= '<div class="rp_am_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'AM' && $row->advanced_payment_status=='no') {
                $content .= '<div class="rp_am_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'PM' && $row->advanced_payment_status=='yes') {
                $content .= '<div class="rp_pm_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'PM' && $row->advanced_payment_status=='no') {
                $content .= '<div class="rp_pm_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'GK' && $row->type == 'AM' && $row->advanced_payment_status=='yes') {
                $content .= '<div class="gk_am_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'GK' && $row->type == 'AM' && $row->advanced_payment_status=='no') {
                $content .= '<div class="gk_am_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'GK' && $row->type == 'PM' && $row->advanced_payment_status=='yes') {
                $content .= '<div class="gk_pm_yes" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            }else if ($row->title == 'GK' && $row->type == 'PM' && $row->advanced_payment_status=='no') {
                $content .= '<div class="gk_pm_no" id="' . $row->date_cal . '" title="Name :' . $row->description . '  Contact No : ' . $row->telephone_number . '">' . $row->title . ' ' . $row->type . '</div>';
            }else{
                $content .='<div class="add"></div>';
            }


            $lastDay = intval(substr($row->date_cal, 8, 2));
            $index++;
        }

        if ($lastDay != -1 && $content != '') {
            $cal_data[$lastDay] = $content;
        }            
        return $cal_data;
    } else {            
        return Null;         
    }
}