博客來網路書店查詢

書名

博客來網路書店查詢

星期一, 11月 22, 2010

PHP&MySQL--12 2010年11月課程講義預覽

PHP&MySQL--12
一、 沒有對應的方法與權限會如何呈現呢
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉沒有對應的方法與權限會如何呈現呢〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}
}
$box1 = new cbox;
$box1-〉titlename="php";
echo $box1-〉titlename;
$box1-〉test2(123);〈/body〉〈/html〉



2. 處理方式【另存新檔後增加或修改內容】

function __get($var) {
echo "沒有對應的屬性可處理,若無此函數該屬性會自動產生:".$var."〈br〉"; }
function __set($a,$b) {
echo "沒有對應的屬性可接收,若無此函數該屬性會自動產生:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }
function __call($a,$b) {
echo "沒有對應的函數可處理,若無此函數會產生錯誤訊息:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }




3. 權限不合的處理方式?【另存新檔後增加或修改內容】

$box1 = new cbox;
$box1-〉lenght1="php";
echo $box1-〉lenght1;
$box1-〉test2(123);




二、 物件複製是否有錯?


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉物件複製是否有錯〈/title〉〈/head〉〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
}
function setlength1($lenght1)
{ $this-〉lenght1=$lenght1; }
function getlength1( )
{ return $this-〉lenght1; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; }
}
$box1 = new cbox(20,20,20);
$box2 = $box1;
$box2-〉setlength1(30);
echo "第1個箱子length1=".$box1-〉getlength1( )."〈br〉";
echo "第2個箱子length1=".$box2-〉getlength1( )."〈br〉";〈/body〉〈/html〉



三、 繼承
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉繼承〈/title〉〈/head〉〈body〉
class cat extends animal {
function __construct( ) {
echo "產生一隻貓科動物"."〈br〉";
} }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉";
} }
class animal {
function __construct( ) {
echo "產生一隻動物"."〈br〉";
} }
$tiger1 = new tiger;
echo "〈hr〉";
$cat1 = new cat;
echo "〈hr〉";
$animal2 = new animal;
echo "〈hr〉";〈/body〉〈/html〉



2. 子類別執行父類別的建構子


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉";
} }



3. 父子類別都有相同的方法

class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉"; }
function run( )
{ echo "貓科動物跑得快"."〈br〉"; } }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉"; } }




4. 子類別執行父類別的方法


class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉";
return parent::run( ); } }



四、 影像處理:劃框線


$img = ImageCreateFromPNG('img.png');
$color_black = ImageColorAllocate($img, 0, 0, 0);
drawBorder($img, $color_black, 30);
header('Content-type: image/png');
Imagepng($img);
function drawBorder(&$img, &$color, $thickness = 1)
{
$x1 = 0;
$y1 = 0;
$x2 = ImageSX($img) - 1;
$y2 = ImageSY($img) - 1;
for($i = 0; $i 〈 $thickness; $i++)
{
ImageRectangle($img, $x1++, $y1++, $x2--, $y2--, $color_black);
}
}




五、 繪製圖表-1

header("Content-type: image/png");
$arrval = array(12,123,21,32,77,85,166,176,163,121);
$height = 260;
$width = 330;
$im = imagecreate($width,$height);
$white = imagecolorallocate($im,255,255,255);
$gray = imagecolorallocate($im,200,200,200);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
$x = 21;
$y = 11;
$num = 0;
while($x〈=$width && $y〈=$height){
$prcnt = ((($height-50)-($y-1))/($height-60))*100;
imageline($im, 21, $y, $width-10, $y, $gray);
imageline($im, $x, 11, $x, $height-50, $gray);
imagestring($im,2,1,$y-10,$prcnt.'%',$red);
imagestring($im,2,$x-3,$height-40,$num,$red);
$x += 30;
$y += 20;
$num++;
}
$tx = 20;
$ty = 210;
foreach($arrval as $values){
$cx = $tx + 30;
$cy = 200-$values;
imageline($im,$tx,$ty,$cx,$cy,$red);
imagestring($im,5,$cx-3,$cy-13,'.',$red);
$ty = $cy;
$tx = $cx;
}
imageline($im, 20, 11, 20, $height-50, $black);
imageline($im, 20, $height-49, $width-10, $height-49, $black);
imagestring($im,3,10,$height-20,'Line Graph by: Roseindia Technologies',$red);
imagepng($im);




六、 繪製圖表-2


$image = imagecreatetruecolor(300, 300);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
for ($i = 160; $i 〉 150; $i--) {
imagefilledarc($image, 150, $i, 200, 100, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 150, $i, 200, 100, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 150, $i, 200, 100, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, 150, 150, 200, 100, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 150, 150, 200, 100, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 150, 150, 200, 100, 75, 360 , $red, IMG_ARC_PIE);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);




七、 繪製圖表與資料庫


include "db.php";
$qt=mysql_query("select * from gd_graph");
header ("Content-type: image/jpg");
$x_gap=40;
$x_max=$x_gap*13;
$y_max=250;
$im = @ImageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image
stream");
$background_color = ImageColorAllocate ($im, 234, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$graph_color = ImageColorAllocate ($im,25,25,25);
$x1=0;
$y1=0;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
$x2=$x1+$x_gap;
$y2=$y_max-$nt[sales];
//ImageString($im,2,$x2,$y2,$nt[month].$nt[sales],$graph_color);
ImageString($im,2,$x2,$y2,$nt[month],$graph_color);
if($first_one=="no"){
imageline ($im,$x1, $y1,$x2,$y2,$text_color);
}
$x1=$x2;
$y1=$y2;
$first_one="no";
}
ImageJPEG ($im);




八、 其他

2010年11月:東區認證中心VB第三階段課程

算一下時間..上次帶VB第三階段課程是2006年12月..
四年後再次開課..希望能提供給各位滿滿的資訊。
這次課程將跟大家說明VB視窗程式設計,原則上以圖檔顯示與運動、
listbox運用、picturebox繪圖、功能表與表單切換做介紹。
課程會運用到第二階段所提的物件導向觀念,所以各位日前所學
的項目會有實作的機會。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。

星期五, 11月 19, 2010

2010年11月:東區認證中心C#初階課程

這次課程將跟大家說明C#程式變數宣告、條件判斷式、迴圈與陣列宣告等觀念。
程式設計採視窗程式設計(Windows Form),所以與坊間書籍於終端機環境
設計有很大的不同,但觀念相同。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。
課程中除參考過往經驗、課本安排課程進度外,另會參考
碁峰的「Visual C# 2005 程式設計經典」這本書內容進行課程內容安排。

星期三, 11月 17, 2010

PHP&MySQL--11 2010年11月課程講義預覽

PHP&MySQL--11
一、 簡單資料操作
1. 新增:請參考上次講義
2. 列表:請參考上次講義
3. 編輯


〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_GET['id'])) die("請先執行返回資料列表網頁");
if($_GET['id']=="") die("請回資料列表網頁選擇資料");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉
〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉





〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_POST['frm_account'])) die("請先執行編輯資料網頁");
if($_POST['frm_account']=="") die("請回編輯資料網頁輸入資料");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉



4. 刪除


〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_GET['id'])) die("請先執行返回資料列表網頁");
if($_GET['id']=="") die("請回資料列表網頁選擇資料");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



二、 CSV檔案匯出與匯入
1. 匯入【接收端網頁】


〈? include("server.php"); ?〉〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉匯入 CSV 檔〈/title〉〈/head〉〈body〉
〈?
$uploaddir='./';
if ($_FILES["CSVfile"]["name"] ) {
$temp_SQL = "";
$file2=mb_convert_encoding($_FILES["CSVfile"]["name"],"big5","utf8");
if(!move_uploaded_file($_FILES['CSVfile']['tmp_name'],$uploaddir.$file2)){
echo "上傳失敗〈br〉";
}
$fp = fopen($_FILES["CSVfile"]["name"] , "r");
while ( $ROW = fgetcsv($fp,$_FILES["CSVfile"]["size"] ) ) {
if ( strlen($ROW[0]) ) {
if ( strlen($temp_SQL) ) $temp_SQL .= ", ";
$temp_SQL .= "('" . $ROW[0] . "', '" . $ROW[1] . "', '" . $ROW[2] . "', '" .
$ROW[3] . "') ";
}
}
fclose($fp);
$sql = "INSERT INTO student1(sid, username,address,birthday) VALUES " . $temp_SQL;
mysql_query($sql) or die(mysql_error( ));
}
else {
echo "Error!";
}?〉〈/body〉〈/html〉



2. 匯出


〈?
include("server.php");
$file = 'export';
$result = mysql_query("show columns from student1");
$i = 0;
if (mysql_num_rows($result) 〉 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("select * from student1");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j〈$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
};
ini_set("date.timezone","Asia/Taipei");
$filename = $file."_".date("Y-m-d_H-i",time( ));
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?〉



三、 上傳與解壓縮


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉zip檔案上傳〈/title〉〈/head〉〈body〉〈?
include ('pclzip.lib.php');
$tmp_folder = basename($_FILES['myfile']['name']);
if(is_dir($tmp_folder))
{
echo "資料夾已存在"."〈br〉";
}
else
{
//echo "資料夾不存在"."〈br〉";
if(mkdir($tmp_folder, 0700))
echo "成功建立資料夾"."〈br〉";
else
echo "建立資料夾失敗"."〈br〉";
}
if($_FILES['myfile']['size']〉0){
if(is_uploaded_file($_FILES['myfile']['tmp_name'])&&$_FILES['myfile']['type']=='application/zip'){
if(is_writeable($tmp_folder)){
if(move_uploaded_file($_FILES['myfile']['tmp_name'],$tmp_folder.'/'.$_FILES['myfile']['name'])){
echo '檔案上傳成功!〈p〉';
$hzip = new PclZip($tmp_folder.'/'.$_FILES['myfile']['name']);
if($hzip-〉extract($tmp_folder.'/'))
echo '檔案解壓縮成功!';
else
echo '檔案解壓縮失敗!';
} else
echo '檔案上傳失敗!〈p〉';
} else
echo '檔案無法寫入!';
} else
echo '沒有上傳檔案!';
}
?〉〈/body〉〈/html〉




