Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:如果你打过游戏, 就会知道一件法器的作用...
函数就是你的法器
这次作业我主要从两点出发,1、完成商城前端静态页面并拆分;2、运用函数实现各页面动态获取数据!
1、这次最难的就是理解和运用函数,foreach循环和if判断好理解,难在自定义函数function,如:
<?php
//定义函数随机获取数组信息
function getSeckillGoods($goods, $num)
{
$seckillGoodsKeys = array_rand($goods, $num);
//遍历数组得到随机商品
foreach ($seckillGoodsKeys as $key) {
$seckillGoods[] = $goods[$key];
}
return $seckillGoods;
}
$seckillGoods = getSeckillGoods($goods, 6);
?>
自己理解了几遍,尝试打印测试,最后还是回顾下视频才理解了,开始一直错误理解为$key不应该也是数组吗?,原来$seckillGoodsKeys是获取的键组成的数组,而$key则是循环赋值的变量;
2、写首页时理解了function函数后,后续的静态页面动态化顺利了很多,这次前端又回顾了一遍flex和grid布局,真好用;
3、这次从静态页面走向动态页面,编程过程中实时查看效果错误蛮多的,自己慢慢找原因解决,过程中查了w3c手册发现arry_rand这个函数手册没解释清楚,回看了视频才瞬间领悟,作为新手确实练习量不够经验知识积累都不够,路还长,真羡慕那些有2~3年经验的同学,哪怕1年也超级羡慕!
<?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
<?php
//定义函数随机获取数组信息
function getSeckillGoods($goods, $num)
{
$seckillGoodsKeys = array_rand($goods, $num);
//遍历数组得到随机商品
foreach ($seckillGoodsKeys as $key) {
$seckillGoods[] = $goods[$key];
}
return $seckillGoods;
}
$seckillGoods = getSeckillGoods($goods, 4);
?>
<main>
<div class="goods">
<!--正在秒杀-->
<div class="title">
<a href="shop_seckill.php">正在秒杀</a>
</div>
<!--从其他专区中随机抽取商品-->
<div class="content">
<?php foreach ($seckillGoods as $good) : ?>
<div>
<a href="<?php echo $good['url'] ?>"><img src="<?php echo STATIC_PATH . $good['pic'] ?>" alt=""></a>
<a href=""><?php echo $good['name'] ?></a>
<div>
<span>¥ <?php echo $good['price'] ?></span>
<button>立即购买</button>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!--动态获取专区商品信息-->
<?php foreach ($categories as $category) : ?>
<div class="goods">
<div class="title">
<a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
</div>
<div class="content">
<?php foreach ($goods as $good) : ?>
<!--获取属于当前专区的商品-->
<?php if ($good['cid'] === $category['id']) : ?>
<div>
<a href="<?php echo $good['url'] ?>"><img src="<?php echo STATIC_PATH . $good['pic'] ?>"
alt=""></a>
<a href=""><?php echo $good['name'] ?></a>
<div>
<span>¥ <?php echo $good['price'] ?></span>
<button>立即购买</button>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</main>
<?php include __DIR__ . '/shop_module/shop_public_footer.php' ?>
<?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
<?php
// 获取栏目
$cid = $_GET['cid'];
// 获取栏目名称
function getCateName($cid, $categories) {
foreach ($categories as $category) {
if ($category['id'] === intval($cid)) {
$cateName = $category['name'];
}
}
return $cateName;
}
$cateName = getCateName($cid, $categories);
// 获取该栏目所有商品
function getCateGoods($cid, $goods) {
//根据栏目id,获取对应的商品信息
foreach ($goods as $good) {
if ($good['cid'] === intval($cid)) {
$currentGoods[] = $good;
}
}
return $currentGoods;
}
$currentGoods = getCateGoods($cid, $goods);
?>
<?php include __DIR__ . '/shop_module/shop_public_footer.php' ?>
<?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
<?php
// 获取当前页面id
$id = $_GET['id'];
// 根据id获取当前商品信息
function getDetailGood($goods, $id)
{
foreach ($goods as $good) {
if ($good['id'] === intval($id)) {
$detailGood = $good;
}
}
return $detailGood;
}
$detailGood = getDetailGood($goods, $id);
?>
<?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
<?php
//定义函数随机获取数组信息
function getSeckillGoods($goods, $num)
{
$seckillGoodsKeys = array_rand($goods, $num);
//遍历数组得到随机商品
foreach ($seckillGoodsKeys as $key) {
$seckillGoods[] = $goods[$key];
}
return $seckillGoods;
}
$seckillGoods = getSeckillGoods($goods, 6);
?>