やりたいこと
dbにあるパスから画像を一覧表示して画像をクリックした時にクリックした画像のページに表示したいです。
現状
どの画像をクリックしても最後のidの画像ページが表示されてしまいます。
コード
db接続は省いてますが接続自体は問題ありません。
画像はディレクト内、db内には画像パス、idなどがあります。
php
1//画像一覧ページ 2 session_start(); 3 $userId = $_SESSION["userId"]; 4 5 $stmt = $mysqli->prepare("SELECT * FROM images where userId = ?"); 6 if($stmt->execute(array($userId))){ 7 while($row = $stmt->fetch()){ 8 9 $url = "../../contents/illust/" . $row["url"]; 10 $id = $row["id"]; 11 $_SESSION["id"] = $id; 12 echo '<a href="../post/php/loadillust.php"><img src="'.$url.'" alt="'.$row['title'].'" /></a>'; 13 } 14 }
php
1//画像表示ページ 2 session_start(); 3 4 $id = $_SESSION["id"]; 5 6 $sql = "SELECT * FROM images WHERE id='$id'"; 7 8 $stmt = $mysqli->prepare($sql); 9 $stmt->execute(); 10 $row = $stmt->fetch(PDO::FETCH_ASSOC); 11 $stmt->execute(); 12 13 while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 14 $url = $row['url']; 15 $type = $row["type"]; 16 } 17 $stmt = null; 18 19 $img = file_get_contents('../../../contents/illust/' . $url); 20 21 if($type == "jpeg"){ 22 header("Content-type: image/jpeg"); 23 echo $img; 24 exit; 25 }elseif($type == "png"){ 26 header("Content-type: image/png"); 27 echo $img; 28 exit; 29 }else{ 30 echo "システムエラー"; 31 }
回答1件
あなたの回答
tips
プレビュー