计算并显示codeignator3中一行的结果

kmbjn2e3  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(260)

我有一个表,需要在同一列中减去两个值,然后在另一个视图表中显示差异。我需要记录5台压缩机的每日总工时计读数,并将差值显示为每日运行小时数。
我的表:crudtabe的图像输出

SELECT a.cuidad_id,
       a.bc100a,
       Coalesce(a.bc100a - (SELECT b.bc100a
                            FROM   cuidadrun b
                            WHERE  a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS
       diffbc100a
FROM   cuidadrun a

在phpmyadmin中,什么工作得很好,请参见图片:sql输出
我无法训练控制器、模型和视图以显示结果。另外,我可以将结果保存在另一个表中吗?
我的控制器

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

class Cactushrs extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Cactushrs_model');
    }
    public function index()
    {

        $this->load->view('template/header');
    $data = $this->Cactushrs_model->cactushrs();
    $this->load->view('pages/cactushrs_view', $data);
        $this->load->view('template/footer');
    }
}

我的模型

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

class Cactushrs_model extends CI_Model
{
  public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
  public function cactushrs()
  {

    $query = $this->db->get ('SELECT a.cuidad_id, a.bc100a, COALESCE(a.bc100a - (SELECT b.bc100a FROM cuidadrun b WHERE a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS differnce FROM cuidadrun a');

  }
}

我的观点

<table id="cuidadhrs" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
  <thead>
      <tr>
          <th>cuidad_id</th>
          <th>bc100a</th>
          <th>diff100</th>
      </tr>
  </thead>
  <tbody>
    <tr>
      <td><?php echo $this->db->get('differnce'); ?></td>
    </tr>
  </tbody>

小提琴https://www.db-fiddle.com/f/vsm6ha2wa3cprjaqeodmkz/0

ktecyv1j

ktecyv1j1#

我自己解决了我的问题。问题出在模型、控制器和视图上。
控制器.php

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

class Cuidadhrs extends CI_Controller {

    public function index()
    {

        $this->load->view('template/header');
        $this->load->model('Cuidadhrs_model');
        $hours['hours'] = $this->Cuidadhrs_model->cuidadhrs();
        $this->load->view('pages/cuidadhrs_view', $hours);
        $this->load->view('template/footer');
    }
}

模型.php

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

class Cuidadhrs_model extends CI_Model
{
  public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
  public function cuidadhrs()
  {

    $sql = "SELECT a.cuidad_id, a.bc100a, COALESCE(a.bc100a - (SELECT b.bc100a FROM cuidadrun b WHERE a.cuidad_id = b.cuidad_id + 1), a.bc100a) AS diffbc100a FROM cuidadrun a";
    $query = $this->db->query($sql);
    return $query->result_array();

  }
}

查看.php

<table id="cuidadhrs" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
      <thead>
        <th>ID</th>
        <th>BC-100a Hourmeter</th>
        <th>Daily Hrs.</th>
      </thead>
      <tbody>
        <?php foreach($hours as $hrs){ ?>
        <tr>
          <td><?php echo $hrs['cuidad_id']; ?></td>
          <td><?php echo $hrs['bc100a']; ?></td>
          <td><?php echo $hrs['diffbc100a']; ?></td>
        </tr>
      </tbody>
      <?php } ?>
      <?php $this->output->enable_profiler(TRUE); ?>
</table>

相关问题