四、 初論
五、 屬性
1. 基本設計【請參考講義補充遺漏的語法】


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉箱子:類別與屬性〈/title〉〈/head〉
〈body〉
class cbox {
public $lenght1;
public $breadth1;
public $height1;
}
$box1 = new cbox( );
$box1-〉lenght1 = 20;
$box1-〉breadth1 = 20;
$box1-〉height1 = 20;
$box2 = new cbox( );
$box2-〉lenght1 = 30;
$box2-〉breadth1 = 30;
$box2-〉height1 = 30;
echo "第1個箱子體積=".$box1-〉breadth1*$box1-〉height1*$box1-〉lenght1."〈br〉";
echo "第2個箱子體積=".$box2-〉breadth1*$box2-〉height1*$box2-〉lenght1."〈br〉";
〈/body〉〈/html〉



2. 如果加上函數呢【另存新檔後增加或修改內容】


function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}



3. 不過屬性仍可呼叫,因此請變更屬性權限並改變類別內方法接收資料方式【另存新檔後增加或修改內容】


function getdata($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;


}
4. 加入建構子【另存新檔後增加或修改內容】


function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;


}
5. 加入解構子【另存新檔後增加或修改內容】


function __destruct( ) {
print "Destroying "."〈br〉"; }



六、 方法內包含類別


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉方法內內包含類別〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; } }
class worker {
public function work($box3) {
$str = $box3-〉getdata( );
echo $str; }
}
$box4 = new cbox(20,30,20);
$worker1 = new worker( );
$worker1-〉work( $box4 ); 〈/body〉〈/html〉



七、 沒有對應會如何呈現呢
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉沒有對應會如何呈現呢〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}
}
$box1 = new cbox;
$box1-〉titlename="php";
echo $box1-〉titlename;
$box1-〉test2(123); 〈/body〉〈/html〉



2. 處理方式【另存新檔後增加或修改內容】


function __get($var) {
echo "沒有對應的屬性可處理,若無此函數該屬性會自動產生:".$var."〈br〉"; }
function __set($a,$b) {
echo "沒有對應的屬性可接收,若無此函數該屬性會自動產生:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }
function __call($a,$b) {
echo "沒有對應的函數可處理,若無此函數會產生錯誤訊息:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }

3. 權限不合的處理方式?【另存新檔後增加或修改內容】



$box1 = new cbox;
$box1-〉lenght1="php";
echo $box1-〉lenght1;
$box1-〉test2(123);





八、 複製是否有錯?


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉複製是否有錯〈/title〉〈/head〉〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
}
function setlength1($lenght1)
{ $this-〉lenght1=$lenght1; }
function getlength1( )
{ return $this-〉lenght1; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; }
}
$box1 = new cbox(20,20,20);
$box2 = $box1;
$box2-〉setlength1(30);
echo "第1個箱子length1=".$box1-〉getlength1( )."〈br〉";
echo "第2個箱子length1=".$box2-〉getlength1( )."〈br〉"; 〈/body〉〈/html〉



九、 繼承
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉繼承〈/title〉〈/head〉〈body〉
class cat extends animal {
function __construct( ) {
echo "產生一隻貓科動物"."〈br〉";
} }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉";
} }
class animal {
function __construct( ) {
echo "產生一隻動物"."〈br〉";
} }
$tiger1 = new tiger;
echo "〈hr〉";
$cat1 = new cat;
echo "〈hr〉";
$animal2 = new animal;
echo "〈hr〉"; 〈/body〉〈/html〉



2. 子類別執行父類別的建構子


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉";
} }



3. 父子類別都有相同的方法


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉"; }
function run( )
{ echo "貓科動物跑得快"."〈br〉"; } }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉"; } }



4. 子類別執行父類別的方法


class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉";
return parent::run( ); } }



十、 其他

星期五, 11月 12, 2010

PHP&MySQL--10 2010年11月課程講義預覽

PHP&MySQL--10
一、 上傳多個檔案
【提供表單網頁upload4.htm及接收資料網頁upload4.php,再請修改】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉處理上傳多個檔案〈/title〉〈/head〉 〈body〉〈?
$uploaddir='./upload/';
$i=count($_FILES["fileupload"]["name"]);
for ($j=0;$j〈$i;$j++){
$tmpfile=$_FILES["fileupload"]["tmp_name"][$j];
$file2=mb_convert_encoding($_FILES["fileupload"]["name"][$j],"big5","utf8");
if(move_uploaded_file($tmpfile,$uploaddir.$file2)){
echo "上傳成功〈br〉";
echo "檔案名稱:".$_FILES["fileupload"]["name"][$j]."〈br〉";
echo "檔案類型:".$_FILES["fileupload"]["type"][$j]."〈br〉";
echo "檔案大小:".$_FILES["fileupload"]["size"][$j]."〈br〉";
}else{
echo $_FILES["fileupload"]["tmp_name"][$j]."上傳失敗!〈br /〉";
switch ($_FILES["fileupload"]["error"][$j]){
case 1:
echo "失敗原因:大小超過php.ini內設定 upload_max_filesize"."〈br〉";
break;
case 2:
echo "失敗原因:大小超過表單設定 MAX_FILE_SIZE"."〈br〉";
break;
case 3:
echo "失敗原因:上傳不完整"."〈br〉";
break;
case 4:
echo "失敗原因:沒有檔案上傳"."〈br〉";
break;
case 6:
echo "失敗原因:暫存資料夾不存在"."〈br〉";
break;
case 7:
echo "失敗原因:上傳檔案無法寫入"."〈br〉";
break;
case 8:
echo "失敗原因:上傳停止"."〈br〉";
break;
} } } ?〉
〈/body〉〈/html〉




