【问题标题】:How do I get looping result?如何获得循环结果?
【发布时间】:2020-04-27 06:05:11
【问题描述】:

如何获得循环结果?见注释无法在代码中得到结果。

public function get_data_by_7_days()
    {   

    $arr = array();
        for ($i=7;$i>=0;$i--)
        {
            $run=0;
            $run=$run-$i;

            $username = $this->session->userdata('username');
            $arr     = $this->M_vodor->get_data_by_7_days($username,$run);

        }
        echo json_encode($arr); //cant get result 
}

这是我的模型M_vodor.php

public function get_data_by_7_days($username,$run)
{

    return $this->db->query("
    SELECT
    DAYNAME(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL $run DAY) ,'%Y-%m-%d')) AS date_checker,
    DATE_FORMAT(DATE_ADD(NOW(), INTERVAL $run DAY) ,'%d-%m-%Y') AS show_date")->result();} 

【问题讨论】:

  • 这和 MySQL 有什么关系?
  • 好的。相应地编辑您的问题
  • 首先,在您的 for 循环中将 $arr=... 更改为 $arr[]=... 您必须将记录添加到数组中。
  • @1stlife 你和mysql没有任何关系。你可以在控制器本身做所有事情。

标签: php mysql loops codeigniter


【解决方案1】:

通过一些性能改进解决您的问题。

public function get_data_by_7_days() {   
    $arr = array();
    $username = $this->session->userdata('username'); //if you put this inside the loop, it will read sessions 7 times which affects perfomance
     for ($i=7;$i>=0;$i--){
         $run=0;
            $run=$run-$i;
            $arr[] = $this->M_vodor->get_data_by_7_days($username,$run); //you should use $arr[]
       }
       echo json_encode($arr);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多