Welcome Guest, Not a member yet? Register   Sign In
Else condition error in model
#1
Exclamation 

Hi, Trying to learn Codeigniter. I have this form where I am trying to load dropdown value from db.

I if I use this code in model, it works fine:
Code:
    function getData($loadType,$loadId){
        if($loadType=="state"){
            $fieldList='id,v_state_name as name';
            $table='vbc_state';
            $fieldName='country_id';
            $orderByField='v_state_name';
        }else{
            $fieldList='id,v_state_region as name';
            $table='vbc_state_region';
            $fieldName='v_state_id';
            $orderByField='v_state_region';
        }
        $this->db->select($fieldList);
        $this->db->from($table);
        $this->db->where($fieldName, $loadId);
        $this->db->order_by($orderByField, 'asc');
        $query=$this->db->get();
        return $query;
    }

Since I need one more field to load, and I add this, I get error:

Code:
    function getData($loadType,$loadId){
        if($loadType=="state"){
            $fieldList='id,v_state_name as name';
            $table='vbc_state';
            $fieldName='country_id';
            $orderByField='v_state_name';
        }else{
            $fieldList='id,v_state_region as name';
            $table='vbc_state_region';
            $fieldName='v_state_id';
            $orderByField='v_state_region';
        }else{
            $fieldList='id,v_city as name';
            $table='vbc_city';
            $fieldName='v_state_region_id';
            $orderByField='v_city';
        }
        $this->db->select($fieldList);
        $this->db->from($table);
        $this->db->where($fieldName, $loadId);
        $this->db->order_by($orderByField, 'asc');
        $query=$this->db->get();
        return $query;
    }

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\drop\application\models\model.php on line 28


Is there anything wrong with this code?
Reply
#2

You can't chain more than one "else" together. It should be:

if
else if
else

not

if
else
else
Reply
#3

Code:
function getData($loadType,$loadId){
       if($loadType=="state"){
           $fieldList='id,v_state_name as name';
           $table='vbc_state';
           $fieldName='country_id';
           $orderByField='v_state_name';
       }elseif{
           $fieldList='id,v_state_region as name';
           $table='vbc_state_region';
           $fieldName='v_state_id';
           $orderByField='v_state_region';
       }else{
           $fieldList='id,v_city as name';
           $table='vbc_city';
           $fieldName='v_state_region_id';
           $orderByField='v_city';
       }
       $this->db->select($fieldList);
       $this->db->from($table);
       $this->db->where($fieldName, $loadId);
       $this->db->order_by($orderByField, 'asc');
       $query=$this->db->get();
       return $query;
   }
Web Developer
Reply
#4

change your condition to

PHP Code:
if ($variable == "value1"){
      
//code
}else if ($variable == "value2"){
       
//code
}else{
      
//code

Reply




Theme © iAndrew 2016 - Forum software by © MyBB