prepare($query); $stmt -> execute(); $news_sec = ''; while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $news_id = $row['news_id']; $news_title = $row['news_title']; $news_text = $row['news_text']; $news_img = $row['news_img']; $img_dir = $row['img_dir']; $main_img = $row['main_img']; $category_id = $row['category_id']; $category_name = $row['category_name']; $post_date = $row['post_date']; // 2015-11-30 00:00:00 $post_date = substr($post_date, 0, 10); // 2015-11-30 $week = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"); $date = strtotime($post_date); $w = date("w", $date); //echo $week[$w]; //$news_sec .= '' . "\n"; $news_sec .= '
' . "\n"; $news_sec .= '

' . $news_title . '

' . "\n"; $og_title = $news_title; $og_description = "ALGOからのお知らせ"; $og_site_name = TITLE; $og_image = 'http://www.algo-ogura.com/img/news/up/' . $img_dir . '/' . $main_img; $news_sec .= '
' . "\n"; //$news_sec .= '

' . "\n"; $news_sec .= $news_text . "\n"; //$news_sec .= '

' . "\n"; $news_sec .= '
' . "\n"; $news_sec .= '

' . $post_date . ' ' . $week[$w] . ' / ' . $category_name . '

' . "\n"; $news_sec .= '
' . "\n\n"; } // 全記事数 $query = "SELECT * FROM " . NEWS; $stmt = $pdo -> prepare($query); $stmt -> execute(); $result = $stmt -> fetchAll(); $limit = count($result); // 全記事数 // 先頭のデータを取得 $query = "SELECT * FROM " . NEWS . " LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_first = $row['news_id']; } //echo "もっとも古いidは" . $id_first; // 最後のデータ取得 $query = "SELECT * FROM " . NEWS . " order by news_id desc LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_last = $row['news_id']; } //echo "もっとも新しいidは" . $id_last; // prev back btnの制御 $btn = ''; if($limit == 1){ // 記事が一つしかないのでボタンはない } else if($news_id == $id_first){ // 先頭のデータ // 一番最初に追加された最も古い記事なので、前はない // その次のデータ $query = "SELECT * FROM " . NEWS . " WHERE news_id > " . $news_id . " order by news_id asc LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_next = $row['news_id']; } //$btn .= '

' . "\n"; $btn .= '
次へ→
' . "\n"; } else if ($news_id == $id_last) { // 最後のデータ(最後に追加されたデータ = 最新) // その前(ひとつ古い)のデータ $query = "SELECT * FROM " . NEWS . " WHERE news_id < " . $news_id . " order by news_id desc LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_prev = $row['news_id']; } //$btn .= '

' . "\n"; $btn .= '
←前へ
' . "\n"; } else { // 通常のデータ // その次のデータ $query = "SELECT * FROM " . NEWS . " WHERE news_id > " . $news_id . " order by news_id asc LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_next = $row['news_id']; } // その前のデータ $query = "SELECT * FROM " . NEWS . " WHERE news_id < " . $news_id . " order by news_id desc LIMIT 1"; $stmt = $pdo -> prepare($query); $stmt -> execute(); while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){ $id_prev = $row['news_id']; } //$btn .= '

' . "\n"; //$btn .= '

' . "\n"; $btn .= '
←前へ次へ→
' . "\n"; } } catch(PDOException $e){ var_dump($e -> getMessage()); } require_once($root . 'view/head.php'); ?>