老板说:

我要一行5个商品,每个长得都不一样

php练习:每5个商品一排

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>练习</title>
<style>
    li{list-style: none;}
    .slide-content li{float:left;width:100px;height:100px;border:1px solid #000;margin:5px;line-height: 100px;color:#000;text-align: center;}
    .li-1{background:red;}
    .li-2{background:yellow;}
    .li-3{background:blue;}
    .li-4{background:green;}
    .li-5{background:gray;}
</style>
</head>
<body>

<?php
//商品组至少5个噢
$items=array(
    array("name"=>"product1","price"=>10),
    array("name"=>"product2","price"=>20),
    array("name"=>"product3","price"=>30),
    array("name"=>"product4","price"=>40),
    array("name"=>"product5","price"=>10)
);
//一行显示5个

    $li_n=5;
    //宝贝数
    $p_num=count($items)+1;    
    //循环的ul数,cei(1.5)=2
    $ul_n=ceil($p_num/$li_n);    
for($i=1;$i<$ul_n;$i++){    
    $li_nav=1;    
    echo '<ul class="slide-content">';
            foreach($items as $key => $item){

                /*listart*/
                $s_n=$key+1;
                $end_q=$i*$li_n;
                $star_q=$end_q-$li_n;

                /*1-5,5-10,10-15等,li_nav每次从1开始*/
                if($s_n>$star_q and $s_n<=$end_q){
                    echo '<li class="li-'.$li_nav.'">'.$item['name'].'</li>';
                    $li_nav++;
                }    
                /*liend*/
            }    
echo '</ul>';
    }
?>


</body>
</html>

 php练习:每5个商品一排

但是,我不会告诉你我有更好的方法>>使用array_chunk()数组分割

相关文章:

  • 2021-11-18
  • 2021-05-11
  • 2021-09-27
  • 2021-08-28
  • 2021-11-20
  • 2021-11-20
  • 2021-05-29
  • 2021-11-17
猜你喜欢
  • 2021-05-14
  • 2022-12-23
  • 2021-11-20
  • 2021-12-12
  • 2022-01-13
  • 2021-11-20
  • 2021-06-23
相关资源
相似解决方案