Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active October 3, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyWay/52d403ca86885391d6048b890218f076 to your computer and use it in GitHub Desktop.
Save JeffreyWay/52d403ca86885391d6048b890218f076 to your computer and use it in GitHub Desktop.
PHP for Beginners, Episode 10 - Separate Logic From the Template https://laracasts.com/series/php-for-beginners-2023-edition/episodes/10
<?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'
],
[
'name' => 'The Martian',
'author' => 'Andy Weir',
'releaseYear' => 2011,
'purchaseUrl' => 'http://example.com'
],
];
$filteredBooks = array_filter($books, function ($book) {
return $book['releaseYear'] >= 1950 && $book['releaseYear'] <= 2020;
});
require "index.view.php";
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
</head>
<body>
<ul>
<?php foreach ($filteredBooks as $book) : ?>
<li>
<a href="<?= $book['purchaseUrl'] ?>">
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>) - By <?= $book['author'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment