Last active
August 24, 2024 05:11
-
-
Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Demo</title> | |
</head> | |
<body> | |
<?php | |
$movies = [ | |
[ | |
'name' => 'Back to the Future', | |
'releaseYear' => 1985, | |
], | |
[ | |
'name' => "Weekend at Bernie's", | |
'releaseYear' => 1989, | |
], | |
[ | |
'name' => 'Pirates of the Caribbean', | |
'releaseYear' => 2003, | |
], | |
[ | |
'name' => 'Interstellar', | |
'releaseYear' => 2014, | |
], | |
]; | |
// Extra Credit: | |
// Research and apply the array_filter() function on your own. | |
function filterByRecent($movies) | |
{ | |
$filteredMovies = []; | |
foreach ($movies as $movie) { | |
if ($movie['releaseYear'] >= 2000) { | |
$filteredMovies[] = $movie; | |
} | |
} | |
return $filteredMovies; | |
} | |
?> | |
<ul> | |
<?php foreach (filterByRecent($movies) as $movie) : ?> | |
<li> | |
<?= $movie['name'] ?> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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' | |
], | |
[ | |
'name' => 'The Martian', | |
'author' => 'Andy Weir', | |
'releaseYear' => 2011, | |
'purchaseUrl' => 'http://example.com' | |
], | |
]; | |
function filterByAuthor($books, $author) | |
{ | |
$filteredBooks = []; | |
foreach ($books as $book) { | |
if ($book['author'] === $author) { | |
$filteredBooks[] = $book; | |
} | |
} | |
return $filteredBooks; | |
} | |
?> | |
<ul> | |
<?php foreach (filterByAuthor($books, 'Philip K. Dick') as $book) : ?> | |
<li> | |
<a href="<?= $book['purchaseUrl'] ?>"> | |
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>) - By <?= $book['author'] ?> | |
</a> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</body> | |
</html> |
Added some select option.. Just for fun...
<body>
<div class="container">
<div class="sub-container">
<h1>
<?php
$name = "The Dark Matter!";
$read = false;
if ($read) {
$message = "You have read $name";
} else {
$message = "You have not read $name";
}
echo $message;
?>
</h1>
<h2>Recommended Books</h2>
<div class="select-container">
<label class="my-author" for="author">Author: </label>
<select name="custom_author" id="author">
<option value="">--Please choose an option--</option>
<option value="all">All</option>
<option value="nate">Nate Panares</option>
<option value="will">Will Smith</option>
</select>
</div>
<?php
$books = [
[
'name' => "The Orly Farm",
'author' => "Nate Panares",
'releaseYear' => 2000,
'url' => "https://laracasts.com/"
],
[
'name' => "Magneto Revenge",
'author' => "Will Smith",
'releaseYear' => 1987,
'url' => "https://laracasts.com/"
],
[
'name' => "Dark Knight",
'author' => "Will Smith",
'releaseYear' => 2020,
'url' => "https://laracasts.com/"
],
[
'name' => "Superman Kryptonite",
'author' => "Will Smith",
'releaseYear' => 2024,
'url' => "https://laracasts.com/"
],
[
'name' => "Bayaning Bulag",
'author' => "Will Smith",
'releaseYear' => 1984,
'url' => "https://laracasts.com/"
]
];
function filterByAuthor($books, $author)
{
$filteredbooks = [];
foreach ($books as $book) {
# code...
if ($book['author'] === $author) {
# code...
$filteredbooks[] = $book;
}
}
return $filteredbooks;
}
?>
<div class="mini-container">
<ul>
<?php foreach (filterByAuthor($books, 'Will Smith') as $book) : ?>
<li>
<a href="<?= $book['url'] ?>" target="_blank">
<?= $book['name']; ?> (<?= $book['releaseYear'] ?>)
- By: <?= $book['author'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</body>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`
<title>Demo</title>  `