douhe6181 2016-06-27 10:40
浏览 44

从数据库中获取数据 - php codeigniter

I want to fetch some data (student details) from database but my code is not working.

This is my Controller

   public function Student_Detail()
   {
     $student_id = $this->uri->segment(3);
     $record = $this->WebAdmin_Model->Student_Details($student_id);

     $this->load->view('admin/template/header.php');
     $this->load->view('admin/students/student_details', $record);
     $this->load->view('admin/template/footer.php');   

   }

This is my Model

 public function Student_Details($student_id)
    {
    $query = $this->db->query("SELECT s.`student_id`, 
                                      s.`std_email`, 
                                      s.`std_fname`, 
                                      s.`std_lname`
                               FROM `student_main` AS s
                               WHERE s.`student_id` = $student_id");
 return $query->result();  
    }

This is my View

     <section class="panel">
     <div class="user-heading">

    <img src="<?=base_url();?>images/profile-avatar.jpg" alt="">

     </div>

<ul class="nav nav-pills nav-stacked">
<li> <?php echo $record->std_fname; ?></li>
<li> <?php echo $record->std_lname; ?></li>
<li> <?php echo $record->std_email; ?></li>
</ul>
 </section>

Note that there is not problem with the query. I want to know how to fetch student details. It gives me the following error. Message : Undefined variable: record

  • 写回答

3条回答 默认 最新

  • doutan8775 2016-06-27 10:44
    关注

    Try this:

    In your controller:

    $data['record'] = $this->WebAdmin_Model->Student_Details($student_id);
    $this->load->view('admin/students/student_details', $data);
    

    And in your view:

    <ul class="nav nav-pills nav-stacked">
    <?php
    foreach($record->$row){
    ?>
    <li> <?php echo $row->std_fname; ?></li>
    <li> <?php echo $row->std_lname; ?></li>
    <li> <?php echo $row->std_email; ?></li>
    <?php
    }
    ?>
    </ul>
    
    评论

报告相同问题?