2013-01-17 22 views
0
//Anyone can help to create a view data with same id? it is a multiple viewing. 

这是我的控制器。我不KHOW适用于Model和View查看同一用户ID

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=1 name=erwin user_id=102 
id=2 name=flores user_id=102 
id=3 name=sample user_id=202 

如何查看同一USER_ID?

+0

首先它不是一个明确的问题。但如果你想知道如何应用模型和视图,请查看一些教程。 –

+0

我编辑我的问题,请刷新它 – Naksuriya

+0

仍然不清楚你想在这里实现... –

回答

1

首先,您提供的网址无法正常工作,您没有遵循CI的常规约定,因此无法知道去哪里寻找。我假设你的控制器被称为示例,那么你需要告诉应用程序你在那个控制器中调用哪个函数,最后URL名称应该是小写,所以我改变了,所以你的URL应该是:

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

此外,您还需要从模型中获取数据,然后加载模型,然后不使用它。加载模型后的行调用该模型的函数,并将它从您的url中获得的id传递给它。注意如果id不是idset,这可以确保如果有人转到没有id段的那个页面,那么没有从具有缺少参数的模型抛出的错误,它将只返回任何东西,这在视图中处理。

控制器:

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并使用从数据库中检索数据。我通常创建数组,我将返回一个空数组,并在视图中处理该数组,这将确保在查询失败时不会发生错误。数据然后返回到最后一行中的控制器,并在您的加载视图调用中传递给视图。

型号:

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'; 
} 
+0

非常感谢你,先生:) – Naksuriya

+0

我有这个 – Naksuriya

+0

HTTP的问题://本地主机/ SMS_System /家庭/样品/ get_pitch/102" 什么是‘样品’ – Naksuriya