Skip to content

Commit f99ef08

Browse files
committed
Add gallery example
1 parent 02e88d2 commit f99ef08

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

examples/gallery/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

examples/gallery/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Really Simple Gallery Example
2+
This is a really simple example which searches characters and display the thumbnails of the results.
3+
4+
## Install dependencies
5+
```sh
6+
cd examples/gallery
7+
composer install
8+
```
9+
10+
## Run PHP web server
11+
```sh
12+
php -S localhost:8888 . -t index.php
13+
```
14+
## Open the example in browser
15+
[http://localhost:8888](http://localhost:8888)

examples/gallery/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"chadicus/marvel-api-client": "^3.0"
4+
}
5+
}

examples/gallery/index.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
5+
use Chadicus\Marvel\Api\Client;
6+
use Chadicus\Marvel\Api\Entities\ImageVariant;
7+
use DominionEnterprises\Util\Arrays;
8+
9+
$nameStartsWith = Arrays::get($_POST, 'nameStartsWith');
10+
?>
11+
12+
<html>
13+
<head>
14+
<style>
15+
label { display: inline-block; width: 140px; text-align: right; }
16+
img { max-width:800px; }
17+
</style>
18+
</head>
19+
<body>
20+
<form method="POST" action="/">
21+
<fieldset>
22+
<legend>Search Characters</legend>
23+
<br />
24+
<label for="nameStartsWith">Name</label>
25+
<input type="text" name="nameStartsWith" size="32" value="<?=$nameStartsWith?>" />
26+
<br />
27+
<input type="Submit" value="Search" />
28+
</fieldset>
29+
</form>
30+
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
31+
<?php
32+
$client = new Client(getenv('PRIVATE_KEY'), getenv('PUBLIC_KEY'));
33+
$dataWrapper = $client->search('characters', ['nameStartsWith' => $nameStartsWith]);
34+
$count = 0;
35+
?>
36+
<h3><?=$dataWrapper->getData()->getTotal()?> characters found</h3>
37+
<table border="1">
38+
<tbody>
39+
<tr>
40+
<?php foreach ($dataWrapper->getData()->getResults() as $character): ?>
41+
<?php if ($count++ % 5 === 0): ?>
42+
</tr><tr>
43+
<?php endif; ?>
44+
<td>
45+
<p><?=$character->getName()?></p>
46+
<a href="<?=$character->getUrls()[0]->getUrl()?>">
47+
<img src="<?=$character->getThumbnail()->getUrl(ImageVariant::PORTRAIT_XLARGE())?>" />
48+
</a>
49+
</td>
50+
<?php endforeach; ?>
51+
</tr>
52+
</tbody>
53+
</table>
54+
<center><?=$dataWrapper->getAttributionHTML()?></center>
55+
<?php endif; ?>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)