Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active July 7, 2024 23:45
Show Gist options
  • Save JeffreyWay/40fcd41c03de5805aedc62ac847094df to your computer and use it in GitHub Desktop.
Save JeffreyWay/40fcd41c03de5805aedc62ac847094df to your computer and use it in GitHub Desktop.
PHP For Beginners, Episode 7 - Associative Arrays https://laracasts.com/series/php-for-beginners-2023-edition/episodes/7
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'releaseYear' => 1968,
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'releaseYear' => 2021,
'purchaseUrl' => 'http://example.com'
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?> (<?= $book['releaseYear'] ?>)
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<?php
$books = [
[
'name' => 'Do Androids Dream of Electric Sheep',
'author' => 'Philip K. Dick',
'purchaseUrl' => 'http://example.com'
],
[
'name' => 'Project Hail Mary',
'author' => 'Andy Weir',
'purchaseUrl' => 'http://example.com'
]
];
?>
<ul>
<?php foreach ($books as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
@AdhiDevX369
Copy link

AdhiDevX369 commented Nov 26, 2023

<body>
    <?php
    $books = [
        [
            'name' => 'Asahara',
            'author' => 'Imesh Madhushanka Yapa',
            'urlToBuy' => 'www.buybooks.com/asahara',
            'releasedYear' => '2022'
        ],
        [
            'name' => 'Game Of thrones',
            'author' => 'George R. R. Martin',
            'urlToBuy' => 'www.buybooks.com/gameofthrones',
            'releasedYear' => '1996'
        ], [
            'name' => 'Forgot me or Not',
            'author' => 'Charitha Prawardhi Bandara, Chathuri Damayanthi',
            'urlToBuy' => 'www.buybooks.com/forgotmeornot',
            'releasedYear' => '2023'

        ], [
            'name' => 'Dark Matter',
            'author' => 'Blake Crouch',
            'urlToBuy' => 'www.buybooks.com/DarkMatter',
            'releasedYear' => '2016'

        ]
    ];
    ?>

    <ul>
        <?php foreach ($books as $book) : ?>
            <li>
                <a href="<?= $book['urlToBuy']; ?>">
                    <?= $book['name']; ?>
                </a>
                <?= $book['releasedYear']; ?>
            </li>
        (<?php endforeach; ?>)
    </ul>
</body>
</html>

@Shimazaky80
Copy link

My solution:

`

<title>Demo</title> <style> body { display: grid; place-items: center; /* height: 100vh; */ margin: 0; font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; } </style>
<h1>Recommended Books</h1>

<?php
    // List of recommended books
    $books = [

        [
            "name" => "Do Androids Dream of Electric Sheep",
            "releaseYear" => 1968,
            "author" => "Philip K. Dick",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "The Langoliers",
            "releaseYear" => 1995,
            "author" => "Andy Weir",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "Project Hail Mary",
            "releaseYear" => 2021,
            "author" => "Jonh Doe",
            "purchaseUrl" => "http://example.com"
        ],
        [
            "name" => "Animal Farm",
            "releaseYear" => 1945,
            "author" => "Another Jonh Doe",
            "purchaseUrl" => "http://example.com"
        ]
        
    ];
?>

<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book["purchaseUrl"] ?>">
                <?= $book["name"] ?>
                (<?= $book["releaseYear"] ?>)
            </a>
        </li>
    <?php endforeach; ?>
</ul>
`

@spacemark3
Copy link

<?php $books = [ [ 'name' =>'Do Androids Dream of Electric Sheep', 'year' => '1980', 'author' => 'Philip K. Dick', 'purchaseUrl' =>'http://example1.com' ], [ 'name' => 'Project Hail Mary', 'year' => '2000', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://example2.com' ], [ 'name' => 'The Langoliers', 'year' => '1950', 'author' => 'Mark Andro', 'purchaseUrl' => 'http://example3.com' ], [ 'name' => 'The shadow of the wind', 'year' => '2001', 'author' => 'Carlos Ruiz Zafon', 'purchaseUrl' => 'http://example4.com' ], ]; ?> <ul> <?php foreach($books as $book) : ?> <li> <a href="<?= $book['purchaseUrl'] ?>"> <?= $book['name']; ?> <?= $book['year']; ?> </a> </li> <?php endforeach; ?> </ul>

@K1LLERB0T
Copy link

<h1>Recommended Books</h1>
<?php
    $books = [
        [
            'name' => 'Do Androids Dream of Electric Sheep?',
            'author' => 'Phillip K. Dick',
            'releaseYear' => '1968',
            'purchaseUrl' => 'example.com'
        ],
        [
            'name' => 'The Langoliers',
            'author' => 'Stephen King',
            'releaseYear' => '1990',
            'purchaseUrl' => 'example.com'
        ],
        [
            'name' => 'Project Hail Mary',
            'author' => 'Andy Weir',
            'releaseYear' => '2021',
            'purchaseUrl' => 'example.com'
        ]
    ]
?>

<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
            <?= $book['name']; ?>
            </a>
            <?= $book['releaseYear']; ?>
        </li> 
    <?php endforeach; ?>
</ul>

@Mickish1
Copy link

Mickish1 commented Apr 29, 2024

<title>FUN STUFFS</title>

Recommended List

'Do Androids Dream of Electric Sheep', 'author' => 'John Doe', 'purchaseUrl' => 'http://pavilion.com', 'releaseDate' => '2024' ], ['name' => 'The Extremist', 'author' => 'Michael Fasina', 'purchaseUrl' => 'http://pavilion.com', 'releaseDate' => '2021'], ['name' => 'Project Hail Mary', 'author' => 'Andy Weir', 'purchaseUrl' => 'http://fashmichael.com', 'releaseDate' => '2022' ] ] ?>

@binsarkiel
Copy link

<!doctype html>
<html lang="en">
<head>
    <title>Demo</title>
    <meta charset="UTF-8">
</head>
<body>

<h1>Recommended Books</h1>

<?php
    $books = [
            [
                    'name' => 'Do Androids Dream of Electric Sheep',
                    'author' => 'Philip K. Dick',
                    'releaseYear' => 1968,
                    'purchaseUrl' => 'https://example.com'
            ],
            [
                    'name' => "Project Hail Mary",
                    'author' => "Andy Weir",
                    'releaseYear' => 2021,
                    'purchaseUrl' => "https://example.com"
            ]
    ];
?>

<ul>
    <?php foreach($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
            <?= $book['name'] ?> (<?= $book['releaseYear'] ?>)
            </a>
            - <?= $book['author'] ?>
        </li>
    <?php endforeach; ?>
</ul>

</body>
</html>

@Rofiaziztr
Copy link

<!doctype html>

<title>Demo</title> 'The Psychology of Money', 'author' => 'Morgan Housel', 'releaseDate' => '2020', 'purchaseUrl' => 'http://example.com' ], [ 'name' => 'Introducing Web Development', 'author' => 'Jörg Krause', 'releaseDate' => '2016', 'purchaseUrl' => 'http://example.com' ] ]; ?>
<ul>
    <?php foreach ($books as $book) : ?>
        <li>
            <a href="<?= $book['purchaseUrl'] ?>">
                <?= $book['name'].' '.$book['releaseDate'] ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment