【问题标题】:How to get the data from database using foreach loop?如何使用foreach循环从数据库中获取数据?
【发布时间】:2020-01-22 12:49:46
【问题描述】:

我正在尝试使用 foreach 循环和以下查询从 CodeIgniter 中的数据库中获取数据,但我只得到最后一个结果。

public function get_tags($limit, $start, $tag_id)
{    

    $snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);

    foreach ($snippetstagdata as $getSnippet) {
        $snippet_id = $getSnippet->snippet_id;
        $this->db->limit($limit, $start);
        $this->db->select('*');
        $this->db->from('snippets');
        $this->db->where("id", $snippet_id);
        //$this->db->like('snippet_tags',$query);
        $query = $this->db->get();

    }

    $result = $query->result();

    $this->db->save_queries = false;

    return $result;
}

getDataOneColumn 函数是

public function getDataOneColumn($table, $col1_name, $col1_value)
{
    $this->db->where("$col1_name", $col1_value);
    $query = $this->db->get("$table");
    $result = $query->result();
    $this->db->save_queries = false;

    return $result;
}

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3 codeigniter-2


    【解决方案1】:

    你应该试试这个

    public function get_tags($limit, $start, $tag_id) {
    
            $snippetstagdata = $this->getDataOneColumn("snippets_tags", "tag_id", $tag_id);
    
            $review = [];
            foreach ($snippetstagdata as $getSnippet) {
                $snippet_id = $getSnippet->snippet_id;
                $this->db->limit($limit, $start);
                $this->db->select('*');
                $this->db->from('snippets');
                $this->db->where("id", $snippet_id);
                //$this->db->like('snippet_tags',$query);
                $query = $this->db->get();
                $result[] = $query->result();
            }       
            $this->db->save_queries = false;
            return $result;
        }
    

    【讨论】:

      【解决方案2】:

      就像@steve 所说的,您每次运行循环时都会替换 $query->result() 。所以首先你需要创建一个关联数组来处理这个。所以你的代码应该是:

      $data = array();
      foreach ($snippetstagdata as $getSnippet) {
          $snippet_id = $getSnippet->snippet_id;
          $this->db->limit($limit, $start);
          $this->db->select('*');
          $this->db->from('snippets');
          $this->db->where("id", $snippet_id);
          //$this->db->like('snippet_tags',$query);
          $query = $this->db->get(); 
          $result = $query->result();
          $data[$snippet_id][] = $result;
      
      }
      

      然后就可以从 $data 数组中获取所有内容了。 希望有帮助

      【讨论】:

        【解决方案3】:

        这是不正确的:

        foreach ($snippetstagdata as $getSnippet) {
            $snippet_id = $getSnippet->snippet_id;
            $this->db->limit($limit, $start);
            $this->db->select('*');
            $this->db->from('snippets');
            $this->db->where("id", $snippet_id);
            //$this->db->like('snippet_tags',$query);
            $query = $this->db->get();
        
        }
        
        $result = $query->result();
        

        您正在遍历数据中的每个项目并每次重置$query,但在您退出循环时仅使用一次$query

        你需要移动这条线:

        $result = $query->result();
        

        在循环内得到正确的输出。

        编辑:

        正如所指出的,这也不是 100%。 $result 应该是一个数组,并且在循环内应该将查询推入数组,如下所示:

        $result[] = $query->result();
        

        然后最终的数组 ($result) 可以自己循环遍历。

        【讨论】:

        • $result 变量仍在循环内被覆盖。应该做成一个数组,用$result[]把查询结果放到数组中
        • 正确,我打算添加更多关于存储变量的内容,但我没有,懒惰了:(我现在就这样做。
        猜你喜欢
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2020-12-11
        • 2015-07-10
        • 2019-05-23
        • 1970-01-01
        • 2019-02-04
        • 1970-01-01
        相关资源
        最近更新 更多