Naksuriya
Naksuriya

Reputation: 55

View Same user_id

//Anyone can help to create a view data with same id? it is a multiple viewing.

this is my Controller. i dont khow apply in Model and View

 function Get_Pitch($id){
            $this->load->model('users_model');

            $data['query'] = $id;

           $this->load->view('view_pitch', $data);  

        }

Example this is my url "http://localhost/SMS_System/home/sample/102"

in my database is

id=1 name=erwin user_id=102
id=2 name=flores user_id=102
id=3 name=sample user_id=202

how to view the same user_id?

Upvotes: 0

Views: 90

Answers (1)

Rick Calder
Rick Calder

Reputation: 18705

First of all with what you've supplied your URL won't work, you aren't following the normal conventions for CI so it won't know where to look. I am assuming your controller is called sample then you need to tell the application which function you're calling in that controller, finally URL names should be lower case so I changed that, so your URL should read:

"http://localhost/SMS_System/home/sample/get_pitch/102"

Also you need to get your data from a model, you loaded the model then didn't use it. The line after loading the model calls a function from that model and passes it the id you got from your url. Notice the if not isset on the id, this ensures that if someone goes to that page without the id segment there are no errors thrown from the model having a missing parameter, it will just return nothing, that is handled in the view.

Controller:

function get_pitch($id){
   //the following line gets the id based on the segment it's in in the URL
   $id=$this->uri_segment(3);
   if(!isset($id))
   {
      $id = 0;
   }
   $this->load->model('users_model');
   $data['query'] = $this->users_model->getUserData($id);
   $this->load->view('view_pitch', $data);  

}

Your model takes the id passed from the controller and uses that to retrieve the data from the database. I normally create the array I am going to return as an empty array and handle that in the view, this makes sure you get no errors if the query fails. The data then returns to the controller in the last line and is passed to the view in your load view call.

Model:

function getUserData($id)
{
    $this->db->where('id',$id);
    $result = $this->db->get('users') //assuming the table is named users 
    $data = array(); //create empty array so we aren't returning nothing if the query fails
    if ($result->num_rows()==1) //only return data if we get only one result
    {
      $data = $result->result_array();
    }
    return $data;
}

Your view then takes the data it received from the model via the controller and displays it if present, if the data is not present it displays an error stating the user does not exist. View:

if(isset($query['id']))
{
  echo $query['id']; //the variable is the array we created inside the $data variable in the controller.
  echo $query['name'];
  echo $query['user_id'];
} else {
  echo 'That user does not exist';
}

Upvotes: 1

Related Questions