Welcome Guest, Not a member yet? Register   Sign In
codeigniter function
#1

[eluser]Unknown[/eluser]
I created codeigniter controller class as follows

class Poems extends CI_Controller{

function __construct() {
parent::__construct();
$this->load->model('poem');
}

and then created method poem inside the above class as follows

function poem($poemID){
$data['poem']= $this->poem->get_poem($poemID);
$this->load->view('poem',$data);

}

/*******************/ for the above controller class I created in the model get_poem() method as follows

function get_poem(){
$this->db->select()->from('poems')->where(array('active'=>1,'poemID'=>$poemID))->order_by('date_added', 'desc');
$query= $this->db->get();
return $query->first_row('array');
}


When I run the poems in the browser it works fine... When I want to load the poem method as poems/poem in the browser the following error appears.

error 1: Missing argument 1 for Poems::poem()
error 2: Undefined variable: poemID

how can I fix these errors
#2

[eluser]CroNiX[/eluser]
Your poem method requires an ID, so your url should be
www.yoursite.com/poems/poem/43

Going to www.yoursite.com/poems/poem would cause the issue you are describing since you didn't pass an ID. You should check for this before assuming the ID was passed and trying to use it.

Something like:
Code:
function poem($poemID = null){
  if (is_null($poemID))
  {
    //show error
  }
  else
  {
    //normal stuff
    $data[‘poem’]=  $this->poem->get_poem($poemID);

    //You might want to make sure that the ID existed by checking to see if you actually received data from get_poem(). Try passing an ID that doesn't exist.
  }
}
#3

[eluser]Unknown[/eluser]
still didn't work




Theme © iAndrew 2016 - Forum software by © MyBB