Skip to content

Instantly share code, notes, and snippets.

@murachi1208
Last active February 24, 2020 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murachi1208/47f85d0a8ccd8a8a5000adeb5b4ece5d to your computer and use it in GitHub Desktop.
Save murachi1208/47f85d0a8ccd8a8a5000adeb5b4ece5d to your computer and use it in GitHub Desktop.
Slim 3 Framework でMySQLとテンプレつかってみる。その2 ref: https://qiita.com/murachi1208/items/bef385f76d055388179f
<table class="pure-table">
<tr>
<th>id</th>
<th>title</th>
<th>name</th>
</tr>
<?php foreach($data['books'] as $test): ?>
<tr>
<td><?=$test['id'] ?></td>
<td><?=$test['title'] ?></td>
<td><?=$test['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
// MySQL
$container['db'] = function ($c) {
$settings = $c->get('settings')['db'];
$pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'] . ";port=" . $settings['port'],
$settings['user'], $settings['pass']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
sudo yum --enablerepo=epel,remi,remi-php70 install php70 php70-php-mcrypt php70-php-mbstring php70-php-fpm php70-php-gd php70-php-pecl-xdebug php70-php-pecl-redis php70-php-pecl-imagick-devel php70-php-pecl-imagick php70-php-mysqlnd php70-php-intl php70-php-bcmath php70-php-pecl-zip php70-php-xmlrpc php70-php-xml php70-php-pecl-http php70-php-pecl-http-devel php70-php-opcache phpunit
$ composer create-project slim/slim-skeleton slim3
array(3) { [0]=> array(4) { ["id"]=> string(1) "1" ["title"]=> string(15) "双子の帝國" ["name"]=> string(24) "ふたごのていこく" ["price"]=> string(3) "300" } [1]=> array(4) { ["id"]=> string(1) "2" ["title"]=> string(15) "進撃の巨人" ["name"]=> string(7) "singeki" ["price"]=> string(3) "500" } [2]=> array(4) { ["id"]=> string(1) "3" ["title"]=> string(21) "となりの関くん" ["name"]=> string(24) "となりのせきくん" ["price"]=> string(5) "50000" } }
id title name
1 双子の帝國 ふたごのていこく
2 進撃の巨人 singeki
3 となりの関くん となりのせきくん
slim3
├── composer.json
├── composer.lock
├── classes ← こいつだけ作成した
├── logs
├── public
│   └── index.php
├── src
│   ├── dependencies.php
│   ├── middleware.php
│   ├── routes.php
│   └── settings.php
├── templates
│   └── index.phtml
└── vendor
CREATE TABLE `book` (
`id` int(11) NOT NULL,
`title` varchar(64) DEFAULT NULL,
`name` varchar(32) DEFAULT NULL,
`price` int(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO `book` VALUES (1,'双子の帝國','ふたごのていこく',300),(2,'進撃の巨人','singeki',500),(3,'となりの関くん','となりのせきくん',50000);
// class load
spl_autoload_register(function ($classname) {
require (__DIR__ . "/../classes/" . $classname . ".php");
});
abstract class Mapper {
protected $db;
public function __construct($db) {
$this->db = $db;
}
}
$app->get('/book_api2', function ($request, $response, $args) {
$mapper = new TestMapper($this->db);
$test = $mapper->getTests();
$response = $this->renderer->render($response, "book_api.phtml", ["books" => $test]);
});
// DataBase(MySQL) settings
'db' => [
'host' => 'hostname',
'port' => '3306',
'user' => 'user',
'pass' => 'passwd',
'dbname' => 'test',
],
class TestMapper extends Mapper
{
public function getTests() {
$sql = "select * from book";
$stmt = $this->db->query($sql);
$results = [];
while($row = $stmt->fetch()) {
$results[] = $row;
}
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment