doukeng1922
2017-01-28 14:22
浏览 53
已采纳

使用codeigniter不显示数据库中的值

I'm new using codeigniter. I'm trying to display items from my database, but I don't know why it doesn't work. Could you help me, please?

My Controller

function Usuario extends CI_Controller{
    function construct(){
        parent::__construct();
        $this->load->model('usuario_m');
    }
    public function modificar_usuario(){
        $id_usuario = 1;
        $this->load->model('usuario_m');
        $data['resultado'] = $this->usuario_m->ver_datos_usuario($id_usuario);
        $this->load->view('paginas/modificar_usuario',$data);

    }

This is my Model:

public function ver_datos_usuario($id_usuario)
{

    $this->db->select('nombre','apellido','nro_ci', 'direccion','email','telefono','password');
    $this->db->where('id_usuario', $id_usuario);
    $this->db->from('usuarios');
    $query = $this->db->get();
    return $resultado = $query->result();

}

And this is my view: (Edited)

<!DOCTYPE>
<head>
<html>
<body>
<?php 
foreach ($data as $d){?>
    <label>Nombre: </label>
    <input type="text" value="<?php echo $d->nombre;?>" />
<?php } ?> 
</body>
</html>

What I'm doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我是使用codeigniter的新手。 我正在尝试显示数据库中的项目,但我不知道 知道为什么它不起作用。 请你帮我吗?

我的控制器

 功能Usuario扩展CI_Controller {
  function construct(){
 parent :: __ construct(); 
 $ this-&gt; load-&gt; model('usuario_m'); 
} 
 public function modificar_usuario(){
 $ id_usuario = 1;  
 $ this-&gt; load-&gt; model('usuario_m'); 
 $ data ['resultado'] = $ this-&gt; usuario_m-&gt; ver_datos_usuario($ id_usuario); 
 $ this-&gt;  load-&gt; view('paginas / modificar_usuario',$ data); 
 
} 
   
 
 

这是我的模型: \ n

  public function ver_datos_usuario($ id_usuario)
 {
 
 $ this-&gt; db-&gt; select('nombre','apellido','nro_ci','direccion','email  ','telefono','password'); 
 $ this-&gt; db-&gt; where('id_usuario',$ id_usuario); 
 $ this-&gt; db-&gt; from('usuarios');  
 $ query = $ this-&gt; d  b-&gt; get(); 
返回$ resultado = $ query-&gt; result(); 
 
} 
   
 
 

这是我的观点 :(已编辑)

 &lt;!DOCTYPE&gt; 
&lt; head&gt; 
&lt; html&gt; 
&lt; body&gt; 
&lt;?php 
foreach($ data as $  d){?&gt; 
&lt; label&gt; Nombre:&lt; / label&gt; 
&lt; input type =“text”value =“&lt;?php echo $ d-&gt; nombre;?&gt;”  /&gt; 
&lt;?php}?&gt;  
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

我做错了什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtfpznrbn503027700 2017-01-28 14:30
    已采纳

    Change this

    foreach ($data as $d){?>
    

    as following

    foreach($resultado as $d){?>
    

    because you are sending resultado to view in $data array so now $resultado can be access as returned result from model

    One more suggestion

    Instead return result like return $resultado = $query->result(); use this return $query->result();

    点赞 打赏 评论
  • dslk6326846 2017-01-28 14:48

    Just for fun.Read docs which is about codeigniter result's format.here https://www.codeigniter.com/userguide3/database/results.html.

    So that you can also achieve like this...

    In view:

    foreach ($resultado as $d){?>
        <label>Nombre: </label>
        <input type="text" value="<?php echo $d['nombre'];?>" />
    <?php } ?> 
    

    In model:

    Change return $resultado = $query->result(); to return $query->result_array();

    Notes:

    1.result_array() returns result in array format.

    2.result() returns result in object format.

    点赞 打赏 评论
  • douluohan3403 2017-01-28 15:54

    Instead of using $data in the foreach iteration use $resultado. Cheers

    点赞 打赏 评论

相关推荐 更多相似问题