• 商城栏目开发


    商城栏目页面左边有一个栏目分级条,点击一个栏目,中间就会显示当前位置在首页下的那个栏目下,实现条理清晰。

    category.php

    <?php
    
    
    define('ACC',true);
    require('./include/init.php');
    
    $cat_id = isset($_GET['cat_id'])?$_GET['cat_id']+0:0;
    
    $cat = new CatModel();
    $category = $cat->getRow($cat_id);
    
    if(empty($category)) {
        header('location: index.php');
        exit;
    }
    
    
    // 取出树状导航
    $cats = $cat->select(); // 获取所有的栏目
    $sort = $cat->subtree($cats,0,0);
    
    
    // 取出面包屑导航
    $nav = $cat->familytree($cats,$cat_id);
    
    
    // 取出栏目下的商品
    $goods = new GoodsModel();
    $goodslist = $goods->catGoods($cat_id);
    
    
    include(ROOT . 'view/front/lanmu.html');
    ?>
    

    所用到的方法

    catModel.class.php
    //查找家谱树
    	public function familytree($arr,$id){
    		static $tree=array();
    		foreach($arr as $v){
    			if($v['id']==$id){
    				//判断要不要找父栏目
    				if($v['parent_id']>0){
    					$this->familytree($arr,$v['parent_id']);
    				}
    				$tree[]=$v;
    			}
    		}
    		return $tree;
    	}
    	//递归格式化数据,查找子孙树
    	public function subtree($arr,$id=0,$lev=0){
    		static $result=array();
    		foreach($arr as $v){
    			if($v['parent_id']==$id){
    				$v['lev']=$lev;
    				$result[]=$v;
    				$this->subtree($arr,$v['id'],$lev+1);
    			}
    		}
    		return $result;
    	}
    
    
    goodsModel.class.php
     /*
            取出指定栏目的商品
            // $cat_id = $_GET['cat_id'];
            // $sql = select .. from goods where cat_id = $cat_id;
            // 这是错的
    
            因为$cat_id对应的栏目有可能是个大栏目,而大栏目下面没有商品.
            商品放在大栏目下面的小栏目下面.
    
            因此,正确的做法,是找出$cat_id的所有子孙栏目,
            然后再查所有$cat_id及其子孙栏目下的商品.
        */
    
        public function catGoods($cat_id) {
            $category = new CatModel();
            $cats = $category->select(); // 取出所有的栏目来
            $sons = $category->getSon($cats,$cat_id);  // 取出给定栏目的子孙栏目
            
            $sub = array($cat_id);
    
            if(!empty($sons)) { // 没有子孙栏目
                foreach($sons as $v) {
                    $sub[] = $v['cat_id'];
                }
            }
    
            $in = implode(',',$sub);
    
            $sql = 'select goods_id,goods_name,shop_price,market_price,thumb_img from ' . $this->table . ' where cat_id in (' . $in . ') order by add_time limit 5';
    
            return $this->db->getAll($sql);
        }
    
        /*
            获取购物中商品的详细信息
            params array $items 购物车中的商品数组
            return 商品数组的详细信息
        */
    
        public function getCartGoods($items) {
            foreach($items as $k=>$v) {  // 循环购物车中的商品,每循环一个,到数据查一下对应的详细信息
    
                $sql = 'select goods_id,goods_name,thumb_img,shop_price,market_price from ' . $this->table . ' where goods_id =' . $k;
    
                $row = $this->db->getRow($sql);
    
                $items[$k]['thumb_img'] = $row['thumb_img'];
                $items[$k]['market_price'] = $row['market_price'];
            
            }
    
            return $items;
           
        }
    

     显示页面lanmu.php部分程序

    <div class="cate_tree">
                	<div class="lib_top">产品分类</div>
                    <div class="lib_mid">
                    	<ul>
                        <?php foreach($sort as $c) { ?>
                                            	<li class="cate_level_<?php echo $c['lev'];?>"><a href="category.php?cat_id=<?php echo $c['id'];?>"><?php echo $c['cat_name'];?></a></li>
                        <?php } ?>
    
                                                       
                        </ul>
                    </div>
                    <div class="lib_down"></div>
                </div>
    
    当前位置: <a href="index.php">首页</a> 
                <?php foreach($nav as $c) { ?>
                <code>></code> <a href="category.php?cat_id=<?php echo $c['id'];?>"><?php echo $c['cat_name'];?></a>
                <?php } ?>
    
  • 相关阅读:
    (11)《数据结构与算法》之赫夫曼树
    (8)《数据结构与算法》之查找算法
    String的用法——转换功能
    结合redis 的List数据结构特性扩展 栈与队列
    上传文件返回前端json的时候,去除返回值带 <pre style="word-wrap:break-word;white-space:prewrap;"></pre>的问题
    Java休眠方式
    DButils自增ID转换失败
    Java元注解
    java 单例模式:饿汉式与懒汉式
    java do -while 三种用法
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4815358.html
Copyright © 2020-2023  润新知