If Else in codeigniter

codeigniter

#1

I read the if else function on google and stackoverflow but wanna I have issue that how
I implement if else function in my code below, please help me how I add if else in my code

Mycode is below;

$user_id = $this->session->userdata('id');
		$is_admin = ($user_id == 1) ? true :false;

		$this->data['is_admin'] = $is_admin;
		$this->render_template('dashboard', $this->data);

#2

Hi @gettingStuck and a warm welbome to the SP Forums:

Try this:

  if(true)  // WITH IF/ELSE
  {
      $user_id  = $this->session->userdata('id');
      $is_admin = false; // DEFAULT TO FALSE
      if( 1 === $user_id ) // 
      {
        // RECTIFIED ERROR
        $is_admin = TRUE;
        $this->data['is_admin'] = $is_admin;
        $this->render_template('dashboard', $this->data);
      }else{
         echo 'NOT LOGGED IN???';
         exit;
      }// endif;
  }else{
   $user_id = $this->session->userdata('id');
   $is_admin = ($user_id == 1) ? true :false;

   $this->data['is_admin'] = $is_admin;
   $this->render_template('dashboard', $this->data); 
}//endif

Using PHP Alternative Syntax - because I find it is easier than matching curly braces.

  $user_id  = $this->session->userdata('id');
  $is_admin = false; // DEFAULT TO FALSE
  if( 1 === $user_id ) : 
      // RECTIFIED ERROR
      $is_admin = TRUE;
      $this->data['is_admin'] = $is_admin;
      $this->render_template('dashboard', $this->data);
  else:
       echo 'NOT LOGGED IN???';
       exit;
  endif;

Edit:
Please note:
// RECTIFIED ERROR


unlisted #4

closed #5

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.