Codeigniter - 如何将多个表中的多个数据传递到单个View?

时间:2017-03-03 13:06:01

标签: php mysql database codeigniter

我在数据库中有表格。

ng-model=$parent.value[sourceSrNo]; ng-model=value[sourceSrNo]; ng-model={{$parent.value[sourceSrNo]}}; ng-model={{value[sourceSrNo]}}; ng-model=$parent.value.sourceSrNo; ng-model=value.sourceSrNo; ng-model={{$parent.value.sourceSrNo}}; ng-model={{value.sourceSrNo}}; ng-model="$parent.value[sourceSrNo]"; ng-model="value[sourceSrNo]"; ng-model="{{$parent.value[sourceSrNo]}}"; ng-model="{{value[sourceSrNo]}}"; ng-model="$parent.value.sourceSrNo"; ng-model="value.sourceSrNo"; ng-model="{{$parent.value.sourceSrNo}}"; ng-model="{{value.sourceSrNo}}"; 专栏:

tbl_parameter

接下来是parameter_ID | parameter_name | parameter_unit | parameter_method ------------ | -------------- | -------------- | ---------------- 3001 | Salt | % | Titrimetri 3002 | pH | - | pH meter 3003 | Visco | poise | Viscometer 来存储COA页面输入。 tbl_coa来自多选输入,因此它以逗号分隔。

parameter_ID

接下来,我正在使用显示COA详细信息的View页面,该视图保存为coa_ID | coa_number | coa_year | product_ID | customer_ID | parameter_ID ------ | ----------- | -------- | ---------- | ----------- | ------------ 4001 | 001/01/2017 | 2017 | 10001 | 2111 | 3001,3002,3003 。代码如下。我正在定义view_coa.php来替换{PRE}

模型tbl_

Coa_model.php

Controller function get_coa_detail($coa_ID){ $this->db->select('*'); $this->db->from('{PRE}coa'); $this->db->join('{PRE}product', '{PRE}product.product_ID = {PRE}coa.product_ID', 'LEFT'); $this->db->join('{PRE}customer', '{PRE}customer.customer_ID = {PRE}coa.customer_ID', 'LEFT'); $this->db->join('{PRE}parameter', '{PRE}parameter.parameter_ID = {PRE}coa.parameter_ID', 'LEFT'); $this->db->where('{PRE}coa.coa_ID', $coa_ID); $query = $this->db->get(); if($query->num_rows() > 0){ $data = $query->row(); $query->free_result(); } else{ $data = NULL; } return $data; }

Coa.php

