猿问

如何从其他表中获取数据?

我想显示另一个表中的数据。这是CodeIgniter框架。


我尝试了很多代码,但它们不起作用。


这是我的代码:


<?php

foreach ($users->result_array() as $key => $user): ?>

   <tr>

      <td> <?php echo $key+1; ?></td>

      

      <td>

        <img src="<?php echo $this->user_model->get_user_image_url($user['id']);?>" alt="" height="50" width="50" class="img-fluid rounded-circle img-thumbnail">

      </td>


      <td> <?php echo $user['first_name'].' '.$user['last_name']; ?>

        <?php if($user['status'] != 1): ?>

          <small><p><?php echo get_phrase('status'); ?>: <span class="badge badge-danger-lighten"><?php echo get_phrase('unverified'); ?></span></p></small>

        <?php endif; ?>

      </td>


      <td>

        <?php echo $user['email']; ?>

      </td>


      <td>

        <?php echo $user['classcategory_id']; ?>

      </td>


      <td>

        <?php $enrolled_courses = $this->crud_model->enrol_history_by_user_id($user['id']);?>

          <ul>

            <?php foreach ($enrolled_courses->result_array() as $enrolled_course):

            $course_details = $this->crud_model->get_course_by_id($enrolled_course['course_id'])->row_array();?>

              <li><?php echo $course_details['title']; ?></li>

           <?php endforeach; ?>

         </ul>

     </td>

我怎样才能改变这个


 <td>

   <?php echo $user['classcategory_id']; ?>

 </td>

从表名称“classcategory”字段“name”中获取数据


我的控制器,用于用户和课程


public function get_courses() {

        if ($this->session->userdata('admin_login') != true) {

            redirect(site_url('login'), 'refresh');

        }

...

我的数据库就像这个表 - > CLASSCATEGORY:列:ID +名称表 - > USER:列:ID + classcategory_id


缥缈止盈
浏览 113回答 2
2回答

30秒到达战场

您可以使用 CLASSCATEGORY 和 SELECT CLASSCATEGORY.name AS 类别名称 JOIN 模型中的 USER 表,然后echo $user['categoryname'].&nbsp;以下是如何在 CodeIgniter 中连接两个表的示例&nbsp;&nbsp;&nbsp;&nbsp;$this->db->select('USER.*,&nbsp;CLASSCATEGORY.name&nbsp;AS&nbsp;categoryname') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->from('users') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->join('CLASSCATEGORY',&nbsp;'CLASSCATEGORY.id&nbsp;=&nbsp;USER.classcategory_id') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->get() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;->result();

qq_花开花谢_0

public function getallRecords($id,$product_id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->db->select('*');&nbsp; &nbsp; &nbsp; &nbsp; $this->db->from('products');&nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('id', $id);&nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('product_id', $product_id);&nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('show', 'no');&nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('Results !=', '0');&nbsp; &nbsp; &nbsp; &nbsp; $this->db->order_by('added', 'DESC');&nbsp; &nbsp; &nbsp; &nbsp; $this->db->limit(30);&nbsp; &nbsp; &nbsp; &nbsp; $getallRecords=$this->db->get();&nbsp; &nbsp; &nbsp; &nbsp; $resultllRecords=$getallRecords->result();&nbsp; &nbsp; &nbsp; &nbsp; return $resultllRecords;&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答