Codeigniter 查看相同的用户id

Codeigniter 查看相同的用户id,codeigniter,Codeigniter,这是我的控制器。我不知道如何在模型和视图中应用 //Anyone can help to create a view data with same id? it is a multiple viewing. 在我的数据库中 function Get_Pitch($id){ $this->load->model('users_model'); $data['query'] = $id; $this->l

这是我的控制器。我不知道如何在模型和视图中应用

//Anyone can help to create a view data with same id? it is a multiple viewing.
在我的数据库中

 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"

如何查看同一用户id?

首先,您提供的URL不起作用,因为您没有遵循CI的正常约定,所以它不知道在哪里查找。我假设您的控制器名为sample,然后您需要告诉应用程序您在该控制器中调用的函数,最后URL名称应为小写,因此我更改了该名称,因此您的URL应为:

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

此外,您还需要从模型中获取数据,您加载了模型,但没有使用它。加载模型后的行调用该模型中的函数,并将从url获得的id传递给它。请注意,如果id上未设置if not,这将确保如果有人在没有id段的情况下访问该页面,则不会从缺少参数的模型中抛出错误,它只会返回在视图中处理的任何内容

控制器:

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

您的模型获取从控制器传递的id,并使用该id从数据库检索数据。我通常创建将返回的数组作为空数组,并在视图中处理它,这样可以确保在查询失败时不会出现错误。然后,数据返回到最后一行中的控制器,并在load view调用中传递到视图

型号:

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);  
然后,您的视图会获取通过控制器从模型接收到的数据,如果存在,则会显示该数据;如果不存在该数据,则会显示一个错误,说明用户不存在。 视图:


首先,这不是一个明确的问题。但如果你想知道如何应用模型和视图,请查看一些相关教程。我编辑了我的问题,请刷新它,直到不清楚你想在这里实现什么…我想实现。是查看所有用户_id=102。对不起:)我可以问rick吗
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;
}
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';
}