Welcome Guest, Not a member yet? Register   Sign In
How to handle eror message in controller ?
#1

[eluser]Edy S[/eluser]
Hi Everybody, i have a problem with my controller programs.
Here's my code :
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {


      function __construct(){
      session_start();
      parent::__construct();
    }
    
    public function index()
    {    
        //load config file
        $this->load->model('conf');
        $setting = $this->conf->webconfig();
        $query   = $this->db->query('SELECT * FROM myTable WHERE FrontPage="1"');
        $data['prm']    = '';
        $data['date']    = $setting['date'];
        $data['home']    = $query->row();
        $this->load->view('template_1',$data);
    }
    
    public function pages($id)
    {    
        $query = $this->db->query("SELECT * FROM myTable WHERE myKey='".$id."'");
        $data['pages']= $query->result();
        $data['prm']  = '';
        $data['mode'] = 'pages';
        $this->load->view('template',$data);
    }

}

It will display error message and disturb the website display when people visit http://www.mysite.com/home/pages/ without insert a value for $id
And my question now, How to handle an empty $id, so that it will not display an error message ?
#2

[eluser]Fireclave[/eluser]
Code:
public function pages($id = false)
    {
        if($id)
        {
            $query = $this->db->query("SELECT * FROM myTable WHERE myKey='".$id."'");
            $data['pages']= $query->result();
            $data['prm']  = '';
            $data['mode'] = 'pages';
            $this->load->view('template',$data);
        }
        else
        {
            // Do something
        }

    }

What do you thing about this ?

You can set an default value (in this case false). Then you check the value by an if statement.
#3

[eluser]CI_adis[/eluser]
Solution also mentioned here: http://stackoverflow.com/questions/20138...odeigniter
#4

[eluser]Edy S[/eluser]
[quote author="Fireclave" date="1313263751"]
Code:
public function pages($id = false)
    {
        if($id)
        {
            $query = $this->db->query("SELECT * FROM myTable WHERE myKey='".$id."'");
            $data['pages']= $query->result();
            $data['prm']  = '';
            $data['mode'] = 'pages';
            $this->load->view('template',$data);
        }
        else
        {
            // Do something
        }

    }

What do you thing about this ?

You can set an default value (in this case false). Then you check the value by an if statement.[/quote]

Hi Fireclave, thanks for your post ...
It work well, now that i can redirect to other page while found an empty $id.
To CI_adis i've read it
Code:
note that this is just not in codeigniter but throughout php in case you were wondering
I thought that i should learn more about php function Smile
Thanks for you all Smile




Theme © iAndrew 2016 - Forum software by © MyBB