Last active
October 26, 2025 07:42
-
-
Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 hidden or 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>
<?php
/* __________________________________________________
Define a Function
Let's start nice and easy. Define a function called add that returns the sum of two arguments you pass in
*/
function addTwoNumbers($num1, $num2) {
return $num1 + $num2;
};
/* __________________________________________________
Fetch an Item
We've defined an empty getFirstMovie function for you. It expects an array of movies and will return the first item. Call the function.
*/
$movies = [
[
'name' => 'Back to the Future',
'releaseYear' => 1985,
],
[
'name' => 'Pirates of the Caribbean',
'releaseYear' => 2003,
],
[
'name' => 'Interstellar',
'releaseYear' => 2014,
],
];
function getFirstMovie($movies)
{
// Write your code here
return $movies[0];
}
/* __________________________________________________
Create a Filter
Write a function called filterByRecent that filters a given array of movies down to only those that were released in the year 2000 or higher. Save the results to a variable called $recentMovies.
*/
$movies = [
[
'name' => 'Back to the Future',
'releaseYear' => 1985,
],
[
'name' => 'Pirates of the Caribbean',
'releaseYear' => 2003,
],
[
'name' => 'Interstellar',
'releaseYear' => 2014,
],
];
function filterByRecent($movies, $year) {
$filteredMovies = [];
foreach ($movies as $movie) {
if ($movie['releaseYear'] >= $year) {
$filteredMovies[] = $movie;
}
}
return $filteredMovies;
};
$recentMovies = $filterByRecent($movies, $year);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

`
<title>Demo</title>  `