dotws86260 2018-12-17 03:28
浏览 19

从mysql数据库中计算codeigniter中的值

I just jump in codeigniter where I believe it so hard to learn in Model and Controller but where Viewes is good. I am facing an issue to creating a calculation (adding +) function in codeigniter.

Actually in my database I save the values like this:-

-------------------------------------------
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

I want add all amount value so that I created this code and added in model

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();
    }

and in controller i make like this

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

and in Views

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

But the code says error 500, I don't know i fixed this, could you help me how oi make the code which works to calculate the value as amount and echo me in views with calculation in Adding method.

  • 写回答

1条回答 默认 最新

  • drfqfuhej48511519 2018-12-17 11:20
    关注

    According to your provided table data you don't have date column. So no need to add where condition

    Try like this

    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();
    }
    

    If you want to add the condition. Use this

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

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并