【问题标题】:CODEIGNITER, Reference data from 2 tables to show in view fileCODEIGNITER,来自 2 个表的参考数据以显示在视图文件中
【发布时间】:2021-12-29 02:39:56
【问题描述】:

试图交叉引用 2 个表中的数据,只显示其中 1 个表中的数据...

如果表 001 中的“区”与表 002 中的“区”匹配,那么我想从表 002 中回显相应的“协调员”

表 001

|  month_of_report  |  district  |    org     |  
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|      January      |   004     |    Dallas  |
|      February     |   029     |    Dallas  |
|      March        |   047     |    Dallas  |

表 002

|  coordinator  |  district  |
- - - - - - - - - - - - - - - 
|      Jamie    |   004      |
|      Susie    |   047      |
|      Jimmy    |   029      |

型号

    public function reg_co()
    {
        $this->db->where('report_month', $report_month);
        $query = $this->db->get('non_clinical_total_tests');

        $this->db->select('*');
        $this->db->from('coordinator');
        $this->db->join('non_clinical_total_tests', 'non_clinical_total_tests.district = coordinator.district');

        if($query->num_rows() > 0)
        {
            return $query->row();
        }

    }

控制器

          $data['reg_co'] = $this->Page_model->reg_co();

查看文件

            <?php if($reg_co) : ?>
                  <strong>DISTRICT: </strong><?php echo $reg_co->district; ?><br>

期望的结果

如果 2 个表中的地区匹配,那么我应该能够回应该地区的协调员。

【问题讨论】:

    标签: codeigniter datatables


    【解决方案1】:
    ________________________________________________________________________________
    //This code is for single record from database
    
    //Model function
    public function reg_co()
    {
    
        $this->db->select('non_clinical_total_tests.*');
        $this->db->from('non_clinical_total_tests');
        $this->db->join('coordinator', 'coordinator.district = non_clinical_total_tests.district');
        $this->db->where('non_clinical_total_tests.report_month', $report_month);
        $this->db->limit(1);
        if($this->db->num_rows() > 0)
        {
            return $this->db->get()->row();
        }
        return false;
    }
    
    //View file
    
    <?php if($reg_co) : ?>
        <strong>DISTRICT: </strong><?php echo $reg_co->district; ?><br>
    
    
    
    _____________________________________________________________________________________
    
    //This code is for all records from database
    
    //Model function
    public function reg_co()
    {
    
        $this->db->select('non_clinical_total_tests.*');
        $this->db->from('non_clinical_total_tests');
        $this->db->join('coordinator', 'coordinator.district = non_clinical_total_tests.district');
        $this->db->where('non_clinical_total_tests.report_month', $report_month);
        if($this->db->num_rows() > 0)
        {
            return $this->db->get()->result_array();
        }
        return false;
    }
    
    //View file
    
    <?php if($reg_co) : ?>
        <?php foreach($reg_co as $reg) { ?>
            <strong>DISTRICT: </strong><?php echo $reg['district']; ?><br>
        <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多