SSブログ

ついに一覧表示 [php]

これでとりあえず機能は果たせます。デザインは知りません。
例によってこのサイトで変換します。

解説は仕事が終わったら少し書いてみます。
<?php
require_once('./db_accesse.php');
ini_set('display_errors', "On");
error_reporting(E_ALL);

try {
  $db = new DB();
  $sql = "SELECT *FROM details";
  $res = $db->executeSQL($sql,null);

  $recordlist = "<table border='2' class='table_01'><th class='th_01'>ID</th><th class='th_01'>題名</th><th class='th_01'>著者</th><th class='th_01'>出版社</th><th class='th_01'>出版年</th>\n";
  while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
    $recordlist .= "<tr class='tr_01'><td>{$row['id']}</td>";
    $recordlist .= "<td><a href='book_information.php?id={$row['id']}'>{$row['title']}</td>";
    $recordlist .= "<td>{$row['author']}</td>";
    $recordlist .= "<td>{$row['publisher']}</td>";
    $recordlist .= "<td>{$row['pubdate']}</a></td></tr>";
  }
  $recordlist .= "</table>\n";
} catch (Exception $e) {
  $res = $e->getMssage();
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>蔵書一覧</title>
  <script>
     $('tr[data-href]').addClass('clickable')
       .click(function (e) {
         if (!$(e.target).is('a')) {
           window.location = $(e.target).closest('tr').data('href');
         };
     });
 </script>
</head>
<body>
  <nav>
       <div class="logo">
            <h1><a href="./"> 書籍管理<簡易></a></h1>
       </div>
  </nav>
  <div class="search">
    <h2>蔵書検索</h2>
    <form action="search.php?=" method="get">
      <input type="search" name="search">
      <input type="submit">
  </div>
  <div class="books">
  <h1>蔵書一覧</h1><br />
  <?php echo $recordlist;?>
</div>
</body>
</html>


nice!(0)  コメント(0) 

書籍データをDBに登録 [php]

バーコードリーダーで読み込んだISBNを基に情報をDBに登録していきます。


<?php
//準備
ini_set('display_errors', "On");
error_reporting(E_ALL);

$id = 0;
//接続用
$USER = 'USER';
$PW ='PASS';
$dnsinfo = "mysql:dbname=DBNAME;host=localhost;charset=utf8";


//入力が確認された場合に処理を実行
if(isset($_POST["ISBN"])){
	$ISBN = $_POST["ISBN"];
	$data = "https://api.openbd.jp/v1/get?isbn={$ISBN}";
	$json = file_get_contents($data);
	$json_decode = json_decode($json,true);

//変数へ格納
	$BookTitle = $json_decode[0]['summary']['title'];
	$author = $json_decode[0]['summary']['author'];
	$publisher = $json_decode[0]['summary']['publisher'];
	$pubdate = $json_decode[0]['summary']['pubdate'];
	$CoverUrl = $json_decode[0]['summary']['cover'];

if ($BookTitle == "") {
	echo "情報を取得できませんでした。\nデータを追加できません。";
	$recordlist ="";
}else {
//データベースへの接続及び追加
	try {
	  $pdo = new PDO($dnsinfo,$USER,$PW);
		$sql = "INSERT INTO details VALUES(?,?,?,?,?,?)";
	  $stmt = $pdo->prepare($sql);
	  $array = array($id,$BookTitle,$author,$publisher,$pubdate,$CoverUrl);
	  $res = $stmt->execute($array);

	} catch (Exception $e) {
	  $res = $e->getMssage();
	}
}



if ($CoverUrl == "") {
	$CoverUrl = "書影が見つかりませんでした";
}

//表示するものども
	$table =  "<table border='1'>";
	$table .= "<tr><th colspan='2'>書籍情報</th></tr>";
	$table .= "<tr><td colspan='2'><img src='{$CoverUrl}'></td></tr>";
	$table .= "<tr><td>タイトル</td><td>{$BookTitle}</td></tr>";
	$table .= "<tr><td>著者</td><td>{$author}</td></tr>";
	$table .= "<tr><td>出版社</td><td>{$publisher}</td></tr>";
	$table .= "<tr><td>出版年</td><td>{$pubdate}</td></tr>";
	$table .= "</table>";

}else {
	$result = "";
	$table = "";
	$recordlist ="";
}
$pdo2 = new PDO($dnsinfo,$USER,$PW);
$sql2 ="SELECT *FROM details";
$stmt2 = $pdo2->prepare($sql2);
$stmt2->execute(null);
$recordlist = "<table border='2'><th>ID</th><th>題名</th><th>著者</th><th>出版社</th><th>出版年</th>\n";
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
	$recordlist .= "<tr><td>{$row['id']}</td>";
	$recordlist .= "<td>{$row['title']}</td>";
	$recordlist .= "<td>{$row['author']}</td>";
	$recordlist .= "<td>{$row['publisher']}</td>";
	$recordlist .= "<td>{$row['pubdate']}</td></tr>";
}
$recordlist .= "</table>\n";
?>
<!DOCTYPE html>
<html lang="ja">
	<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="style.css">
	<title>書籍管理システム</title>
	<script>document.book_info.ISBN.focus();</script>
	</head>
<body>
	<nav>
		<div class="logo">
			<h1><a href="./index.php"> 書籍管理<簡易></a></h1>
		</div>

	</nav>
	<h1>書籍追加</h1>
	<form action="" method="post">
		<b>ISBNを入力:<input type="textarea" name="ISBN" id=book_info autofocus></b>
		<input type="submit" name="submit">
	</form>
	<?php echo $result;?>
	<?php echo $table;?>
	<button name='regist'>登録</button>
	<h1>データ一覧</h1>
	<?php echo $recordlist;?>
</body>
</html>



nice!(0)  コメント(0) 

phpでDB接続 [php]

csvとかで管理して表示とかでもできるのかもしれませんが、やっぱりデータベース使うと格好いい(小並感)
そんなわけで


「」で囲まれた部分は各自変えるべし。
ini_set('display_errors', "On");
error_reporting(E_ALL);

class DB{
  private $USER = '「USER_NAME」';
  private $PW = '「PASSWORD」';
  private $dnsinfo = "mysql:dbname=「DBNAME」;host=localhost;charset=utf8";

  private function Connectdb(){
    try{
      $pdo = new PDO($this->dnsinfo,$this->USER,$this->PW);
      return $pdo;
    }catch(Exception $e){
      return false;
    }
  }

    public function executeSQL($sql,$array){
      try{
        if(!$pdo = $this->Connectdb())return false;
        $stmt = $pdo->prepare($sql);
        $stmt->execute($array);
        return $stmt;

      }catch(Exception $e){
        return false;
      }
    }
}
 ?>

nice!(0)  コメント(0) 

書籍管理 [php]

私の蔵書が100冊を超えてきたので整理するためにバーコードリーダーを買いましたー。
こんな感じのやつ。





こいつを使って書籍管理をしていきたいと思います。
構成としてはphpで登録ページを作ってバーコードリーダーで読み込むとISBNから書籍情報を検索してタイトル、著者名、出版社、出版年を取得。書影とともにデータベースに保存。といった形です。
そしてトップページには書籍をずらっと一覧表示していきます。

ではそのコードは次の記事で。
nice!(0)  コメント(0) 

phpMyAdminエラー [php]

phpMyAdminを久しぶりに開こうと思ったら以下のエラー。

session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13) session_start(): Failed to read session data: files (path: /var/opt/php/lib/session)

二行目の所にあるsessionというディレクトリを以下で所有者変更。
chown -R nginx:nginx session/

無事に開けましたー。

nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。