问题是,当我想显示参数详细信息(以逗号分隔public function view_coa($coa_ID=0){ if($coa_ID != 0 && !empty($coa_ID)){ $data = array( 'record' => $this->Coa_model->get_coa_detail($coa_ID) ); //print_r($data); $this->site->view('view_coa', $data); } else{ redirect(set_url('coa/data_table/'.date('Y'))); } } 存储)时,它只显示了来自第一个parameter_ID的数据。在上面的示例中,它只显示了parameter_ID的详细信息。我想显示所有parameter_ID = 3001(3001,3002和3003)中的数据(parameter_nameparameter_unitparameter_method)。

parameter_ID数据结果:

print_r

我的问题是,如何显示Array ( [record] => stdClass Object ( [coa_ID] => 4001 [coa_number] => 001/01/2017 [coa_year] => 2017 [product_ID] => 10001 [product_type] => Sweet Soy [product_name] => Product A [Salt] => 5.5 [pH] => 4.4 [Visco] => 12.5 [customer_ID] => 2111 [customer_name] => Company 1 [parameter_ID] => 3001 [parameter_name] => Salt [parameter_unit] => % [parameter_method] => Titrimetri ) ) [parameter_ID] => 3002的所有参数详情?

显示数据库数据的当前视图[parameter_ID] => 3003

view_coa

1 个答案:

答案 0 :(得分:2)

在函数get_coa_detail中,您只从结果集中获取第一行。

 if($query->num_rows() > 0){
        $data = $query->row();
        $query->free_result();
    }
    else{
        $data = NULL;
    }

如果要返回所有行,请使用下面的内容并在视图中迭代它

if($query->num_rows() > 0){
        $data = $query->result();
        $query->free_result();
    }
    else{
        $data = NULL;
    }

要修复视图,您可以执行以下操作:

在您的控制器中,更改包含数据的变量名称(这样您就可以使用视图代码进行最少的修改)。

所以我在Coa.php中更改了这个

 $data = array(
                'coa' => $this->Coa_model->get_coa_detail($coa_ID)
            );

并在视图文件中执行以下修改:

<?php foreach ($coa as $record): ?>
    <!-- COA title and number row -->
    <div class="row">

并将其关闭:

<?php endforeach;?>

编辑: 以下是我使用的示例代码:

控制器:Coa.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Coa extends CI_Controller {

    public function index() {

        $this->view_coa(4001);
    }

    public function view_coa($coa_ID = 0) {

        $this->load->model('coa_model');

        if ($coa_ID != 0 && !empty($coa_ID))
        {
            $data['record'] = $this->coa_model->get_coa_detail($coa_ID);
            $data['parameters'] = $this->coa_model->get_parameter($data['record']->parameter_ID);

            $this->load->view('view_coa', $data);
        } else
        {
            redirect(set_url('coa/data_table/' . date('Y')));
        }
    }

}

这是模型Coa_model.php

<?php if (!defined('BASEPATH'))    exit('No direct script access allowed');

class Coa_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function get_coa_detail($coa_ID) {

        $pre = 'tbl_';

        $this->db->select('*');
        $this->db->from($pre . 'coa');
        $this->db->join($pre . 'product', $pre . 'product.product_ID  = tbl_coa.product_ID', 'LEFT');
        $this->db->join($pre . 'customer', $pre . 'customer.customer_ID  = tbl_coa.customer_ID', 'LEFT');
        //  $this->db->join($pre . 'parameter', $pre . 'parameter.parameter_ID  = tbl_coa.parameter_ID', 'LEFT');
        $this->db->where($pre . 'coa.coa_ID', $coa_ID);
        $query = $this->db->get();

        if ($query->num_rows() > 0)
        {
            $data = $query->row();
            $query->free_result();
        } else
        {
            $data = NULL;
        }

        return $data;
    }

    /**
     *   This get's all the parameters
     * 
     * @param string $parameter_ID
     * @return object
     */
    public function get_parameter($parameter_ID = NULL) {

        // Delimiter
        $delimiter = ',';

        // Check the position of the first delimiter,
        // if there are no delimiters this will return false
        $multiple = strpos($parameter_ID, $delimiter);

        // Table prefix
        $pre = 'tbl_';

        $this->db->select('*');

        if ($multiple !== FALSE)
        {
            // We have multiple parameters delimited by $delimiter
            $params = explode($delimiter, $parameter_ID);

            $this->db->where_in('parameter_ID', $params);
        } else
        {
            // We have a single parameter
            $this->db->where('parameter_ID', $parameter_ID);
        }

        $this->db->from($pre . 'parameter');
        $query = $this->db->get();

        if ($query->num_rows() > 0)
        {
            $data = $query->result();
            $query->free_result();
        } else
        {
            $data = NULL;
        }

        return $data;
    }

}

这是视图文件:view_coa.php

<!-- COA title and number row -->
<div class="row">
    <div class="col-xs-12 coa-title-number">
        <h3>Certificate of Analysis</h3>
        <p>NO. <?php echo $record->coa_ID; ?></p>
    </div>
</div>

<!-- COA detail row -->
<div class="row">
    <div class="col-xs-1">
    </div>
    <div class="col-xs-11">
        <table class="table-coa-detail">
            <tr class="text-nowrap">
                <th>Product Name</th>
                <td><strong>: <?php echo $record->product_name; ?></strong></td>
            </tr>
            <tr class="text-nowrap">
                <th>Customer Name</th>
                <td><strong>: <?php
                        if ($record->customer_ID == "0" || $record->customer_ID == NULL)
                        {
                            echo "-";
                        } else
                        {
                            echo $record->customer_name;
                        }
                        ?></strong></td>
            </tr>
            <tr class="text-nowrap">
                <th>Analysis Results</th>
                <td>:</td>
            </tr>
        </table>
    </div>
</div>

<!-- COA results row -->
<div style="padding-right: 50px;" class="row">
    <div class="col-xs-1">
    </div>
    <div class="col-xs-11">
        <table class="table-results">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Parameter</th>
                    <th>Unit</th>
                    <th>Results</th>
                    <th>Method</th>
                    <th>Conclusion</th>
                </tr>
            </thead>
            <tbody class="text-nowrap">
                <?php $c = 1; // counter?>
                <?php foreach ($parameters as $params): ?>
                    <tr>
                        <td><?= $c; ?></td>
                        <td><?= $params->parameter_name; ?></td>
                        <td><?= $params->parameter_unit; ?></td>
                        <td>result would come here</td>
                        <td><?= $params->parameter_method; ?></td>
                        <td>OK</td>
                    </tr>
                    <?php $c ++; // increase row counter ?>
                <?php endforeach; ?>
            </tbody>
        </table>

    </div>
</div>

这是我在屏幕上看到的结果: enter image description here