Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active November 1, 2024 12:04
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>
@CaioCesarP
Copy link

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Books</title>
</head>

<style>
  body {
    font-family: Arial, sans-serif;
  }

  container {
    display: grid;
    place-items: center;
  }

</style>

<body>
  <?php
    $books = [
      [
        "id" => 1,
        "cover" => null,
        "name" => "Dark Matter",
        "author" => "Blake Crouch",
        "releaseYear" => 2016,
        "purchaseUrl" => "http://example.com",
        "summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
        "readed" => false,
        "rating" => 4.8
      ],
      [
        "id" => 2,
        "cover" => null,
        "name" => "The Catcher in the Rye",
        "author" => "J. D. Salinger",
        "releaseYear" => 1951,
        "purchaseUrl" => "http://example.com",
        "summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
        "readed" => true,
        "rating" => 4.8
      ],
      [
        "id" => 3,
        "cover" => null,
        "name" => "To Kill a Mockingbird",
        "author" => "Harper Lee",
        "releaseYear" => 1960,
        "purchaseUrl" => "http://example.com",
        "summary" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vel nisi nec velit consectetur dignissim. In non ipsum sed ipsum gravida tristique. Sed euismod, enim ut pulvinar cursus, nisi felis rutrum ex, id viverra justo felis non massa.",
        "readed" => false,
        "rating" => 3.7,
      ],
    ];
  ?>

  <div class="container">
    <ul class="books list">
      <?php foreach ($books as $book) :?>
        <li id="book-<?= $book['id'];?>" class="book">
          <div id="book-cover"><?= $book['cover']?></div>
          <a class="book-name" href="<?= $book['purchaseUrl'];?>">
            <?= $book['name'];?> (<?= $book['releaseYear'];?>)
          </a>
          <p class="book-summary"><?= $book['summary'];?></p>
          <p class="book-rating">Rating: <?= $book['rating'];?></p>
          <div class="book-controls">
            <input type="checkbox" class="book-read-checkbox" <?= $book['readed']? 'checked' : '';?>>
            <label for="book-read-checkbox">Read</label>
          </div>
        </li>
      <?php endforeach;?>
    </ul>
    </ul>
  </div>

</body>

</html>

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