【问题标题】:Calculate value in codeigniter from mysql database从mysql数据库计算codeigniter中的值
【发布时间】:2018-12-17 03:28:26
【问题描述】:

我只是跳入 codeigniter,我认为在 Model 和 Controller 中学习很难,但 Viewes 很好。我面临在codeigniter中创建计算(添加+)函数的问题。

实际上,在我的数据库中,我保存了这样的值:-

-------------------------------------------
ID  |  Name      |  Amount   |  DATE
-------------------------------------------
01  | Product 1  |  $250     | 20/09/1996
02  | Product 2  |  $200     | 20/09/1996
03  | Product 3  |  $210     | 20/09/1996
04  | Product 4  |  $260     | 20/09/1996
05  | Product 5  |  $280     | 20/09/1996

我想添加所有金额值,以便我创建此代码并添加到模型中

public function countTotalToday()
    {
        date_default_timezone_set('Asia/Mumbai');
        $now = date('d-m-Y');
        //$sql = "SELECT amount FROM orders_item WHERE date = '$now'";
        $sql = "sum(amount) from order_item where date(dateCol)=date(now())";
        $query = $this->db->query($sql);
        //return $query->num_rows();
    }

在控制器中我是这样的

$this->data['total_paisa'] = $this->model_brands->countTotalToday();

在视图中

<h3 style="display:inline;"><?php echo $total_paisa ?></h3><h5 style="display:inline; vertical-align:center;">(Sleep <?php echo $total_paisa ?>)</h5>

但代码显示错误 500,我不知道我已解决此问题,您能否帮我制作用于计算金额的代码,并在添加方法中计算的视图中回显我。

【问题讨论】:

  • “不工作”是什么意思?你有任何错误吗?在您的文件顶部添加error reportingini_set("display_errors", 1); error_reporting(E_ALL); 并告诉我们您得到了什么。
  • 在 codeigniter 中我编辑您的代码以显示错误,我上面的代码是否正确?
  • arjunphp.com/error-reporting-handling-in-codeigniter(另外,请不要全部输入大写。这表示你在大喊大叫,这不是你想对试图帮助你的用户做的事情)跨度>
  • 您的表中没有date 列。那么为什么要在where 条件下使用它呢?
  • 很抱歉,我忘记在帖子中更新日期,但我确实在数据库中

标签: php codeigniter calculation


【解决方案1】:

根据您提供的表格数据,您没有date 列。所以不需要加where条件

这样试试

public function countTotalToday()
{
    date_default_timezone_set('Asia/Mumbai');
    $now = date('d-m-Y');
    $this->db->select('SUM(amount) AS amount');
    $qwer = $this->db->get('order_item')->result();
}

如果要添加条件。使用这个

$this->db->where('column-name', $value);

【讨论】:

  • 我为这样的回声做什么&lt;h3 style="display:inline;"&gt;&lt;?php echo $total_paisa ?&gt;&lt;/h3&gt;&lt;h5 style="display:inline; vertical-align:center;"&gt;(Sleep &lt;?php echo $total_paisa ?&gt;)&lt;/h5&gt;
猜你喜欢
  • 1970-01-01
  • 2011-10-19
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多