从数据库中获取单个记录并使用Codeigniter在文本框中显示

时间:2015-10-21 09:42:55

标签: html codeigniter

我在数据库中有多条记录。 当我点击任何ID时,所有记录都应该使用Codeigniter显示在文本框中。 当我点击用户ID页面时,我有两个选项卡自动刷新,我在URL中获取用户ID并且选项卡重定向到第一个选项卡。

我不想这样。我只有在点击用户标识

时才能在第二个标签上显示记录

enter image description here 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;
}

型号

public function update_retrive($userId)
{
$this->db->where('userId',$userId);
$this->db->from('add_user');
$q = $this->db->get();
return $q;
}

查看

<div id="btnclick" style="display: none">
<?php
foreach ($posts->result() 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" >
<input type="submit" class="submit" name="cancel" value="Cancel" >
<?php }
?>
</div>

2 个答案:

答案 0 :(得分:0)

在控制器中

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;
$this->load->view('yourview',$data);
}
在您的视图中

<div id="btnclick" 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" >
<input type="submit" class="submit" name="cancel" value="Cancel" >
<?php }
?>
</div>

答案 1 :(得分:0)

你模型上的

应该是这样的

public function update_retrive($userId)
{
$this->db->where('userId',$userId);
$this->db->from('add_user');
$q = $this->db->get();
return $q->result();// this will get all the result inside the database
}

将在您看来轻松。

控制器应该是这样的

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;
$this->load->view('yourview',$data);
}

控制器回答:@ricky

注意:重新检查您尝试访问的页面。