Try   HackMD

PHP 陣列 session cookie POST&GET foreach isset

tags: PHP

陣列

練習:
試設color陣列,裡面存放yellow、orange、blie、red及green五個字串,請設計一網頁在每次開啟時被景色都會隨機產生

提示:rand()隨機產生亂數
rand(min,max)|ex:rand(0,10)

<head>
<style>
 .body1 { background-color:yellow;}
 .body2 { background-color:orange;}
 .body3 { background-color:blue;}
 .body4 { background-color:red;}
 .body0 { background-color:green;}
</style>
</head>

<body>
<?php
//寫法一
$bg = rand(0,4);
$color["1"] = "<body class=\"body1\">yellow<div>";
$color["2"] = "<body class=\"body2\">orange<div>";
$color["3"] = "<body class=\"body3\">blue<div>";
$color["4"] = "<body class=\"body4\">red<div>";
$color["0"] = "<body class=\"body0\">green<div>";
	echo $color[$bg];
?>

老師的寫法

<?PHP //老師的寫法,把PHP放在整個網頁最前面 $color = array ("yellow","red","blue","orange","green"); $a = rand(0,4); ?> <!DOCTYPE html> <html> <head> <title>陣列</title> </head> <body bgcolor="<?php echo $color[$a]; ?>"> </body> </html>

練習:
設計一記錄使用者造訪本網站次數的程式。(使用session)
Output:歡迎您第N次造訪本站

isset()判斷變數是否有被設置
isset ( mixed $var [, mixed $ ] ) : bool
可參考官網說明 https://www.php.net/isset

isset

<?php if (($_POST['sex']=='男')){ echo "<img src=\"man.png\" width=\"100\" height=\"100\" alt=\"man\" />"; }else{ echo "<img src=\"woman.png\" width=\"100\" height=\"100\" alt=\"woman\" />"; } ?>
如果(沒有設置session變數count) 設置session變數count; 再不然 把session變數count+1; <?php session_start(); if(!isset($_SESSION['count'])){ //驚嘆號是none的意思,把句子的意思反過來 $_SESSION['count']=1; }else{ $_SESSION['count']++; } ?> <?php echo "歡迎您第".$_SESSION['count']."次造訪"; ?>

POST&GET

<td width="49%" height="100" align="center" valign="middle"> 生日:<br> <?php echo $_POST['year']."年".$_POST['month']."月".$_POST['day']."日"; ?> </td> <td height="100" align="center" valign="middle"> 興趣:<br> <?php if(!isset($_POST['interest'])){ echo "沒有"; }else{ foreach($_POST['interest'] as $interest){ echo $interest."<br>" ; } } ?> </td>

練習:
資料表單傳送
1.表單傳送頁面

<body> <form id="form1" name="form1" method="post" action="php20191230-1.php"> <p>姓名: <input type="text" name="name" id="name" /> </p> <p>性別: <label> <input type="radio" name="sex" value="男生" id="RadioGroup1_0" /> 男生 </label> <label> <input type="radio" name="sex" value="女生" id="RadioGroup1_1" /> 女生</label> </p> <p> <input type="submit" name="button" id="button" value="送出" /> <br /> </p> </form> </body>

2. php20191230-1.php 接收頁面

<?php echo $_POST['name'].",".$_POST['sex']; ?>

作業:綜合應用練習,製作一個表單

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php作業</title> <style> body{ font-family:'微軟正黑體'; } </style> </head> <body> <form id="form1" name="form1" method="post" action="php_14-2.php"> <table width="300" border="2" align="center" cellpadding="0" cellspacing="0"> <tr> <td valign="middle"><p>姓名: <label for="name4"></label> <input type="text" placeholder="請輸入姓名" name="name" id="name4" /> </p> <p>性別: <label> <input name="sex" type="radio" id="sex_0" value="男" checked="checked" /></label> <label> <input type="radio" name="sex" value="女" id="sex_1" /></label> <br /> </p> <p>生日: <label for="year4"></label> <select name="year" id="year4"> <?php for($i=1910; $i<=2019; $i++){ echo "<option value=\"$i\">$i</option>"; } ?> </select><label for="month4"></label> <select name="month" id="month4"> <?php for($m=1; $m<=12; $m++){ echo "<option value=\"$m\">$m</option>"; } ?> </select><label for="day4"></label> <select name="day" id="day4"> <?php for($d=1; $d<=31; $d++){ echo "<option value=\"$d\">$d</option>"; } ?> </select></p> <p>興趣: <label> <input type="checkbox" name="interest[]" value="羽球" id="interest_0" /> 羽球</label> <label> <input type="checkbox" name="interest[]" value="聽音樂" id="interest_1" /> 聽音樂</label> </p> <p>    <label> <input type="checkbox" name="interest[]" value="閱讀" id="interest_2" /> 閱讀</label> <label> <input type="checkbox" name="interest[]" value="游泳" id="interest_3" /> 游泳</label> <br /> </p> <p align="center"> <input type="submit" name="button" id="button" value="送出" /> </p></td> </tr> </table> </form> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php作業-結果</title> </head> <body> <table width="300" border="2" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="49%" height="100" align="center" valign="middle"> <?php //echo $_POST['sex']; //$sex = '男'; if (($_POST['sex']=='男')){ echo "<img src=\"man.png\" width=\"100\" height=\"100\" alt=\"man\" />"; }else{ echo "<img src=\"woman.png\" width=\"100\" height=\"100\" alt=\"woman\" />"; } ?> </td> <td width="49%" height="100" align="center" valign="middle"> 姓名:<br> <?php echo $_POST['name']; ?> </td> </tr> <tr> <td width="49%" height="100" align="center" valign="middle"> 生日:<br> <?php echo $_POST['year']."年".$_POST['month']."月".$_POST['day']."日"; ?> </td> <td height="100" align="center" valign="middle"> 興趣:<br> <?php if(!isset($_POST['interest'])){ echo "沒有"; }else{ foreach($_POST['interest'] as $interest){ echo $interest."<br>" ; } } ?> </td> </tr> </table> </body> </html>