zoukankan      html  css  js  c++  java
  • 相关产品推荐:根据相同tag来推荐产品,一共8个;不足的话就选择同分类下的随机

    // 推荐产品: 根据相同tag来推荐产品,一共8个;不足的话就选择同分类下的随机
    $tag_string = $product['tag'];
    $tag_arr = explode(',', $tag_string);
    
    
    $all_res = Db::table('yg_product')->select();
    $related_arr = array();
    $related_id_arr = array();
    for($i=0; $i<count($all_res); $i++){
        if($all_res[$i]['id'] == $id){
            continue;
        }
        $t_string = $all_res[$i]['tag'];
        $t_arr = explode(',', $t_string);
    
    
        $res_arr = array_intersect($tag_arr,$t_arr);
        if(count($res_arr)>0){
            array_push($related_arr, $all_res[$i]);
            array_push($related_id_arr, $all_res[$i]['id']);
        }
    }
    
    
    // 如果相同tag标签的产品数量不足8个,则获取同分类下的产品
    if(count($related_id_arr) < 8){
        $need_num = 8 - count($related_id_arr);
        $same_cate_res = Db::table('yg_product')
            ->where('procatelog_id', $product['procatelog_id'])
            ->where('id','not in',$related_id_arr)->limit($need_num)->select();
        $related_arr = array_merge($related_arr, $same_cate_res);
    }else if(count($related_id_arr) > 8){
        $related_arr = array_slice($related_arr,0,8);
    }
    
    $this->assign([
        'product'=>$product,
        'related_arr'=>$related_arr,
    ]);
  • 相关阅读:
    简单读取winfrom资源文件
    string.Format对C#字符串格式化
    如何在SQL中使用循环结构
    Linq to SQL 多条件动态组合查询(实战篇)
    提问的艺术
    XtraGrid 单元格加边框颜色
    凭证控件制作
    C# double 四舍五入
    自定义光标样式
    触发窗体事件(例如按Esc关闭窗体)
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/15131557.html
Copyright © 2011-2022 走看看