dragon071111 2018-11-19 02:20
浏览 74
已采纳

如何从codeigniter活动记录中将result_array列表导入row_array

Hi need to get a products value and its variant as there is one product and its variants are many, so in ordr to show the data i need the result in this format

[0] => Array
        (
            [id] => 4
            [cat_id] => 8
            [subcat_id] => 46
            [manu_id] => 108
            [pro_name] => sample product 1
            [slug] => sample-product-1
            [meta_title] => sample product 1
            [meta_desc] => sample product 
            [pro_desc] => sample product
            [feature_img] => 20181118100828.png
            [feature] => 0
            [view] => 0
            [created_at] => 2018-11-18 10:08:34
            [updated_at] => 
            [variant](
                     [0] =>  [sku_no] => JMS/20181118100834/0/4
                             [rprice] => 2500
                             [sprice] => 2000
                             [availability] => 1,
                     [1] =>  [sku_no] => JMS/20181118100834/0/4
                             [rprice] => 2500
                             [sprice] => 2000
                             [availability] => 1
                     )

As i am using this query:

public function GetProductData($id){
        $this->db->select('A.*, B.variant, sku_no, rprice, sprice, availability');
        $this->db->from('products A');
        $this->db->join('product_sku B','A.id = B.product_id');
        $this->db->where('A.id', $id);
        $query = $this->db->get();
        if($query->num_rows()>0){
            $results = $query->result_array();
            echo '<pre>';
            print_r($results);
            die;
        }
    }

Which gives me result into this format:

Array
(
    [0] => Array
        (
            [id] => 4
            [cat_id] => 8
            [subcat_id] => 46
            [manu_id] => 108
            [pro_name] => sample product 1
            [slug] => sample-product-1
            [meta_title] => sample product 1
            [meta_desc] => sample product 1sample product 1sample product 1sample product 1
            [pro_desc] => 
sample product 1sample product 1sample product 1sample product 1


            [feature_img] => 20181118100828.png
            [feature] => 0
            [view] => 0
            [created_at] => 2018-11-18 10:08:34
            [updated_at] => 
            [variant] => cotton large
            [sku_no] => JMS/20181118100834/0/4
            [rprice] => 2500
            [sprice] => 2000
            [availability] => 1
        )

    [1] => Array
        (
            [id] => 4
            [cat_id] => 8
            [subcat_id] => 46
            [manu_id] => 108
            [pro_name] => sample product 1
            [slug] => sample-product-1
            [meta_title] => sample product 1
            [meta_desc] => sample product 1sample product 1sample product 1sample product 1
            [pro_desc] => 
sample product 1sample product 1sample product 1sample product 1


            [feature_img] => 20181118100828.png
            [feature] => 0
            [view] => 0
            [created_at] => 2018-11-18 10:08:34
            [updated_at] => 
            [variant] => cotton medium
            [sku_no] => JMS/20181118100834/1/4
            [rprice] => 2500
            [sprice] => 1800
            [availability] => 1
        )

)

I don't want to use other function into this as it will slow the process function is there any mysql solution available apart form using another function within this function.

</div>
  • 写回答

1条回答 默认 最新

  • douzouchang7448 2018-11-27 12:13
    关注

    You could iterate each row of the result set from products and then do another query on product_sku. This has over-head and isn't efficient. But, MYSQL group_concat is your friend here.

    The idea is to concatenate the fields you want, and then group them in the select for each of the variants. Then you can explode them later to create the actual result. make sure the delimiters you choose can't occur in the database.

    Something like:

    $this->db->simple_query('SET SESSION group_concat_max_len = 1000000');
    $q = $this->db->select("A.id,A.cat_id,A.subcat_id,A.manu_id,A.pro_name,A.slug,A.meta_title,A.meta_desc,A.pro_desc,
          A.feature_img,A.feature,A.view,A.created_at,A.updated_at, 
          GROUP_CONCAT(CONCAT(B.sku_no, 'æ',B.rprice,'æ',B.sprice,'æ',B.availability) SEPARATOR 'ø') as variants",FALSE)
      ->from('products A')
      ->join('product_sku B','A.id = B.product_id')
      ->where('A.id', $id)
      ->group_by('A.id,A.cat_id,A.subcat_id,A.manu_id,A.pro_name,A.slug,A.meta_title,A.meta_desc,A.pro_desc,
          A.feature_img,A.feature,A.view,A.created_at,A.updated_at')
      ->get();
    $results = [];
    foreach ($q->result_array() as $row) {
      $variants = array();
      if($row['variants']){
       foreach (explode('ø',$row['variants']) as $variant) {
         list($sku,$rprice,$sprice,$availability) = explode('æ',$variant);
         $variants[] = [
           'sku' => $sku,
           'rprice' => $rprice,
           'sprice' -> $sprice,
           'availability' => $availability
         ];
       }
      }
      $row['variant'] = $variants;
      unset($row['variants']);
      $results[] = $row;
    }
    return $results;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题