55回目授業内容 PHP〜脱線
[授業][Dreamweaver][PHP]
php
- array関数
- 「count」で数を自動で数えてもらう
- 「for」の場合
<?php $data = array('山田太郎', '鈴木花子', '田中一郎', '山本久美子', '斎藤次郎', '佐々木裕子', '佐藤秀雄' ); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>無題ドキュメント</title> </head> <body> 名簿には<?php print(count($data)); ?>人が登録されています。 <ol> <?php for ($i=0; $i<count($data); $i++){ print '<li>' . $data[$i] . '</li>'; } ?> </ol> </body> </html>
- 「while」の場合
名簿には<?php print(count($data)); ?>人が登録されています。 <ol> <?php $i = 0; while ($i<count($data)){ print '<li>' . $data[$i] . '</li>'; $i++; } ?> </ol>
- printのみで出す場合
名簿には<?php print(count($data)); ?>人が登録されています。 <ol> <?php print '<li>' . $data[0] . '</li>'; print '<li>' . $data[1] . '</li>'; print '<li>' . $data[2] . '</li>'; print '<li>' . $data[3] . '</li>'; print '<li>' . $data[4] . '</li>'; print '<li>' . $data[5] . '</li>'; print '<li>' . $data[6] . '</li>'; print '<li>' . $data[7] . '</li>'; ?> </ol>
- foreach
- キー値と繰り返し文
<?php $stock['みかん'] = 80; $stock['いちご'] = 60; $stock['りんご'] = 22; $stock['もも'] = 50; $stock['くり'] = 75; ?> <table border="1"> <tr><th>商品名</th><th>在庫状況</th></tr> <?php foreach($stock as $name => $value){ print '<tr><td>' . $name . '</td><td>' . $value . '</td></tr>' . "\n"; } ?> </table>
- 添字と繰り返し文
<?php $product[0] = '鉛筆'; $product[1] = '消しゴム'; $product[2] = '定規'; $product[3] = 'コンパス'; $product[4] = 'ボールペン'; ?> <table border="1"> <tr><th>番号</th><th>商品名</th></tr> <?php foreach($product as $id => $value){ print '<tr><td>' . $id . '</td><td>' . $value . '</td></tr>' . "\n"; } ?> </table>
本格お問合せフォームを作る
- 入力画面
<form action="php/check.php" method="post" id="inquiry"> <table border="1" summary="お問い合わせに関する入力項目名とその入力欄"> <tr> <th><label for="name">お名前</label></th> <td><input type="text" name="name" size="50" id="name" class="text1"></td> </tr> <tr> <th><label for="email">メールアドレス</label></th> <td><input type="text" name="email" size="50" id="email" class="text2"></td> </tr> <tr> <th><label for="message">ご意見</label></th> <td><textarea name="message" cols="40" rows="5" class="text3" id="message"></textarea></td> </tr> </table> <input type="submit" value="送信"> </form>
- 確認画面
<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $name = htmlspecialchars($name); $email = htmlspecialchars($email); $message = htmlspecialchars($message); ?> <body> <table border="1" summary="お問い合わせに関する入力項目の確認" id="inquiry"> <tr> <?php if ($name==''){ print '<th>お名前</th><td>' . 'お名前が入力されていません。</td></tr><br>'; } else { print '<th>お名前:</th><td>' . $name. ' 様。</td></tr><br>'; } if ($email==''){ print '<th>メールアドレス</th><td>メールアドレスが入力されていません。</td></tr><br>'; } else { print '<th>メールアドレス:</th><td>' . $email . '</td></tr><br>' . "\n"; } if ($message==''){ print '<th>お問い合わせ内容</th><td>お問合せ内容が入力されていません。</td></tr>'; } else { print '<th>お問合せ内容:</th><td>' . $message . '</td></tr>' . "\n"; } ?> </table> <?php if ($name=='' || $email=='' || $message==''){ print '<form>' . "\n"; print '<input type="button" onClick="history.back()" value="戻る">'; print '</form>' . "\n"; } else { print '<form action="thanks.php" method="post">' . "\n"; print '<input type="hidden" name="name" value="' . $name . '">'; print '<input type="hidden" name="email" value="' . $email . '">'; print '<input type="hidden" name="message" value="' . $message . '">'; print '<input type="button" onClick="history.back()" value="戻る">'."\n"; print '<input type="submit" value="送信">' . "\n"; print '</form>' . "\n"; } ?>
- サンキュウ画面
<?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $name = htmlspecialchars($name); $email = htmlspecialchars($email); $message = htmlspecialchars($message); print $name . '様<br>' . "\n"; print 'お問合せありがとうございます。<br>' . "\n"; print 'お問い合わせ内容「'.$message.'」を<br>' . "\n"; print $email . 'にメールでお送り致しましたのでご確認下さい。' . "\n"; $mail_sub = 'お問合せを受け付けました。'; $mail_body = $name.'様、ご協力ありがとうございました。'; $mail_body = html_entity_decode($mail_body,ENT_QUOTES,"UTF-8"); $mail_head = 'ttokiyo@me.com'; mb_language('japanese'); mb_internal_encoding('UTF-8'); mb_send_mail($email,$mail_sub,$mail_body,$mail_head); ?>