【问题标题】:Codeigniter - How to passing multiple data from multiple table to a single View?Codeigniter - 如何将多个数据从多个表传递到单个视图?
【发布时间】:2017-03-03 13:06:00
【问题描述】:

我在数据库中有表。

tbl_parameter专栏:

parameter_ID | parameter_name | parameter_unit | parameter_method
------------ | -------------- | -------------- | ----------------
3001         | Salt           | %              | Titrimetri
3002         | pH             | -              | pH meter
3003         | Visco          | poise          | Viscometer

接下来是tbl_coa,用于存储 COA 页面输入。 parameter_ID 来自多选输入,所以以逗号分隔存储。

coa_ID | coa_number  | coa_year | product_ID | customer_ID | parameter_ID
------ | ----------- | -------- | ---------- | ----------- | ------------
4001   | 001/01/2017 | 2017     | 10001      | 2111        | 3001,3002,3003

接下来,我正在处理显示 COA 详细信息的视图页面,该视图保存为 view_coa.php。代码如下。我正在定义 {PRE} 来替换 tbl_

型号Coa_model.php

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 的详细信息。我想显示来自所有parameter_ID(3001、3002 和 3003)的数据(parameter_nameparameter_unitparameter_method)。

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 显示来自数据库的数据:

  <!-- 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->no_coa;?></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">
          <tr>
          <td style="text-align: center;">1</td>
          <td><strong>Analytical</strong>
            <?php 
            if($record->product_type == "Sweet Soy"){
              echo "<p>pH</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3002
              echo "<p>Visco</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3003
              echo "<p>Salt</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3001
            }
            else{
              echo "<p>pH</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3002
              echo "<p>Salt</p>"; // it should "<p>".$record->parameter_name."</p>"; from [parameter_ID] => 3001
            }
            ?>
          </td>
          <td style="text-align: center;">&nbsp;
            <?php 
            if($record->product_type == "Sweet Soy"){
              echo "<p>-</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3002
              echo "<p>poise</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3003
              echo "<p>%</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3001
            }
            else{
              echo "<p>-</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3002
              echo "<p>%</p>"; // it should "<p>".$record->parameter_unit."</p>"; from [parameter_ID] => 3001
            }
            ?>
          </td>
          <td style="text-align: center;">&nbsp;
            <?php 
            if($record->product_type == "Sweet Soy"){
              echo "<p>".$record->pH."</p>";
              echo "<p>".$record->Visco."</p>";
              echo "<p>".$record->Salt."</p>";
            }
            else{
              echo "<p>".$record->pH."</p>";
              echo "<p>".$record->Salt."</p>";
            }
            ?>
          </td>
          <td style="text-align: center;">&nbsp;
            <?php 
            if($record->product_type == "Sweet Soy"){
              echo "<p>pH meter</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3002
              echo "<p>Viscometer</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3003
              echo "<p>Titrimetri</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3001
            }
            else{
              echo "<p>pH meter</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3002
              echo "<p>Titrimetri</p>"; // it should "<p>".$record->parameter_method."</p>"; from [parameter_ID] => 3001
            }
            ?>
          </td>
          <td style="text-align: center;">&nbsp;
            <p>OK</p>
            <p>OK</p>
            <p>OK</p>
          </td>
          </tr>
        </tbody>
      </table>

    </div>
  </div>

【问题讨论】:

  • 检查这个答案请stackoverflow.com/a/2322511/6068342我认为它会帮助你
  • @ArmKh 我实际上知道implodeexplode 函数,我使用implode 将多选值存储到数据库(如上所述),我使用explode在编辑页面中。它对查看页面没有帮助..
  • @Firman 请按照Minimal, Complete, and Verifiable example 将其缩小到特定问题的最小表示。然后更新问题陈述,使其与实际代码相关。很难理解你的问题是什么
  • @AbdullaNilam 我已经简化了代码和问题。这可以解释我的问题吗?

标签: php mysql database codeigniter


【解决方案1】:

在函数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>

这是我在屏幕上看到的结果:

【讨论】:

  • 不工作,如果从$data = $query-&gt;row(); 更改为$data = $query-&gt;result();,我只会收到错误消息Message: Trying to get property of non-object
  • 您能否粘贴试图显示结果的视图部分?您需要使用 while 或 foreach 来显示每一行。类似于: = $row->parameter_name; ?>
  • 我已将视图部分粘贴到上述问题中。请查看应在何处进行更正。请注意。
  • 您好 Ferenc,您的回答似乎无效。首先,您说函数get_coa_detail 只从结果中获取第一行?是的,我的目的是只获取指定coa_ID 的行,这就是我使用get_coa_detail($coa_ID) 的原因。我可以使用$record-&gt;column_name 从每一列中获取值。唯一的问题是,对于具有值3001,3002,3003parameter_ID,我只能从3001 获取数据。我的问题是,如何从 _ID 3002,3003 获取数据。其次,我认为没有必要将变量名从record改为coa
  • 嗨,Firman,您的查询的基本问题是它在连接语句中使用了 parameter_ID,并且由于某些可怕的原因,mysql 不会使查询失败,但实际上只是采用第一个 parameter_ID 并通过它加入,所以它确实返回了一行。我建议你消除它。在您的视图文件中,您有 1 列列出参数。我所做的是创建了另一种获取每个参数并分别显示它们的方法。我会用代码更新我的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多