Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active August 24, 2024 05:11
Show Gist options
  • Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
Save JeffreyWay/33491872094b136b96758018cdd7b76c to your computer and use it in GitHub Desktop.
<!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>
<!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>
@Elisha-Sani
Copy link

`

<title>Demo</title>
<h1>My Favorite Movies</h1>

<?php

    $movies = [

        [
            'title' => 'The Shawsshank Redemption',
            'year' => 1994,
            'director' => 'Frank Darabont'
        ],
        [
            'title' => 'Pulp Fiction',
            'year' => 1994,
            'director' => 'Quentin Tarantino'
        ],
        [
            'title' => 'The Matrix',
            'year' => 1999,
            'director' => 'Lana Wachowski, Lilly Wachowski'
        ],
        [
            'title' => 'Fight Club',
            'year' => 1999,
            'director' => 'David Fincher'
        ],
        [
            'title' => 'Gladiator',
            'year' => 2000,
            'director' => 'Ridely Scott'
        ],
        [
            'title' => 'The Lord of the Rings: The Fellowship of the Ring',
            'year' => 2001,
            'director' => 'Peter Jackson'
        ],
        [
            'title' => 'City of God',
            'year' => 2002,
            'director' => 'Fernando Meirelles, Katia Lund'
        ],
        [
            'title' => 'The Dark Knight',
            'year' => 2008,
            'director' => 'Christopher Nolan'
        ],
        [
            'title' => 'No Country for Old Men',
            'year' => 2007,
            'director' => 'Ethan Coen, Joel Coen'
        ],
        [
            'title' => 'The Departed',
            'year' => 2006,
            'director' => 'Martin Scorsese'
        ]

    ];

    function filterMovieByYear($movies, $year){
        $filteredMovies = [];

        foreach($movies as $movie) {
            if ($movie['year'] > $year){
                $filteredMovies[] = $movie;
            }
        }

        return $filteredMovies;
    }
?>

<ul>
        <?php foreach(filterMovieByYear($movies, 2000) as $movie) : ?>
            <li><p><?=$movie['title']?> (<?=$movie['year']?>)</p> - By <h5><?=$movie['director']?></h5></li>

        <?php endforeach; ?>
</ul>
![Screenshot 2024-07-18 194213](https://gist.github.com/user-attachments/assets/ecf42972-f757-4e33-9c81-bfe033e9fe6b) `

@Sajjalh7
Copy link

<!doctypehtml>

<title> demo </title> 'Harry Potter' 'writer' => 'idk lol' 'purchaseUrl' => 'https://example.com' ] ]; ?>

@ShinjiX-Web
Copy link

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_one

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