画像を削除するボタンが、機能しますが、削除ボタンを押したら、
画像は削除されますが、
画像だけ消えて削除ボタンだけ表示される段ができてしまい、更新ボタンを押さないとちゃんと消えません。
更新ボタンを押さなくても削除ボタンを押したらちゃんと消えるようにしたいです。
php
1<?php 2$images = array (); 3// 1ページに表示する画像の枚数 4$num = 3; 5 6// albumフォルダをオープンにする 7// $handleにディレクトリハンドルが入っている 8// opendir(フォルダ名)⇒戻り値ディレクトリハンドル 9if ($handle = opendir ( './album' )) { 10// readdir(ディレクトリハンドル)⇒指定したディレクトリのファイル一覧を取得する 11// ファイル名をどんどん取得する 12 while ( $entry = readdir ( $handle ) ) { 13 // 現在のディレクトリの.or..を除いた全てのファイル名リストを 14 // $imagesに格納している 15 if ($entry != "." && $entry != "..") { 16// $entry⇒readdirで取得したファイル名が沢山格納されている。 17// それを$images配列に格納。 18 $images [] = $entry; 19 } 20 } 21 // ディレクトリハンドルをクローズする 22 closedir ( $handle ); 23 24 25 rsort($images); 26 27 28 //古い順ボタン 29if (isset($_POST ['sort'])) { 30 switch ($_POST['sort']): 31 case 'fromOldest': 32 sort ( $images ); 33 break; 34 case 'fromNewest': 35 rsort ( $images ); 36 break; 37 endswitch; 38} 39 40?> 41 42<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 43"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 44<html xm1ns="http://www.w3.org/1999/xhtml"> 45<html lang="ja"> 46<head> 47<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 48<meta http-equiv="Content-Script-Type" content="text/javascript"> 49<meta name="viewport" content="width=device-width, 50 initial-scale=1.0,user-scalable=yes" /> 51 52<title>アルバム</title> 53 54</head> 55<body> 56 <h1 class="title_a">アルバム</h1> 57 <p class="textstyle"> 58 <a href="upload2.php">写真をアップロードする</a> 59 </p> 60 <?php 61 if(!(isset($_POST['command'])&& $_POST['command']=='delete')){ 62 echo '<p class="textstyle">削除ボタンを押すと、画像が削除出来ます。</p>'; 63 } 64 ?> 65 <?php 66if(isset($_POST['command'])){ 67if(!empty($_POST['command']) && $_POST['command'] == 'delete'){ 68 $filename = './album/'.$_POST['id']; 69 if(file_exists($filename)){ 70 unlink($filename); 71// header ("Location:album2.php"); 72// exit(); 73 echo ' <p class="textstyle">削除しました。更新ボタンを押してください。</p>'; 74 echo '<FORM><INPUT TYPE="button" VALUE="更新" onClick="window.location.reload();"></FORM>'; 75 unset($_POST); 76 } 77} 78} 79?> 80<table bordercolor="hotpink" border="1" cellspacing="0" cellpadding="5"> 81 <tr> 82 <td bgcolor="mistyrose" align="center"><font color="deeppink" 83 size="2"><font size="1"><sup>●</sup></font> <b>A L B U M</b> <font 84 size="1"><sub>●</sub></font></font></td> 85 </tr> 86 <tr> 87 <td align="center"> 88 <!-- <caption class="font2">アルバム</caption> --> 89<?php 90if(count($images) > 0){ 91 $images = array_chunk ( $images, $num ); 92 $page = 0; 93 if (isset ( $_GET ['page'] ) && is_numeric ( $_GET ['page'] )) { 94 $page = intval ( $_GET ['page'] ) - 1; 95 if (! isset ( $images [$page] )) { 96 $page = 0; 97 } 98 } 99 100// if(!empty($_POST['command']) && $_POST['command'] == 'delete'){ 101 // 画像の表示 102 foreach ( $images [$page] as $img ) { 103 echo '<form action="" method="post">'; 104 echo '<tr>'; 105 echo '<td align="center"><img src="./album/' . $img . '"class="album_image"><input type="hidden" name="command" value="delete"><input type="hidden" name="id" value="',$img,'">',' <button type="submit" style="border: 1px solid #902342; color: #902342; background-color: #f2dae8;">削除</button></td>'; 106 echo '</tr>'; 107 echo '</form>'; 108 echo "\n"; 109 } 110// } 111 echo '<p>'; 112 for($i = 1; $i <= count ( $images ); $i ++) { 113 echo '<a href="album2.php?page=' . $i . '">' . $i . '</a> '; 114 } 115 echo '</p>'; 116} else { 117 echo '<p class="textstyle">画像はまだありません。</p>'; 118} 119} 120?> 121<tr> 122<td bgcolor="mistyrose" align="center"><font color="deeppink"><font size="1"><sup>●</sup><sub>●</sub><sup>●</sup><sub>●</sub><sup>●</sup><sub>●</sub><sup>●</sup><sub>●</sub><sup>●</sup></font></td> 123</tr> 124 125</table> 126<p> 127<form action="" method="post"> 128 129<?php 130if(!(isset($_POST['sort'])) || $_POST['sort'] == 'fromNewest'){ 131// 古い順というボタンを表示する 132 echo '<input type="hidden" name="sort" value="fromOldest"> <button type="submit" style="border: 1px solid #902342; color: #902342; background-color: #f2dae8;">古い順</button>'; 133}elseif (!isset($_POST['sort']) || $_POST['sort'] == 'fromOldest'){ 134// 新しい順というボタンを表示する 135 echo '<input type="hidden" name="sort" value="fromNewest"><button type="submit" style="border: 1px solid #902342; color: #902342; background-color: #f2dae8;">新しい順</button>'; 136} 137 138?> 139 </form> 140 </p> 141</body> 142</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/22 02:39