简体   繁体   中英

cant get array to display text and pictures in php

I'm trying to get elements from an array to display on the screen. How do I target the array elements?

<!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>
    <h1>My favourite Car</h1>

    <?php 

    $cars = [
        [

            "name" => "HRV",
            "price" => "CAD-$23300",
            "src" => "http://direct.automobiles.honda.com/images/2016/hr-v/exterior-gallery-new/2016-honda-hrv-front-view.jpg",
            "description" => "HRV is a mini suv made by Honda."
         ],
             [
            "name" => "CHR",
            "price" =>"CAD-$23675",
            "src" => "https://d1bxbrgbmu84na.cloudfront.net/wp-content/uploads/2019/08/16093812/CHR.jpg",
            "description" => "CHR is a mini suv made by Toyota."
             ],
             [
            "name" => "RDX",
            "price" =>"CAD-$43990",
            "src" => "https://www.acura.ca/Content/acura.ca/e270f141-7f67-4fe2-99bd-e808e3c3c2d7/MediumSizedFeature/03_rdx19_overview_MediumFeature_mobile.jpg",
            "description" => "RDX is a large SUV made by Acura." 
             ]

            ];

                       foreach($cars as $car){ ?>

                <img src="<?php echo "$car->src" ?>">
                <h2><?php echo $car->name ?></h2>
                <p><?php echo $car->price ?></p>
                 <p><?php echo $car->description ?></p>
        <?php

    } 

 ?>


</body>

</html>

You are using object syntax but are working with an array.

Use the following syntax to access the array elements:

$car['src'];
$car['name'];

...etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM