猿问

如何将数组添加到我的 php 变量中,循环不适用于数组

我正在尝试将我的变量放入一个或多个数组中,然后循环它们


<!DOCTYPE html>

<html>

<style>

    html {

        background-color: bisque;

    }


    h1 {


        display: block;

        text-align: center;


    }


    h2 {

        display: block;

        text-align: center;

        color: saddlebrown;

    }


    img {

        border-radius: 50%;

        width: 800px;

        text-align: center;

    }


    p {


        text-align: center;

        color: saddlebrown;

    }


</style>

<head>

    <meta charset="UTF-8">

    <title>Dynamic web pages with PHP</title>


</head>



<body>

    <header>

        <nav id="main-navigation">


        </nav>

    </header>


    <?php 


     $Cartitle = "My favourite Cars";

                    $Carname = "HRV";

                    $Carprice = "CAD-$23300";

                    $Carimg = "http://direct.automobiles.honda.com/images/2016/hr-v/exterior-gallery-new/2016-honda-hrv-front-view.jpg";

                    $Cardescription = "HRV is a mini suv made by Honda";


                   $Carname2 = "CHR";

                   $Carprice2 = "CAD-$23675";

                    $Carimg2 = "https://d1bxbrgbmu84na.cloudfront.net/wp-content/uploads/2019/08/16093812/CHR.jpg";

                  $Cardescription2 = "HRV is a mini suv made by Toyota";


                  $Carname3 = "RDX";

                   $Carprice3 = "CAD-$43990";

                    $Carimg3 = "https://www.acura.ca/Content/acura.ca/e270f141-7f67-4fe2-99bd-e808e3c3c2d7/MediumSizedFeature/03_rdx19_overview_MediumFeature_mobile.jpg";

                  $Cardescription3 = "RDX is a large SUV made by Acura";


 ?>



    <?php


    echo "<h1>$Cartitle<h2>$Carname <p>$Carprice <p> $Cardescription <br>  <img src=$Carimg></p></h2><h1><br>"; 

    echo "<h2>$Carname2 <p>$Carprice2 <p> $Cardescription2 <br>  <img src=$Carimg2></p></h2><br>";

    echo "<h2>$Carname3 <p>$Carprice3 <p> $Cardescription3 <br>  <img src=$Carimg3></p></h2>";


        ?>





</body>


</html>


慕村9548890
浏览 150回答 1
1回答

DIEA

作为示例(样式并根据需要添加)$cars[] = ['name'&nbsp; => 'HRV',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'price' => 'CAD-$23300',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'img'&nbsp; &nbsp;=> 'http://direct.automobiles.honda.com/images/2016/hr-v/exterior-gallery-new/2016-honda-hrv-front-view.jpg',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'desc'&nbsp; => 'HRV is a mini suv made by Honda',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];$cars[] = ['name'&nbsp; => 'CHR' ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'price' => 'CAD-$23675' ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'img'&nbsp; &nbsp;=> 'https://d1bxbrgbmu84na.cloudfront.net/wp-content/uploads/2019/08/16093812/CHR.jpg' ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'desc'&nbsp; => 'RDX is a large SUV made by Acura' ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;];echo '<h1>My favourite Cars</h1>';foreach ($cars as $detail) {echo '<h2>' . $detail['name']&nbsp; . '<p>' .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $detail['price'] . '<p>' .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $detail['desc']&nbsp; . '<br>' .&nbsp;'<img src=' . $detail['img']&nbsp; &nbsp;. '</p></h2>';}
随时随地看视频慕课网APP
我要回答