博客來網路書店查詢

書名

博客來網路書店查詢

星期三, 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( ); } }



十、 其他

沒有留言: