星期二, 7月 27, 2010

20100727 PHP-MySQL上課小記

20100727 上課小記

建立一個 Netbean 的PHP 專案 class3
建立新專案
File → New Project
PHP: PHP Application → Next
點選 Browse 按鈕 建立一個 class3 資料夾( source 按鈕調整於 C:\Appserv\www 目錄下 [網站根目錄] )
Finish

將\\PC1 上面預先準備好的資料 複製到 C:\Appserv\www\class3 下面

執行 year.html 若輸入的年齡 大於 18, 結果為?
year.html 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
<form action="year.php" method="post" name="form1">
請輸入年齡:<input type="text" name="years">
<input type="submit" value="ok" /><br></form>
</body>
</html>


year.php 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
<?
if (!isset($_POST["years"]))
{
  die("沒有資料") ;
}
$year1=$_POST["years"];
if ($year1>=12)
{  echo "可看輔導級,不可看限制級"; }
elseif($year1>=18)
{echo "可看限制級";}
elseif ($year1>=6)
{  echo "可看保護級"; }
else
{  echo "可看普遍級"; }
?>
</body>
</html>


if (條件1)
    else if (條件2)
else  以上條件均不成立時

Notes:
  • 若有多個條件要依序來作分析, 建議條件由大而小, 或由小而大依序執行
  • if, else 是前一個條件成立,  就不在執行 elseif 及 else


因為 18 歲以上只會顯示一種, 所以做一下修改
將year.html 另存為 year2.html,  action 改為 year2.php
將yearphp 改為 year2.php

year2.html 內容如下
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
   <title></title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
   <form action="year2.php" method="post" name="form1">
請輸入年齡:<input type="text" name="years">
<input type="submit" value="ok" /><br></form>
</body>
</html>

year2.php 內容如下 (將所有的 elseif 改為 if 並將 else 註解)
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title></title>
   </head>
   <body>
<?
if (!isset($_POST["years"]))
{
  die("沒有資料") ;
}
$year1=$_POST["years"];
if ($year1>=12)
{  echo "可看輔導級,不可看限制級"; }
if($year1>=18)
   {echo "可看限制級";}
if ($year1>=6)
{  echo "可看保護級"; }
//else
{  echo "可看普遍級"; }
?>

   </body>
</html>


isset( )
  • 判斷( ) 內的變數是否存在, 存在傳回true


if( ) 內若使用 !  代表相反,  例如 if( !isset())  代表當檔案或變數不存在時
die("訊息”顯示訊息後停止網頁運作
  • 不執行之後的html 及 php 語法


只要表單有送出資料, 接收資料的 php 檔就會建立 $_POST 或 $_GET 變數

Lab: 偵測內容是否有被輸入

使用 from1.html 來輸入資料, 內容如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>text與passwd</title>
</head>

<body>
<form action="form1.php" method="post">
請輸入姓名:<input type="text"  size="10" name="username" maxlength="6"><br>
請輸入密碼:<input type="password"  size="10" name="passwd" maxlength="6"><br>
<input type="submit"><input type="reset">
</form>
</body>
</html>


接收的 form1.php 內容如下
<html><head><title>接收text與passwd</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <? if (!isset($_POST["username"])) {   die("沒有姓名") ; } if (!isset($_POST["passwd"])) {   die("沒有密碼") ; }

//用來 Debug 用
echo $_POST['username'];
echo $_POST['passwd'];

if($_POST['username'] == '')
      die("姓名是空的");   if($_POST['passwd'] == '')      die("密碼是空的");

  ?><br> 接收的姓名為<? echo $_POST['username'];?><br> 接收的密碼為<? echo $_POST['passwd'];?><br> </body> </html>





for 迴圈
  • 最嚴謹及複雜的迴圈形式
  • for ( 1 ; 2 ; 3 )
    • 1: 變數的初始值
    • 2: 可執行範圍
    • 3: 每跑完一次迴圈固定變化
  • for 迴圈有初始值,有離開的條件,有固定的變化, 可知道固定的範圍


Q: 如何在 form , html 上作資料檢查?
A:
  • 可自寫 java script 或 jQuery
  • 但瀏覽器可關閉 script 功能, 所以 php 能須作檢查


jQuery:
  • java script 套件.
  • 可用較少的語法, 使用套件內提供的功能


while(  )
{
//先判斷再執行
//可能一次都不執行
}

範例 while.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>迴圈介紹:while </title></head><body>
 <?
$i = 1;
while ($i<=10){   //while是先做判斷再執行迴圈
echo "i=".$i."<br>";
   $i++;
}
  ?>
</body>
</html>



do
{
//先執行再判斷
//至少執行一次
}while (  ); //請注意 ;

範例 
dowhile1.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dowhile:當條件成立時 </title></head>
<body><?
$i = 1;
do
{   
echo "i=".$i."<br>";
$i++;
}while ($i<10);
   echo "離開迴圈時 i=".$i."<br>";
?> </body></html>

dowhile2.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dowhile:當條件不成立時 </title></head>
<body><?
$i = 11;
do
{   
echo "i=".$i."<br>";
$i++;
}while ($i<10);
   echo "離開迴圈時 i=".$i."<br>";
?> </body></html>

continue:
  • 中止此次迴圈, 繼續跑下一個值


continue 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> conitue </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{
       if ($i==5)
{
    echo "迴圈停止<br>";
    continue;
}         
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>


break:
  • 跳出迴圈


break 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>break </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{
  if ($i==5)
{
    echo "迴圈停止<br>";
    break;
}
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>


exit:
  • 中止後面網頁運作


exit 範例
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> exit </title></head>
<body><?
for ($i=1;$i<=10;$i++)
{  
if ($i==5)
{
    echo "迴圈停止<br>";
    exit;
}
echo "i->".$i."<br>";
}
echo "結束執行";
?></body></html>

Array
  • [ ]  代表陣列
  • php 的 陣列預設由 index=0 開始, index 代表索引值
  • 若沒給索引值, 預設由 0 或是前一個索引值開始編號
  • 以 array 方式規劃
    • $陣列名稱=array(索引值);
  • php 陣列索引值允許以文字代替


範例 array1.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>第一個陣列</title></head>
<body><?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;      
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array2.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>第一個陣列,不給予索引值編號</title></head>
<body><?
$chinese[] = 80;
$chinese[] = 60;
$chinese[] = 90;      
$chinese[] = 50;
$chinese[] = 70;
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array3.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>以array方式建立陣列,索引值編號任意給</title></head>
<body><?
$chinese=array(
1=>80,
3=>60,      
6=>90,
8=>50,
9=>70
);
for ($a=0; $a<5; $a++)
 echo "$chinese[$a] <br>" ;
?></body></html>

範例 array4.php
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>以array方式建立文字型態索引值陣列</title></head>
<body><?
   $a=array(
    "Jan" => "一月",
    "Feb" => "二月",
    "Mar" => "三月"
   );
  echo $a["Mar"]."<br>";       
?></body></html>


foreach 有兩種用法
  • foreach($陣列 as $值)  可將每一筆資料逐一列出(空值省略)
  • foreach($陣列 as $值  => $值)  可
  • 文字為索引值時必須使用foreach



for 與 foreach 的比較
for
  • 有範圍
  • 列出所有資料(包含空值)

foreach
  • 執行至陣列結束
  • 列出的值不包含空值



Homework:
輸入3位同學的國英數三科成績, 並計算各科平均
(若時間允許, 再算每個人平均)
以陣列方式作業
-- class end --

沒有留言: