■enq01.php --- assign("message", array()); $tpl->assign("lovely_string", Enquery::$LOVELY_STRING); $tpl->assign("pet_string", Enquery::$PET_STRING); $tpl->assign("age_string", Enquery::$AGE_STRING); $tpl->assign("enq", new Enquery()); // テンプレートに基づいて表示 $tpl->display("enq01.tpl"); ?> --- ■template/enq01.tpl --- {* enq01.tpl *} わたしのホーム・ページ

わたしのホーム・ページ

ホイップ

女の子で、誕生日は12月14日です。
ホイップ
感想を送ってくださいね。
{if count($message) > 0} {/if}
お名前: 
email: 
かわいいですか? {foreach from=$lovely_string key=index item=value} {$value} {/foreach}
なにか、飼っていらっしゃいますか? {foreach from=$pet_string key=index item=value} {$value} {/foreach}
年代は?
コメントをご記入ください。


--- ■enq02.php --- setName($name)) { $message[] = "名前の指定が誤っています。"; } if (!$enq->setEmail($email)) { $message[] = "emailの指定が誤っています。"; } if (!$enq->setLovely($lovely)) { $message[] = "かわいさの指定が誤っています。"; } if (!$enq->setPet($pet)) { $message[] = "飼っているペットの指定が誤っています。"; } if (!$enq->setAge($age)) { $message[] = "年齢の指定が誤っています。"; } if (!$enq->setComment($comment)) { $message[] = "コメントの指定が誤っています。"; } // Smartyのインスタンス$tplの生成する $tpl = new Smarty(); // エラーがあれば、入力画面を表示する if (count($message) > 0) { // データを割り当てる $tpl->assign("message", $message); $tpl->assign("lovely_string", Enquery::$LOVELY_STRING); $tpl->assign("pet_string", Enquery::$PET_STRING); $tpl->assign("age_string", Enquery::$AGE_STRING); $tpl->assign("enq", $enq); //テンプレートに基づいて表示 $tpl->display("enq01.tpl"); return; } // セッションにアンケート結果を登録する $_SESSION['enq'] = $enq; // $tplにアンケート結果をアサイン(登録)する $tpl->assign("enq", $enq); // テンプレートに基づいて表示する $tpl->display("enq02.tpl"); ?> --- ■template/enq02.tpl --- {* enq02.tpl *}

次の内容でデータを登録します。間違いがないかご確認ください。

お名前:{$enq->name|escape}
email:{$enq->email|escape}
かわいい:{$enq->getLovely()|escape}
飼っているペット:{foreach from=$enq->getPet() item=v} {$v|escape} {/foreach}
年代:{$enq->getAge()|escape}
コメント:{$enq->comment|escape}

--- ■enq03.php --- connect(); $dao->insert($enq); $enqs = $dao->select(); $dao->disconnect(); // Smartyのインスタンス$tplの生成する $tpl = new Smarty(); // $tplにアンケート結果をアサイン(登録)する $tpl->assign("enq", $enq); $tpl->assign("enqs", $enqs); // テンプレートに基づいて表示する $tpl->display("enq03.tpl"); ?> --- ■template/enq01.tpl --- {* enq03.tpl *}

次の内容でデータを登録しました。

お名前:{$enq->name|escape}
email:{$enq->email|escape}
かわいい:{$enq->getLovely()|escape}
飼っているペット:{foreach from=$enq->getPet() item=v} {$v|escape} {/foreach}
年代:{$enq->getAge()|escape}
コメント:{$enq->comment|escape}

いままでの結果

