Blogger Information
Blog 32
fans 2
comment 2
visits 23298
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php动态数据商城(0110~0112)
暴风战斧
Original
511 people have browsed it

编程思路

这次作业我主要从两点出发,1、完成商城前端静态页面并拆分;2、运用函数实现各页面动态获取数据!

静态页面动态化思路

作业总结

1、这次最难的就是理解和运用函数,foreach循环和if判断好理解,难在自定义函数function,如:

  1. <?php
  2. //定义函数随机获取数组信息
  3. function getSeckillGoods($goods, $num)
  4. {
  5. $seckillGoodsKeys = array_rand($goods, $num);
  6. //遍历数组得到随机商品
  7. foreach ($seckillGoodsKeys as $key) {
  8. $seckillGoods[] = $goods[$key];
  9. }
  10. return $seckillGoods;
  11. }
  12. $seckillGoods = getSeckillGoods($goods, 6);
  13. ?>

自己理解了几遍,尝试打印测试,最后还是回顾下视频才理解了,开始一直错误理解为$key不应该也是数组吗?,原来$seckillGoodsKeys是获取的键组成的数组,而$key则是循环赋值的变量;
2、写首页时理解了function函数后,后续的静态页面动态化顺利了很多,这次前端又回顾了一遍flex和grid布局,真好用;
3、这次从静态页面走向动态页面,编程过程中实时查看效果错误蛮多的,自己慢慢找原因解决,过程中查了w3c手册发现arry_rand这个函数手册没解释清楚,回看了视频才瞬间领悟,作为新手确实练习量不够经验知识积累都不够,路还长,真羡慕那些有2~3年经验的同学,哪怕1年也超级羡慕!

1.商城首页

  • 演示代码
  1. <?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
  2. <?php
  3. //定义函数随机获取数组信息
  4. function getSeckillGoods($goods, $num)
  5. {
  6. $seckillGoodsKeys = array_rand($goods, $num);
  7. //遍历数组得到随机商品
  8. foreach ($seckillGoodsKeys as $key) {
  9. $seckillGoods[] = $goods[$key];
  10. }
  11. return $seckillGoods;
  12. }
  13. $seckillGoods = getSeckillGoods($goods, 4);
  14. ?>
  15. <main>
  16. <div class="goods">
  17. <!--正在秒杀-->
  18. <div class="title">
  19. <a href="shop_seckill.php">正在秒杀</a>
  20. </div>
  21. <!--从其他专区中随机抽取商品-->
  22. <div class="content">
  23. <?php foreach ($seckillGoods as $good) : ?>
  24. <div>
  25. <a href="<?php echo $good['url'] ?>"><img src="<?php echo STATIC_PATH . $good['pic'] ?>" alt=""></a>
  26. <a href=""><?php echo $good['name'] ?></a>
  27. <div>
  28. <span>&yen <?php echo $good['price'] ?></span>
  29. <button>立即购买</button>
  30. </div>
  31. </div>
  32. <?php endforeach; ?>
  33. </div>
  34. </div>
  35. <!--动态获取专区商品信息-->
  36. <?php foreach ($categories as $category) : ?>
  37. <div class="goods">
  38. <div class="title">
  39. <a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
  40. </div>
  41. <div class="content">
  42. <?php foreach ($goods as $good) : ?>
  43. <!--获取属于当前专区的商品-->
  44. <?php if ($good['cid'] === $category['id']) : ?>
  45. <div>
  46. <a href="<?php echo $good['url'] ?>"><img src="<?php echo STATIC_PATH . $good['pic'] ?>"
  47. alt=""></a>
  48. <a href=""><?php echo $good['name'] ?></a>
  49. <div>
  50. <span>&yen <?php echo $good['price'] ?></span>
  51. <button>立即购买</button>
  52. </div>
  53. </div>
  54. <?php endif; ?>
  55. <?php endforeach; ?>
  56. </div>
  57. </div>
  58. <?php endforeach; ?>
  59. </main>
  60. <?php include __DIR__ . '/shop_module/shop_public_footer.php' ?>
  • 效果图

2、商城频道页

  • 演示代码
  1. <?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
  2. <?php
  3. // 获取栏目
  4. $cid = $_GET['cid'];
  5. // 获取栏目名称
  6. function getCateName($cid, $categories) {
  7. foreach ($categories as $category) {
  8. if ($category['id'] === intval($cid)) {
  9. $cateName = $category['name'];
  10. }
  11. }
  12. return $cateName;
  13. }
  14. $cateName = getCateName($cid, $categories);
  15. // 获取该栏目所有商品
  16. function getCateGoods($cid, $goods) {
  17. //根据栏目id,获取对应的商品信息
  18. foreach ($goods as $good) {
  19. if ($good['cid'] === intval($cid)) {
  20. $currentGoods[] = $good;
  21. }
  22. }
  23. return $currentGoods;
  24. }
  25. $currentGoods = getCateGoods($cid, $goods);
  26. ?>
  27. <?php include __DIR__ . '/shop_module/shop_public_footer.php' ?>
  • 效果图

商城详情页

  • 演示代码
  1. <?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
  2. <?php
  3. // 获取当前页面id
  4. $id = $_GET['id'];
  5. // 根据id获取当前商品信息
  6. function getDetailGood($goods, $id)
  7. {
  8. foreach ($goods as $good) {
  9. if ($good['id'] === intval($id)) {
  10. $detailGood = $good;
  11. }
  12. }
  13. return $detailGood;
  14. }
  15. $detailGood = getDetailGood($goods, $id);
  16. ?>
  • 效果图

4、商城秒杀专题页

  • 演示代码
  1. <?php include __DIR__ . '/shop_module/shop_public_header.php' ?>
  2. <?php
  3. //定义函数随机获取数组信息
  4. function getSeckillGoods($goods, $num)
  5. {
  6. $seckillGoodsKeys = array_rand($goods, $num);
  7. //遍历数组得到随机商品
  8. foreach ($seckillGoodsKeys as $key) {
  9. $seckillGoods[] = $goods[$key];
  10. }
  11. return $seckillGoods;
  12. }
  13. $seckillGoods = getSeckillGoods($goods, 6);
  14. ?>
  • 效果图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:如果你打过游戏, 就会知道一件法器的作用... 函数就是你的法器
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments