简体   繁体   中英

PHP, addressing objects using class

I,m trying to list all the names from a json database to html using a class. the html output should look something like this:-

Characters

  • name: John
  • name: Sarah
  • name: Michael

I've managed to reach this far with my code. I know this code is not finished or it is has errors but I'm still a beginner and learning. Can you please help me get this code to work?

Json database file

databas.json

{
"results": [
    {
        "name": "John",
        "height": "182 cm",
        "mass": "80 kg",
    },
    {
        "name": "Sarah",
        "height": "165 kg",
        "mass": "60 cm",
    },
    {
        "name": "Michael",
        "height": "178 cm",
        "mass": "75 kg",
    },
    ]
}

Index file

index.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">

        <title>Character</title>

    </head>

    <body style="margin-bottom:20em;">
        <h1>Characters</h1>
            <?php include('characters.php'); ?>

            <?php 
            $data = file_get_contents("databas.json");
            $data = json_decode($data, true);

            echo '<pre>';
            print_r($data);
            echo '</pre>';

            ?>
    </body>
</html>

Class code

characters.php

<?php

class Character {

        private $name;

public function __construct($name) {
            $this->name = $name;

        }
public function getName($name){
        return $this->name;
        }

}
 ?>

The $data content are

Array
(
    [results] => Array
        (
            [0] => Array
                (
                    [name] => John
                    [height] => 182 cm
                    [mass] => 80 kg
                )

            [1] => Array
                (
                    [name] => Sarah
                    [height] => 165 kg
                    [mass] => 60 cm
                )

            [2] => Array
                (
                    [name] => Michael
                    [height] => 178 cm
                    [mass] => 75 kg
                )

        )

)

I am not sure if I get you rigth but you can do simple foreach loop:

$data = file_get_contents("databas.json");
$data = json_decode($data, true);

foreach($data["result"] as $e) {
    echo "Name: " . $e["name"];
}

Notice that send true in json_decode return associative array so you can access the name key with [key] so no need in include('characters.php');

Edit : If you want to use the class:

foreach($data["result"] as $e) {
    $character = new Character($e["name"]);
    $characters[] = $character;
}

foreach($characters as $character) {
    echo "Name: " . $character->getName();
}    

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