{foreach from=$enqs item=v} {/foreach}
お名前 email かわいい 飼っているペット 年代 コメント
{$v->name|escape} {$v->email|escape} {$v->getLovely()|escape} {foreach from=$v->getPet() item=e} {$e|escape} {/foreach} {$v->getAge()|escape} {$v->comment|escape}
--- ■Enquery.class.php --- "すごくかわいい", "とてもかわいい", "かわいい"); /** * 「ペット」の選択肢の文字列 */ public static $PET_STRING = array(1=>"飼っていない", "犬", "猫", "フェレット", "その他"); /** * 「年齢」の選択肢の文字列 */ public static $AGE_STRING = array(1=>"未成年", "20代", "30代", "40代", "50代以降"); /** * 名前 */ public $name; /** * email */ public $email; /** * かわいさ */ public $lovely; /** * 飼っているペットの配列 */ public $pet; /** * 年代 */ public $age; /** * コメント */ public $comment; /** * コンストラクター */ public function __construct() { $this->name = ""; $this->email = ""; $this->lovery = 1; $this->pet = array(1); $this->age = 2; $this->comment = ""; } /** * 名前を取得する。 * @return 名前 */ public function getName() { return $this->name; } /** * 名前を設定する。 * @param $name 名前 * @return 設定できたときtrue、できなかったときfalse */ public function setName($name) { if (empty($name)) { return false; } $this->name = $name; return true; } /** * emailを取得する。 * @return email */ public function getEmail() { return $this->email; } /** * emailを設定する。 * @param $email email * @return 設定できたときtrue、できなかったときfalse */ public function setEmail($email) { if (empty($email)) { return false; } if (!preg_match(Enquery::$MAIL_PATTERN, $email)) { return false; } $this->email = $email; return true; } /** * かわいさを取得する。 * @return かわいさ */ public function getLovely() { return Enquery::$LOVELY_STRING[$this->lovely]; } /** * かわいさを設定する。 * @param $lovely かわいさ * @return 設定できたときtrue、できなかったときfalse */ public function setLovely($lovely) { if (empty($lovely)) { return false; } if ($lovely < 1 || $lovely > count(Enquery::$LOVELY_STRING)) { return false; } $this->lovely = $lovely; return true; } /** * ペットを取得する。 * @return ペット */ public function getPet() { $pet = array(); foreach ($this->pet as $i) { $pet[] = Enquery::$PET_STRING[$i]; } return $pet; } /** * 飼っているペットを設定する。 * @param $pet ペット配列 * @return 設定できたときtrue、できなかったときfalse */ public function setPet($pet) { if (empty($pet)) { return false; } foreach ($pet as $i => $v) { if ($v < 1 || $v > count(Enquery::$PET_STRING)) { unset($pet[$i]); } } $this->pet = $pet; return true; } /** * 年代を取得する。 * @return 年代 */ public function getAge() { return Enquery::$AGE_STRING[$this->age]; } /** * 年代を設定する。 * @param $age 年代 * @return 設定できたときtrue、できなかったときfalse */ public function setAge($age) { if (empty($age)) { return false; } if ($age < 1 || $age > count(Enquery::$AGE_STRING)) { return false; } $this->age = $age; return true; } /** * コメントを取得する。 * @return コメント */ public function getComment() { return $this->comment; } /** * コメントを設定する。 * @param $name コメント * @return 設定できたときtrue、できなかったときfalse */ public function setComment($comment) { $this->comment = $comment; return true; } /** * プロパティの添字配列を取得する。 * なお、飼っているペットについては、その種類をカンマ区切りにした文字列とする。 * @return プロパティの添字配列 */ public function getOrderedArray() { $data[] = $this->name; $data[] = $this->email; $data[] = $this->lovely; $data[] = implode(",", $this->pet); $data[] = $this->age; $data[] = $this->comment; return $data; } } ?> --- ■EnqueryDAO.class.php --- getMessage() . "
"; echo $conn->getDebugInfo() ."
"; return false; } $this->conn = $conn; return $this->conn; } /** * データベースとの接続を切断する。 */ public function disconnect() { $this->conn->disconnect(); } /** * アンケート・テーブルにデータを挿入する。 * なお、飼っているペットについては、その種類をカンマ区切りにした文字列とする。 * @param $enq アンケート結果 * @return 挿入できたときはtrue、できなかったときfalse */ public function insert($enq) { // SQL文を実行 $sql = "insert into enquery values(?, ?, ?, ?, ?, ?, default);"; $ret = $this->conn->query($sql, $enq->getOrderedArray()); if( DB::isError($ret) ){ echo $ret->getMessage() . "
"; echo $ret->getDebugInfo() . "
"; return false; } return true; } /** * アンケート・テーブルのすべてのデータを取得する。 * @return 取得できたときはアンケート結果の添字配列、できなかったときfalse */ public function select() { // SQL文を実行 $sql = "select * from enquery"; $ret = $this->conn->query($sql); if( DB::isError($ret) ){ echo $ret->getMessage() . "
"; echo $ret->getDebugInfo() . "
"; return false; } // 配列化 $data = array(); while ($row = $ret->fetchRow(DB_FETCHMODE_OBJECT)) { $enq = new Enquery(); $enq->setName($row->name); $enq->setEmail($row->email); $enq->setLovely($row->lovely); $enq->setPet(explode(",", $row->pet)); // カンマ区切りの文字列を配列化する $enq->setAge($row->age); $enq->setComment($row->comment); $data[] = $enq; } $ret->free(); return $data; } } ?> ---