简体   繁体   English

从数据库中获取数据-PHP Codeigniter

[英]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 使用foreach循环检索数据

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. 您需要这样做,因为codeigniter尝试从$ record中提取变量,但它失败了。 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 因为codeigniter将从$ record数组中提取变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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