总结5.27php数组练习

<?php
$arr = [
    ['id'=>1,'title'=>'产品1','img'=>'images/tree_03.jpg'],
    ['id'=>2,'title'=>'产品2','img'=>'images/tree_05.jpg'],
    ['id'=>3,'title'=>'产品3','img'=>'images/tree_07.jpg'],
    ['id'=>4,'title'=>'产品4','img'=>'images/tree_12.jpg'],
    ['id'=>5,'title'=>'产品5','img'=>'images/tree_13.jpg'],
    ['id'=>6,'title'=>'产品6','img'=>'images/tree_14.png'],
]
?>
通过二维数组,设定每次循环的产品名称与图片
<?php
                        foreach($arr as $val){
                            ?>
                                <li>
                                <a href="b.php?id=<?php echo $val['id']?>">
                                    <p><img src="<?php echo $val['img']?>" alt=""></p>
                                    <span><?php echo $val['title']?></span>
                                    <div class="hide_con">
                                        <em><img src="images/add.png" alt=""></em>
                                        <i>查看详情</i>
                                    </div>
                                </a>
                                </li>
                            <?php
                        }
                        ?>
采用foreach循环,将二维数组中每个数组的元素打印到html区间的相应位置,再通过循环来形成多个框架取代li标签
<?php
if(isset($_GET['id'])){
    $id = $_GET['id'];采用是否存在获取变量的方法来实现页面如何跳转,与前面目录跳转类似
}else{
    header("location:/");
}
$arr = [];
switch($id){
    case 1:
        $arr = ['id'=>1,'title'=>'产品1','img'=>'images/tree_03.jpg'];
    break;
    case 2:
        $arr = ['id'=>2,'title'=>'产品2','img'=>'images/tree_05.jpg'];
    break;
    case 3:
        $arr = ['id'=>3,'title'=>'产品3','img'=>'images/tree_07.jpg'];
    break;
    case 4:
        $arr = ['id'=>4,'title'=>'产品4','img'=>'images/tree_12.jpg'];
    break;
    case 5:
        $arr = ['id'=>5,'title'=>'产品5','img'=>'images/tree_13.jpg'];
    break;
    case 6:
        $arr = ['id'=>6,'title'=>'产品6','img'=>'images/tree_14.png'];
    break;
}采用switch判断给数组添加设置元素来确定跳转后页面的内容
?>
<div class="l_assay_img fl">
                                <img src="<?php echo $arr['img']?>" alt="">
                            </div>
                            <div class="r_assay_intro fr">
                                <h2><?php echo $arr['title']?></h2>
 
posted @ 2020-05-27 20:34  HighKK  阅读(127)  评论(0编辑  收藏  举报