0

I am checking some conditional statements and finally outputting a value. I get the correct output. But when the $query is empty i want to return null. Otherwise it shows a php error. I want to get rid from the php error when the query is empty. I have no idea about this. If anyone has an idea it would be a great help.

here is my model method.

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

I am getting this php error when the $query is empty. It says about the return value which outputting when the $query is not empty.

error

4 Answers 4

4

Define $cal_data = NULL; at the top of the function. If the condition doesnot satisfied then $cal_data will never be defined.

If it is define as NULL then if the condition is satisfied it will return the proper data else NULL.

$cal_data = NULL;
... rest of the code ...
if ($lastDay != -1 && $content != '') {
    $cal_data[$lastDay] = $content;
}            
return $cal_data;
Sign up to request clarification or add additional context in comments.

Comments

2

Make sure that you always write the DB related code in try..catch blocks. Assuming that the $db->select returns array, you can write the following:

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

Comments

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.

Comments

0

User num_rows() functions to get number of rows in query.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.