简体   繁体   中英

fetching data from database - 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

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>

You need to pass your model data into record array then pass into view

Controller

 $student_id = $this->uri->segment(3);
     $record['data'] = $this->WebAdmin_Model->Student_Details($student_id);// store data into record array

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

Use foreach loop to retrieve data

Views

 <?php 
    foreach($data as $record)// use foreach loop
    { ?>
    <li> <?php echo $record->std_fname; ?></li>
    <li> <?php echo $record->std_lname; ?></li>
    <li> <?php echo $record->std_email; ?></li>
    <?php } ?>

You need to do it like this, because codeigniter try to extract variable from $record and it fails. So to make possible, make it,

 $record['record'] = $this->WebAdmin_Model->Student_Details($student_id);

and then in your view,

<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>

Because codeigniter will extract variable from $ record array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM