UPDATE goods SET is_new =1 ORDER BY rand( ) LIMIT 10

GoodModel.class.php

/*取出指定条数的新品*/

publicfunctiongetNew($n = 5){

$sql = 'select goods_id,goods_name,shop_price,goods_price,goods_thumb  from '.$this->table.' order byaddtime limit '.$n;

return $this->db->getAll($sql);

}

Index.php

/*取出五条新品*/

$goods = new GoodsModel();

$newlist = $goods->getNew(5);

前台展示页面

<?php foreach($newlist as $g) { ?>

<div>

<div>

<a  href="goods.php?goods_id=<?php echo $g['goods_id'];  ?>"><img alt="<?php echo $g['goods_name']; ?>"  src="<?php echo $g['thumb_img']; ?>"></a>

</div>

<div>

<div><a  title="<?php echo $g['goods_name']; ?>"  href="goods.php?goods_id=<?php echo $g['goods_id']; ?>">

<?php echo  mb_substr($g['goods_name'],0,12,'UTF-8'),'...'; ?></a></div>

<span>市场价:</span><font><?php echo $g['market_price']; ?></font><br>中意价:<font><?php echo $g['shop_price']; ?></font>

</div>

</div>

<?php } ?>

根据栏目取商品:

/*取出指定栏目的商品

cat_id 对应的栏目下,可能没有商品,

商品放在大栏目下的分类中

正确的做法是:找出cat_id的所有子孙栏目下的所有商品

*/

publicfunctioncatGoods($cat_id){

$category = new CatModel();

$cats = $category->select();//取出的栏目

$sons = $category->getCatTree($cats,$cat_id);//取出给定栏目的子孙栏目

$sub = array();

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);

}