二、 繪製圖片—graph.php


〈? $height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, 'Sales', $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);?〉



三、 如何呼叫已經產生圖片的網頁? (請自行設計)


〈img src="graph.php" height=200 width=200 alt="顯示php的內容"〉



四、 輸入資料檢驗驗證碼
1. 表單部分


〈html〉 〈head〉〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉〈body〉〈form action="input.php" method="post"〉
〈input type="text" name="graph" maxlength="7" size="7"〉
〈input type="submit"〉〈/form〉
〈img src="graph.php" width="200" height="200" alt="show image"/〉
〈/body〉〈/html〉



2. 驗證碼部分(graph.php,請修改網頁)


〈? session_start( );
$_SESSION['test']=generatorPassword( );
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, $_SESSION['test'], $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);
function generatorPassword( )
{ $password_len = 7;
$password = '';
$word = '0123456789';
$len = strlen($word);
for ($i = 0; $i 〈 $password_len; $i++) {
$password .= $word[rand( ) % $len]; }
return $password; } ?〉



3. 接收資料

〈?session_start( ); ?〉〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉驗證資料〈/title〉〈/head〉〈body〉
〈? if ($_POST['graph']==$_SESSION['test'])
{ ?〉
〈script language="JavaScript"〉
alert("〈? echo "輸入資料正確,轉往google"; ?〉");
location.href="http://www.google.com";
〈/script〉〈? }
else
{ ?〉
〈script language="JavaScript"〉
alert("輸入資料不正確,請重新輸入");
history.back( );
〈/script〉 〈?
} ?〉 〈/body〉〈/html〉




五、 簡單資料操作
1. 新增


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉新增〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");

mysql_query("insert into student (account,username,sex,date1) values('$account','$username','$sex','$temp_date') ") or die("error");

header("location:index.php");
?〉〈/body〉〈/html〉



2. 列表

〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉表列資料〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$res =mysql_query("select * from student") or dir("sql error");
?〉〈a href="add.htm"〉新增〈/a〉〈br〉 〈?
while($row=mysql_fetch_array($res)){
echo "姓名:".$row["username"]."帳號:".$row["account"]."性別:";
if($row["sex"] == 1)
echo "男";
else
echo "女";
echo "日期:".$row["date1"];?〉
〈a href="edit.php?id= 〈? echo $row["id"]; ?〉"〉編輯〈/a〉
〈a href="del.php?id= 〈? echo $row["id"]; ?〉"〉刪除〈/a〉
〈br〉 〈?
}
?〉〈/body〉〈/html〉




3. 編輯

〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉

〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉




〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉





4. 刪除


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



六、 CSV檔案匯出與匯入
1. 匯入【接收端網頁】


〈? include("server.php"); ?〉〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉匯入 CSV 檔〈/title〉〈/head〉〈body〉
〈?
$uploaddir='./';
if ($_FILES["CSVfile"]["name"] ) {
$temp_SQL = "";
$file2=mb_convert_encoding($_FILES["CSVfile"]["name"],"big5","utf8");
if(!move_uploaded_file($_FILES['CSVfile']['tmp_name'],$uploaddir.$file2)){
echo "上傳失敗〈br〉";
}
$fp = fopen($_FILES["CSVfile"]["name"] , "r");
while ( $ROW = fgetcsv($fp,$_FILES["CSVfile"]["size"] ) ) {
if ( strlen($ROW[0]) ) {
if ( strlen($temp_SQL) ) $temp_SQL .= ", ";
$temp_SQL .= "('" . $ROW[0] . "', '" . $ROW[1] . "', '" . $ROW[2] . "', '" .
$ROW[3] . "') ";
}
}
fclose($fp);
$sql = "INSERT INTO student1(sid, username,address,birthday) VALUES " . $temp_SQL;
mysql_query($sql) or die(mysql_error());
}
else {
echo "Error!";
}?〉〈/body〉〈/html〉



2. 匯出


〈?
include("server.php");
$file = 'export';
$result = mysql_query("show columns from student1");
$i = 0;
if (mysql_num_rows($result) 〉 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("select * from student1");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j〈$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
};
ini_set("date.timezone","Asia/Taipei");
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?〉



七、 上傳與解壓縮


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉zip檔案上傳〈/title〉〈/head〉〈body〉〈?
include ('pclzip.lib.php');
$tmp_folder = basename($_FILES['myfile']['name']);
if(is_dir($tmp_folder))
{
echo "資料夾已存在"."〈br〉";
}
else
{
//echo "資料夾不存在"."〈br〉";
if(mkdir($tmp_folder, 0700))
echo "成功建立資料夾"."〈br〉";
else
echo "建立資料夾失敗"."〈br〉";
}
if($_FILES['myfile']['size']〉0){
if(is_uploaded_file($_FILES['myfile']['tmp_name'])&&$_FILES['myfile']['type']=='application/zip'){
if(is_writeable($tmp_folder)){
if(move_uploaded_file($_FILES['myfile']['tmp_name'],$tmp_folder.'/'.$_FILES['myfile']['name'])){
echo '檔案上傳成功!〈p〉';
$hzip = new PclZip($tmp_folder.'/'.$_FILES['myfile']['name']);
if($hzip-〉extract($tmp_folder.'/'))
echo '檔案解壓縮成功!';
else
echo '檔案解壓縮失敗!';
} else
echo '檔案上傳失敗!〈p〉';
} else
echo '檔案無法寫入!';
} else
echo '沒有上傳檔案!';
}
?〉〈/body〉〈/html〉




八、 其他

星期二, 11月 09, 2010

PHP&MySQL--9 2010年11月課程講義預覽

PHP&MySQL--9
一、 補充:資料表正規化
1. 第一正規化


1. 欄位內容必須是單一內容。
2. 同一欄位的內容必須是相同的類型。
3. 必須要有一個或一組主鍵可以辨識資料。
4. 不可用很多欄位來表達同一筆資料。




2. 第二正規化


1.無法單獨新增一筆學生資料。因為 Subject_id是主鍵之一,不能為空值。當學生尚未選修任何課程之前,將無法寫入資料表。  
2.無法單獨刪除一筆成績資料。如果打算刪除科目代表為P1234這個科目資訊,您會把這名學生資訊也刪除,學號99345這位同學資料就此消失。
3.需要同步異動的資料太多。假設學號為99312 這個學生搬家了,那麼我們得異動其中的 6 筆紀錄。



第一正規化之後資料表將產生以下的問題:

第二正規化將要求資料表裡所有的資料必需與該資料表的主鍵有相依關係,以避免資料重複與不一致性。若某欄位只和主鍵的一部份而非全部有關的話,請將這些欄位獨立成另外一個資料表。

3. 第三正規化


1.無法單獨新增一筆縣市資料。因為 Stu_no 是主鍵,不能是空值;因此,若無任何學生居住的某個縣市,其郵遞區號資料將無法被事先建立。  
2.無法單獨刪除一筆學生資料。如果我們打算刪除 99524 這筆資料的話,新北市郵遞區號資料也將一併消失。
3.更新郵遞區號困難。假設有兩位同學同住在台中市清水區,若郵遞區號變更,則要同時異動多筆紀錄。



第三正規化將要求資料表內刪除不依賴主鍵的欄位,也就是不應該存在遞移相依性欄位,將非主鍵決定的欄位獨立為新資料表。

二、 人數統計分頁分析【提供display1.php,並請修改網頁內容】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉人數統計分析〈/title〉〈/head〉
〈body〉〈?
include("server.php");
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else{$page = 1;}
$page_size = 10;
$sql = "select count(*) as total from record";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total = $row['total'];
if(isset($total))
{
if( $total 〈 $page_size )
$page_count = 1;
if( $total % $page_size )
$page_count = (int)($total / $page_size) + 1;
else
$page_count = $total / $page_size;
}
else
$page_count = 0;
$page_number = '';
if( $page == 1 )
$page_number .= '[第一頁][上一頁] ';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page=1〉第一頁〈/a〉]';
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page-1).'〉'."上一頁〈/a〉]";
}
if(($page == $page_count) || ($page_count == 0))
$page_number .= '[下一頁][尾頁]';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page+1).'〉'."下一頁〈/a〉]";
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.$page_count.'〉'."尾頁〈/a〉]";
}
if(isset($total)) {
$sql = "select * from record limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
echo "〈table border=1〉";
echo "〈tr〉〈td〉時間〈/td〉〈td〉ip〈/td〉〈/tr〉";
while($list1=mysql_fetch_array($result))
{
echo "〈tr〉〈td〉".$list1['visitday']."〈/td〉";
echo "〈td〉".$list1['ip']."〈/td〉〈/tr〉"; }
echo "〈/table〉";
echo $page_number; }
?〉〈/body〉〈/html〉



三、 檔案上傳
1. 表單網頁【提供upload.htm】


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉檔案上載〈/title〉〈/head〉〈body〉
〈form action="upload.php" method="post" enctype="multipart/form-data"〉
〈input type="hidden" name="MAX_FILE_SIZE" value="1000000"〉
Send this file: 〈input name="userfile" type="file"〉
〈input type="submit" value="Send File"〉
〈/form〉〈/body〉〈/html〉



2. 接收端網頁【提供upload.php,並請修改網頁內容】


〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉檔案上傳:接收端網頁〈/title〉〈/head〉〈body〉〈?
$uploaddir='./tmp/';
print "〈pre〉";
$file2=mb_convert_encoding($_FILES["userfile"]["name"],"big5","utf8");
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$file2))
{ echo "檔名:".$_FILES['userfile']['name']."〈br〉" ;
echo "大小:".$_FILES['userfile']['size']."〈br〉" ;
echo "型態:".$_FILES['userfile']['type']."〈br〉" ; }
else
{
print_r ($_FILES);
$a=$_FILES['userfile']['error'];
echo "a-〉".$a;
if ($a==1)
{ die("檔案大小超出 php.ini:upload_max_filesize 限制"); }
if($a==2)
{ die("檔案大小超出 MAX_FILE_SIZE 限制"); } }
echo $a;
?〉〈/body〉〈/html〉



