【问题标题】:How to get array values in codeigniter using explode method如何使用explode方法在codeigniter中获取数组值
【发布时间】:2015-11-02 07:22:44
【问题描述】:

查询运行基于 2 个 id,但我得到一个输出值

输入:

public function get_place_order_category($id)
  {
     /* $id = 1,2;*/

      $ids = explode(',',$id);
      foreach($ids as $catid)
      {
      $this->db->where('product_id =',trim($catid));
      $query = $this->db->get('products'); 

      return $query->result_array();          
      }

  }

输出:

Array
(
    [0] => Array
        (
            [product_id] => 12
            [product_name] => Product1
            [product_code] => pro12345
            [product_price] => 200.00
            [product_newprice] => 0.00
            [size] => 0-6M , 0-9M ,9-12M
            [product_front] => 1446200664.JPG
            [product_back] => 14462006641.JPG
            [product_left] => 14462006642.JPG
            [product_right] => 14462006643.JPG
            [product_description] => 
            [styling_tips] => 
            [category_id] => 62
            [user_id] => 5
            [status] => 1
            [parent_id] => 42
            [qty] => 4
            [promo_id] => 0
            [etc4] => 0
            [etc5] => 0
        )

)

【问题讨论】:

  • 您想要基于所有通过的 id 的记录吗?
  • 输出来自return $query->result_array();这里??
  • 当然你只会得到一个,一旦return 行命中,你的方法就结束了,要么先收集所有结果,要么只选择带有IN 子句的行
  • 只需将分解后的 id 传递给->where_in 方法,不需要循环等。
  • 我使用 wher_in 查询 @Ghost 但我收到错误您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的 'IN ('12', '13')' 附近使用正确的语法 SELECT * FROM (products) WHERE product_id = IN ('12' , '13')

标签: php mysql codeigniter


【解决方案1】:

您可以使用一个全局数组并向其中添加结果数组。试试这个:

public function get_place_order_category($id)
  {
     /* $id = 1,2;*/
     //first test here what comes in $id
      echo $id."<br/>";
      $ans_arr = array();
      $ids = explode(',',$id);
      /*test here too whats actually happens after explode weather   
      arrayhas generated and what are the element of it */
      echo "<pre>";print_r($ids);echo "<br/>";
      foreach($ids as $catid)
      {
          $this->db->where('product_id =',trim($catid));
          $query = $this->db->get('products'); 

          $ans_arr[] = $query->result_array();          
      }
      return $ans_arr;
  }

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 2021-08-31
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多