シヴァのブログ

UnityやUE4や趣味とかいろいろ...

HTML、PHP、CSS、SQLの簡単なメモ

管理画面を作成するにあたって、
HTML、PHPCSSSQLを使っているので忘れないように、
機能とコードをメモ。


・リダイレクト処理
HTMLにmeta要素を書いて、数秒後に指定サイトに自動移動(リダイレクト)させる方法

<meta http-equiv="refresh" content="0;URL=http://hogehoge.php">


・「index of」を表示させない
さくらインターネットを使っている人のみhelp.sakura.ad.jp


・指定ファイルの読み込み
 ┗文字コード設定(UTF-8
 ┗ヒアドキュメント
 ┗ファビコン(タイトルアイコン)

他のファイルの先頭に、

require("header.php");

を記入して全てのファイルに反映。

▼header.php

<?php
//文字コード設定
mb_http_output('UTF-8');
header('Content-Type: text/html; charset="UTF-8"');

//ヒアドキュメント
print<<<EOF
<title>
    シヴァの管理画面
</title>
<head>
    //ファビコン設定
    <link rel="shortcut icon" href="img/favicon.ico" />
</head>
EOF;
?>


SQLの昇順・降順
ASCC・・・昇順(1,2,3,4...[小→大に表示])
DESC・・・降順(9,8,7,6...[大→小に表示])


・foreach文
ヒアドキュメントとループ文の処理(テーブル表示)

<?php
    require("header.php");
    require("dbconnect.php");

    $sql = sprintf('SELECT * FROM question ORDER BY id ASC');
    $record = mysqli_query($db, $sql) or die(mysqli_error($db));
    while($sqlResult = mysqli_fetch_assoc($record)){
        $id = htmlspecialchars($sqlResult["id"]);
        $category = htmlspecialchars($sqlResult["category"]);
        
        $question[$id]["answer"]   = htmlspecialchars($sqlResult["answer"]);
        $question[$id]["question"] = htmlspecialchars($sqlResult["question"]);
        $question[$id]["select1"]  = htmlspecialchars($sqlResult["select1"]);
        $question[$id]["select2"]  = htmlspecialchars($sqlResult["select2"]);
        $question[$id]["select3"]  = htmlspecialchars($sqlResult["select3"]);
    }
print<<<EOF
    <table border="1" cellpadding="5" cellspacing="0">
        <caption>【{$category}】</caption>
        <tr>
            <th scope="col">id</th>
            <th scope="col">answer</th>
            <th scope="col">question</th>
            <th scope="col">select1</th>
            <th scope="col">select2</th>
            <th scope="col">select3</th>
        </tr>
EOF;
    foreach($question as $key => $val){
print<<<EOF
        <tr>
            <td>{$key}</td>
            <td>{$val["answer"]}</td>
            <td>{$val["question"]}</td>
            <td>{$val["select1"]}</td>
            <td>{$val["select2"]}</td>
            <td>{$val["select3"]}</td>
        </tr>
EOF;
    }
print<<<EOF
    </table>
EOF;
?>