2012年03月28日
講義038日目
------------------------------1H------------------------------
★smarty
●smart復習
・$smarty.sessionは「smartyの予約変数」
・phpファイルからhtml部分をカット
・smartyのfetch関数を使うと他ページにある複数のロジックを扱える
・smartyで使えないphpの関数もあるので注意
------------------------------2H------------------------------
★smarty
●smarty復習
・配列のインデックスを引っ張るときに
例{$row.nickname|htmlspecialchars}
{$row.1|htmlspecialchars}
{$row.[1]|htmlspecialchars}
でも引っ張ってこれるが、連想配列の.○○○を使った方が分かりやすい
・includeを使ってheader部分を別の.tplファイルに持たせる
例{include file"header.tpl"}
を元のheaderがあった場所に記述
------------------------------3H------------------------------
★smarty
●smartyの関数
・$smarty->fetch関数
指定したテンプレートの出力結果を(文字列として)取得する関数
(テンプレートを表示するdisplayとは違う)
・article.tplを作成
index.tpl内の
{foreach from=$rows item=row key=keyname}~{/foreach}
を移動する
・.inc
includeするファイルの意味
・データベースのcommentテーブルにcategory_idカラムを追加する
index.phpに追記
5行目あたりに
include("./config/config.inc");
$cid = $_GET["cid"];
------------------------------4H------------------------------
★smarty
●カテゴリの実装
index.phpからarticle.tplへ
{*かつてindex.phpで出力していた処理をコメントアウト
<?php
while($row = mysql_fetch_array($result)) {
print "<div class=\"commentBox\">";
//urlカラムにデータが入っていたら名前にリンクを貼る
if($row["url"]!=""){
print "<a href=\"" . $row["url"] . "\" target=\"_blank\">";
print nl2br(htmlspecialchars($row["nickname"]));
print "</a>";
print "さんの投稿<br>";
}else{
print nl2br(htmlspecialchars($row["nickname"]))."さんの投稿<br>";
}
//画像を出力する
if($row["photo"] != ""){
print "<img src=\"./img/".$row["photo"]."\" width=\"100\"><br>";
}
print nl2br(htmlspecialchars($row["content"]))."<br>";
//年月日を装飾する
//strtotime関数:文字列の日時をtime型に変える
print date("Y年m月d日",strtotime($row["created_at"]))."<br>";
print "<a href=\"reply.php?comment_id=".$row["id"]."\">返信する</a>";
print "</div>";
//返信用ループここから
$sql = "SELECT * FROM reply
WHERE comment_id = '".$row["id"].
"' ORDER BY created_at ASC";
$result_reply = mysql_query($sql);
while($row_reply = mysql_fetch_array($result_reply)) {
print "<div class=\"replyBox\">";
print nl2br(htmlspecialchars($row_reply["nickname"]))."さんの返信<br>";
print nl2br(htmlspecialchars($row_reply["content"]))."<br>";
print $row_reply["created_at"]."<br>";
print "</div>";
}
//返信用ループここまで
}
?>
*}
------------------------------5H------------------------------
★smarty
●カテゴリの実装
・phpのソースと連動しなければならないときは
$smarty->fetch関数を使う
動的でなければ
{includeでかまわない
・config.incをリネーム
直打ちしたときに内容が出ないようにconfig.inc.phpに変更
F2でリネーム
●関数の書き方
・引数を持つ関数もある
・予約変数
{$smarty.変数名.添字}
------------------------------6H------------------------------
★BBSの管理画面作成
●管理画面の実装
メンバーテーブルのpasswordカラムの後に、admin_ynカラムを追加する
データ型char(1)
ALTER TABLE member ADD admin_yn char(1) AFTER password;
sql文にadmin_ynを追加
if($row[2] == "y"){
$_SESSION["admin"] ="y";
}else{
$_SESSION["admin"] ="n";
}
-----------------------------memo------------------------------
if文が複雑になるとマジでよくわからなくなる件
多分苦手意識が発生してるっぽいので改善する方向で
0 件のコメント:
コメントを投稿