如何在CodeIgniter中点击用户ID后从数据库获取记录?

3

当点击编辑图标(铅笔图标)后,我能够从数据库中检索记录,但我正在获取在模型中编写的特定id详细信息。如果我们单击编辑图标,则会打开一个弹出窗口,并显示特定用户id的记录。例如下面的图像,如果我单击用户id 3,则记录将被显示,如果我单击用户id 4,则记录将被显示,用户id 5也是一样的。请在这方面给我帮助。

提前致谢。 enter image description here enter image description here

控制器

public function update_retrive()
{
$id = $this->uri->segment(3);
$this->load->model("model_add_user");
$d=$this->model_add_user->update_retrive($id);
$data['posts'] = $d;

}

Model

public function update_retrive($id)
{
$this->db->where('userId',$id);/* I have to select multiple userid but how?*/
$this->db->from('add_user');
$q = $this->db->get();
return $q->result();
}

查看

<div  class="table-responsive">
   <table class="table table-bordered" >  
        <thead>
         <tr>  


          <td><b></b></td>
          <td><b></b></td>
            <td><b>userId</b></td>  

            <td><b>firtsname</b></td>  

            <td><b>lastname</b></td>  
            <td><b>mobileno</b></td>  


            <td><b>address</b></td> 
            <td><b>city</b></td>  

            <td><b>email</b></td> 

         </tr> 
          </thead>

         <?php  
         foreach ($user->result() as $row)  
         {  
            ?>

              <tbody><tr >  



        <td><a href="<?php echo site_url('Welcome/delete/'.$row->userId)?>"><img src="http://localhost/CRM/img/delete.png" name="delete_image" ></a></td>
       <!-- <td><a href="<?php echo site_url('Welcome/update/'.$row->userId)?>"><img src="http://localhost/CRM/img/edit.png" name="edit_image" ></a></td>-->
        <td><a id="btnclick" href="<?php echo site_url('Welcome/update_retrive/'.$row->userId)?>"><img src="http://localhost/CRM/img/edit.png" name="edit_image" ></a></td>


            <td><?php echo $row->userId;?></td>  

            <td><?php echo $row->firtsname;?></td>    

            <td><?php echo $row->lastname;?></td>  
            <td><?php echo $row->mobileno;?></td>  


            <td><?php echo $row->address;?></td> 
            <td><?php echo $row->city;?></td> 

            <td><?php echo $row->email;?></td> 

            </tr>   </tbody> 
         <?php }  

         ?>  

   </table>  

   <form id="form1">

<div id="popupdiv"  style="display: none">

   <?php foreach($posts as $post){?>

    <input type="text" name="userId" placeholder="USER ID" style="width:200px" value='<?php echo $post->userId;?>'>&nbsp;


    <input type="date" name="date" style="width:200px" value='<?php echo $post->date;?>'>
    <input type="text" name="firtsname" placeholder="FIRST NAME" style="width:200px" value='<?php echo $post->firtsname;?>'>&nbsp;
    <input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:200px" value='<?php echo $post->middlename;?>'>&nbsp;
    <input type="text" name="lastname" placeholder="LAST NAME" style="width:200px" value='<?php echo $post->lastname;?>'>&nbsp;

    <input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:200px" value='<?php echo $post->mobileno;?>'>&nbsp;
    <input type="text" name="landline" placeholder="LANDLINE No." style="width:410px" value='<?php echo $post->landline;?>'>
    <textarea name="address" placeholder="ENTER ADDRESS" style="width:410px" ><?php echo $post->address;?></textarea>

    <input type="text" name="city" placeholder="CITY" style="width:200px" value='<?php echo $post->city;?>'>&nbsp;
    <input type="text" name="locality" placeholder="LOCALITY" style="width:200px" value='<?php echo $post->locality;?>'>
    <input type="text" name="email" placeholder="ENTER EMAIL" style="width:410px" value='<?php echo $post->email;?>'></br>

  <input type="submit" class="submit" name="UPDATE" value="UPDATE" >
<?php }
         ?>
</div>

</form>

  </div>

你能在弹出窗口中获取特定的值吗? - Indrasinh Bihola
使用Ajax获取与您点击的ID相对应的结果。 - Vinie
抱歉今天回复晚了,Stack Overflow网站非常慢。Indrasinh Bihola先生,如果我在代码中指定用户名,我可以从数据库中检索数据。您可以查看上面的图片。但是我不想指定用户名。 - user6930268
2个回答

2
您需要在URL中发送ID值。 尝试像这样:
<a href = "<?php echo site_url('Welcome/update_retrive');?>/<?php echo 'your id'";?>">edit</a>

在您的控制器中创建一个函数。
    public function update_retrive()
    {
    $id = $this->uri->segment(3);
    $this->load->model("model_add_user");
    $d=$this->model_add_user->update_retrive($id);
    $data['posts'] = $d;
}

现在在你的模型中。
public function update_retrive($id)
{

$this->db->where('userId',$id);/* I have to select multiple userid but how?*/
$this->db->from('add_user');
$q = $this->db->get();
return $q->result();
}

如果您不想将ID作为URL发送,也可以使用AJAX获取数据。http://w3code.in/2015/09/how-to-insert-and-view-data-without-refreshing-page-using-ajax-and-jquery-in-codeigniter

好的,我已经根据你的要求更新了我的答案。尝试在你的控制器中使用URI段获取URL中的ID。 - Ricky
请分享代码。这将对我非常有帮助。 - user6930268
Ricky先生,我觉得您没有理解我的问题。 - user6930268
@Hybreeder,您需要在每次点击时将ID作为参数发送到控制器,才能从数据库中获取数据。为此,您需要动态地将ID存储在编辑锚点标记中。 - Ricky
绝对正确。就像上面的代码一样,当我点击userId时,我会在url上得到userid,这很烦人,因为弹出窗口没有显示。 - user6930268
显示剩余9条评论

1
Controller:

  $id = $this->input->post('id');

   $data = array("id" => $this->input->post('id'),
                    "****" => $this->input->post('***'),
                    "*****" => $this->input->post('***'),
                    "*****" => $this->input->post('***'),
                    "*****" => $this->input->post('***')
                       );


    $this->load->model('court_page_model');
    $result = $this->court_page_model->update_id($id, $data);


Model:


 public function update_id($id, $data)
{

$this->db->where('***', $id);
$this->db->update('****', $data);
if ($this->db->affected_rows() > 0) {
        return true;

}   

Kumar先生,我应该在哪里输入控制器代码?我应该在index中输入还是创建新函数? - user6930268
你的选择应该在两个函数中都能运行。 - B.Nadesh kumar
先生,我尝试了您的代码,我在控制器中创建了一个新函数并添加了您的代码,但是我遇到了错误:“未定义变量:posts”“文件名:views/index.php”。我必须在文本框中显示记录,请检查我上传的图片。 - user6930268
我必须从数据库中获取数据,在弹出窗口中根据用户单击事件显示在文本框中,以便我可以更新记录。 - user6930268
在上面的图像中,我点击了userId 3,它显示了弹出窗口中的所有记录。同样的事情我需要点击4、5等,所有数据应该根据userId显示。 - user6930268

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接