四、 header( )進階使用
1. 開啟jpg圖檔【提供jpg1.php,並請修改網頁內容】


〈?
$im = imagecreatefromjpeg("test.jpg");
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im); ?〉



2. 與上例有關,增加ip限制【提供lockip.php,並請修改網頁內容】


〈?
function allow_ip($addr)
{
$first = substr($addr,0,(strrpos($addr,".")));
$second = substr($addr,(strrpos($addr,".")+1));
if(($first=="140.111.144") && ($second〉=1 && $second 〈=255))
{ return true; }
else
{ return false; }
}?〉



3. ip限制才能看圖【請自行撰寫】


〈?
require("lockip.php");
$addr_ip=$_SERVER['REMOTE_ADDR'];
$flag=allow_ip($addr_ip);
if(!$flag)
{die("不能看圖,IP位置不合法");}
include('./jpg1.php'); ?〉



4. 連結PHP檔案瀏覽圖片 【提供jpg3.php,並請修改網頁內容】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉檢測縮圖並做連結〈/title〉〈/head〉〈body〉
按下連結可察看原圖〈br〉
〈a href="jpg2.php"〉
〈img src = "jpg1.php" alt="看不到" width="100" height="90" border="0"〉〈/a〉
〈/body〉〈/html〉




五、 開啟網頁前的認證【提供auth1.php,並請修改網頁內容】

〈? header("Content-type: text/html; charset=utf-8");
if (empty($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please input"');
header('HTTP/1.0 401 Unauthorized');
echo '請輸入正確的帳號及密碼, 不可以取消!';
exit;
} else {
$correctName="pcschool";
$correctpwd="mysql" ;
if (($_SERVER['PHP_AUTH_USER'] != $correctName) or
($_SERVER['PHP_AUTH_PW'] !=$correctpwd)){
echo "登入失敗,請開啟新的瀏覽器重新登入"; }} ?〉




六、 下載的檔案與實際的檔案不同【請自行撰寫】


〈?php
include("auth1.php"); //可不加上此行
header("Content-type: text/html; charset=utf-8");
$file="./9707.zip"; // 實際檔案的路徑+檔名
$filename="0714.zip"; // 下載的檔名
header("Content-type: ".filetype("$file"));//指定類型
header("Content-Disposition: attachment; filename=".$filename."");
readfile($file); ?〉



七、 上傳多個檔案【提供表單網頁upload4.htm及接收資料網頁upload4.php,再請修改】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉處理上傳多個檔案〈/title〉〈/head〉 〈body〉〈?
$uploaddir='./upload/';
$i=count($_FILES["fileupload"]["name"]);
for ($j=0;$j〈$i;$j++){
$tmpfile=$_FILES["fileupload"]["tmp_name"][$j];
$file2=mb_convert_encoding($_FILES["fileupload"]["name"][$j],"big5","utf8");
if(move_uploaded_file($tmpfile,$uploaddir.$file2)){
echo "上傳成功〈br〉";
echo "檔案名稱:".$_FILES["fileupload"]["name"][$j]."〈br〉";
echo "檔案類型:".$_FILES["fileupload"]["type"][$j]."〈br〉";
echo "檔案大小:".$_FILES["fileupload"]["size"][$j]."〈br〉";
}else{
echo $_FILES["fileupload"]["tmp_name"][$j]."上傳失敗!〈br /〉";
switch ($_FILES["fileupload"]["error"][$j]){
case 1:
echo "失敗原因:大小超過php.ini內設定 upload_max_filesize"."〈br〉";
break;
case 2:
echo "失敗原因:大小超過表單設定 MAX_FILE_SIZE"."〈br〉";
break;
case 3:
echo "失敗原因:上傳不完整"."〈br〉";
break;
case 4:
echo "失敗原因:沒有檔案上傳"."〈br〉";
break;
case 6:
echo "失敗原因:暫存資料夾不存在"."〈br〉";
break;
case 7:
echo "失敗原因:上傳檔案無法寫入"."〈br〉";
break;
case 8:
echo "失敗原因:上傳停止"."〈br〉";
break;
} } } ?〉
〈/body〉〈/html〉




八、 繪製圖片—graph.php


〈? $height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, 'Sales', $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);?〉



九、 如何呼叫已經產生圖片的網頁? (請自行設計)



〈img src="graph.php" height=200 width=200 alt="顯示php的內容"〉




十、 輸入資料檢驗驗證碼
1. 表單部分


〈html〉 〈head〉〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉〈body〉〈form action="input.php" method="post"〉
〈input type="text" name="graph" maxlength="7" size="7"〉
〈input type="submit"〉〈/form〉
〈img src="graph.php" width="200" height="200" alt="show image"/〉
〈/body〉〈/html〉



