jeudi 25 juin 2015

php codeIgniter how to return null when the array is empty?

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

Aucun commentaire:

Enregistrer un commentaire