Reputation: 359
I made a controller for a web, this controller works between desktop and mobile user if user coming from mobile so it shows another view and if user come from desktop computer so it shows another view for that thing I made a code but when I upload a code on website and try to run it shows error this error:
Parse error: syntax error, unexpected T_ELSE in line 17
Here is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en")
{
if ($this->agent->is_mobile())
{
$this->load->view('m_english_signup');
}
}
else
{
$this->load->view('d_english_signup');
}
else
$this->agent->is_mobile()
{
$this->load->view('m_arabic_signup');
}
else {
$this->load->view('d_arabic_signup');
}
}
}
Upvotes: 1
Views: 1410
Reputation: 596
If single line in if else then there is no {}
like this
if(condition)
$this->agent->is_mobile(); //statement
else
$this->agent->is_mobile(); //statement
otherwise
if(condition)
{
$this->agent->is_mobile(); //statement
}
else
{
$this->agent->is_mobile(); //statement
}
Upvotes: 1
Reputation: 845
You cannot have if-else-else-else logic sequence. The valid is
if-elseif-elseif-...-else
or
if-elseif-elseif
or
if-else
Beside that, your second 'else' was not properly enclosed with brackets.
Upvotes: 0
Reputation: 821
That error due to unexpected else condition or not properly closed.There are one else not properly closed.And the usage of if else in this code is incorrect and confusing.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->agent->is_mobile())
{
if ($this->input->get("lang") =="en"){
$this->load->view('m_english_signup');
}else{
$this->load->view('m_arabic_signup');
}
}
else
{
if ($this->input->get("lang") =="en"){
$this->load->view('d_english_signup');
}else{
$this->load->view('d_arabic_signup');
}
}
}
}
Please note that i cant identify what conditions you want.I just clear that error occured.
Upvotes: 1
Reputation: 6938
Your if-else statements are totally messed up :
class Welcome extends CI_Controller {
public function index(){
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en"){
if ($this->agent->is_mobile()) {
$this->load->view('m_english_signup');
}else {
$this->load->view('d_english_signup');
}
}else{
if($this->agent->is_mobile()) {
$this->load->view('m_arabic_signup');
}else{
$this->load->view('d_arabic_signup');
}
}
}
}
Upvotes: 1
Reputation: 3329
try this
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en"){
if ($this->agent->is_mobile()) {
$this->load->view('m_english_signup');
} else {
$this->load->view('d_english_signup');
}
} else if($this->agent->is_mobile()) {
$this->load->view('m_arabic_signup');
} else {
$this->load->view('d_arabic_signup');
}
}
}
Upvotes: 2
Reputation: 442
Try this
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en"){
if ($this->agent->is_mobile()) {
$this->load->view('m_english_signup');
}
else {
$this->load->view('d_english_signup');
}
}
else if($this->agent->is_mobile())
{
$this->load->view('m_arabic_signup');
}
else {
$this->load->view('d_arabic_signup');
}
}
}
Upvotes: 1
Reputation: 30565
Try this code. It's a bit cleaner and has indenting which makes it easier to see what's going on.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->agent->is_mobile()) {
if ($this->input->get("lang") =="en"){
$this->load->view('m_english_signup');
}else{
$this->load->view('m_arabic_signup');
}
}else {
if ($this->input->get("lang") =="en"){
$this->load->view('d_english_signup');
}else{
$this->load->view('d_arabic_signup');
}
}
}
}
Upvotes: 1
Reputation: 1011
You again made mistake in placing the code for mobile check. Try this:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") == "en") {
if ($this->agent->is_mobile()) {
$this->load->view('m_english_signup');
} else {
$this->load->view('d_english_signup');
}
} else {
if ($this->agent->is_mobile()) {
$this->load->view('m_arabic_signup');
} else {
$this->load->view('d_arabic_signup');
}
}
}
}
Upvotes: 1