2. 驗證碼部分(graph.php,請修改網頁)

〈? session_start( );
$_SESSION['test']=generatorPassword( );
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, $_SESSION['test'], $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);
function generatorPassword( )
{ $password_len = 7;
$password = '';
$word = '0123456789';
$len = strlen($word);
for ($i = 0; $i 〈 $password_len; $i++) {
$password .= $word[rand( ) % $len]; }
return $password; } ?〉




3. 接收資料


〈?session_start( ); ?〉〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉驗證資料〈/title〉〈/head〉〈body〉
〈? if ($_POST['graph']==$_SESSION['test'])
{ ?〉
〈script language="JavaScript"〉
alert("〈? echo "輸入資料正確,轉往google"; ?〉");
location.href="http://www.google.com";
〈/script〉〈? }
else
{ ?〉
〈script language="JavaScript"〉
alert("輸入資料不正確,請重新輸入");
history.back( );
〈/script〉 〈?
} ?〉 〈/body〉〈/html〉




十一、 簡單資料操作
1. 新增

〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉新增〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");

mysql_query("insert into student (account,username,sex,date1) values('$account','$username','$sex','$temp_date') ") or die("error");

header("location:index.php");
?〉〈/body〉〈/html〉




2. 列表


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉表列資料〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$res =mysql_query("select * from student") or dir("sql error");
?〉〈a href="add.htm"〉新增〈/a〉〈br〉 〈?
while($row=mysql_fetch_array($res)){
echo "姓名:".$row["username"]."帳號:".$row["account"]."性別:";
if($row["sex"] == 1)
echo "男";
else
echo "女";
echo "日期:".$row["date1"];?〉
〈a href="edit.php?id= 〈? echo $row["id"]; ?〉"〉編輯〈/a〉
〈a href="del.php?id= 〈? echo $row["id"]; ?〉"〉刪除〈/a〉
〈br〉 〈?
}
?〉〈/body〉〈/html〉





3. 編輯


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉
〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉






〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉




4. 刪除


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



十二、 其他

星期五, 11月 05, 2010

PHP&MySQL--8 2010年11月課程講義預覽

PHP&MySQL--8
一、 表單送資料至PHP後查詢資料【提供表單網頁】
1. 練習SQL語法中where語法

select employeeid,firstname from employees where (employeeid 〉= 3);
select employeeid,firstname from employees where (employeeid in(2,4,9));
select employeeid,firstname from employees where (employeeid not in(2,4,9));
select country,companyname from customers where (country in('Argentina','Mexico'));

select region,companyname from customers where (region is null);
select region,companyname from customers where (region is not null);
select firstname from employees where (firstname like 'a%');
select firstname from employees where (firstname like '%a%');
select firstname from employees where (firstname like '%a');
select companyname,contactname,country from customers where (country in('France','Germany') and companyname like 'b%');

select companyname,contactname,country from customers where (country in('France','Germany') or companyname like 'b%');








2. 若直接將post傳送過來的資料送至sql語法?【提供query03.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉




3. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";



4. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
//$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";
$a=$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='$a'";



5. 測試各種模糊查詢【提供query04.php,但請修改表單網頁路徑】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉
〈/head〉〈body〉
〈?php
include("server.php");
//測試各種模糊查詢
$sql="SELECT * FROM orders where CustomerID like '%$a%'";
$sql="SELECT * FROM orders where CustomerID like '$a%'";
$sql="SELECT * FROM orders where CustomerID like '%$a'";
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



二、 傳遞參數給SQL語法:例如查詢customers筆數與範圍(提供表單)
【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉回傳各欄位資料〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
include("unicode.php");
echo $_SERVER['HTTP_REFERER']."〈br〉";
//$sql1="select * from customers limit ".$_POST['first'].",".$_POST['total'];
//$sql1="select * from customers limit $_POST['first'],$_POST['total']";

$a=$_POST['first'];
$b=$_POST['total'];
$sql1="select * from customers limit $a,$b";
//echo $sql1;
//$sql = @mysql_query($sql1);
$sql = mysql_query($sql1) or die(mysql_error());
$rows = @mysql_num_rows($sql);
//echo $rows;
if($rows==""){
echo "查無資料!";
}else{
echo "有 ".$rows." 筆資料喔!";
}
?〉〈/body〉〈/html〉



限制筆數之SQL語法:

select employeeid,firstname, lastname from employees ;
select employeeid,firstname, lastname from employees limit 1,30;
select employeeid,firstname, lastname from employees limit 0,5;
select employeeid,firstname, lastname from employees limit 6;

select productid,productname,unitprice from products order by unitprice desc limit 1;

select employeeid,firstname, lastname from employees order by rand() limit 6;




三、 人數統計分頁分析【提供display1.php,並請修改網頁內容】



〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉人數統計分析〈/title〉〈/head〉
〈body〉〈?
include("server.php");
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else{$page = 1;}
$page_size = 10;
$sql = "select count(*) as total from record";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total = $row['total'];
if(isset($total))
{
if( $total 〈 $page_size )
$page_count = 1;
if( $total % $page_size )
$page_count = (int)($total / $page_size) + 1;
else
$page_count = $total / $page_size;
}
else
$page_count = 0;
$page_number = '';
if( $page == 1 )
$page_number .= '[第一頁][上一頁] ';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page=1〉第一頁〈/a〉]';
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page-1).'〉'."上一頁〈/a〉]";
}
if(($page == $page_count) || ($page_count == 0))
$page_number .= '[下一頁][尾頁]';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page+1).'〉'."下一頁〈/a〉]";
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.$page_count.'〉'."尾頁〈/a〉]";
}
if(isset($total)) {
$sql = "select * from record limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
echo "〈table border=1〉";
echo "〈tr〉〈td〉時間〈/td〉〈td〉ip〈/td〉〈/tr〉";
while($list1=mysql_fetch_array($result))
{
echo "〈tr〉〈td〉".$list1['visitday']."〈/td〉";
echo "〈td〉".$list1['ip']."〈/td〉〈/tr〉"; }
echo "〈/table〉";
echo $page_number; }
?〉〈/body〉〈/html〉



四、 檔案上傳
1. 表單網頁【提供upload.htm】


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉檔案上載〈/title〉〈/head〉〈body〉
〈form action="upload.php" method="post" enctype="multipart/form-data"〉
〈input type="hidden" name="MAX_FILE_SIZE" value="1000000"〉
Send this file: 〈input name="userfile" type="file"〉
〈input type="submit" value="Send File"〉
〈/form〉〈/body〉〈/html〉



2. 接收端網頁【提供upload.php,並請修改網頁內容】

〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉檔案上傳:接收端網頁〈/title〉〈/head〉〈body〉〈?
$uploaddir='./tmp/';
print "〈pre〉";
$file2=mb_convert_encoding($_FILES["userfile"]["name"],"big5","utf8");
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$file2))
{ echo "檔名:".$_FILES['userfile']['name']."〈br〉" ;
echo "大小:".$_FILES['userfile']['size']."〈br〉" ;
echo "型態:".$_FILES['userfile']['type']."〈br〉" ; }
else
{
print_r ($_FILES);
$a=$_FILES['userfile']['error'];
echo "a-〉".$a;
if ($a==1)
{ die("檔案大小超出 php.ini:upload_max_filesize 限制"); }
if($a==2)
{ die("檔案大小超出 MAX_FILE_SIZE 限制"); } }
echo $a;
?〉〈/body〉〈/html〉



五、 header( )進階使用
1. 開啟jpg圖檔【提供jpg1.php,並請修改網頁內容】


〈?
$im = imagecreatefromjpeg("test.jpg");
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im); ?〉




2. 與上例有關,增加ip限制【提供lockip.php,並請修改網頁內容】



〈?
function allow_ip($addr)
{
$first = substr($addr,0,(strrpos($addr,".")));
$second = substr($addr,(strrpos($addr,".")+1));
if(($first=="140.111.144") && ($second〉=1 && $second 〈=255))
{ return true; }
else
{ return false; }
}?〉



3. ip限制才能看圖【請自行撰寫】


〈?
require("lockip.php");
$addr_ip=$_SERVER['REMOTE_ADDR'];
$flag=allow_ip($addr_ip);
if(!$flag)
{die("不能看圖,IP位置不合法");}
include('./jpg1.php'); ?〉



4. 連結PHP檔案瀏覽圖片 【提供jpg3.php,並請修改網頁內容】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉檢測縮圖並做連結〈/title〉〈/head〉〈body〉
按下連結可察看原圖〈br〉
〈a href="jpg2.php"〉
〈img src = "jpg1.php" alt="看不到" width="100" height="90" border="0"〉〈/a〉
〈/body〉〈/html〉



六、 開啟網頁前的認證【提供auth1.php,並請修改網頁內容】

〈? header("Content-type: text/html; charset=utf-8");
if (empty($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please input"');
header('HTTP/1.0 401 Unauthorized');
echo '請輸入正確的帳號及密碼, 不可以取消!';
exit;
} else {
$correctName="pcschool";
$correctpwd="mysql" ;
if (($_SERVER['PHP_AUTH_USER'] != $correctName) or
($_SERVER['PHP_AUTH_PW'] !=$correctpwd)){
echo "登入失敗,請開啟新的瀏覽器重新登入"; }} ?〉




七、 下載的檔案與實際的檔案不同【請自行撰寫】


〈?php
include("auth1.php"); //可不加上此行
header("Content-type: text/html; charset=utf-8");
$file="./9707.zip"; // 實際檔案的路徑+檔名
$filename="0714.zip"; // 下載的檔名
header("Content-type: ".filetype("$file"));//指定類型
header("Content-Disposition: attachment; filename=".$filename."");
readfile($file); ?〉




八、 其它

星期三, 11月 03, 2010

2010年11月:東區認證中心VB.Net 物件導向課程

這次課程將跟大家說明物件導向的封裝、繼承與多形等觀念。
程式設計採視窗程式設計(Windows Form),所以與坊間書籍於終端機環境
設計有很大的不同,但觀念相同。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。

PHP&MySQL--7 2010年10月課程講義說明

因為第六次上課對於MySQL新增使用者說明與操作的時間較久,所以
第七次上課講義同第六次上課第二頁之後的講義。