ผมเขียนสครัปเช็คการกรอก...
TRANSCRIPT
http://php.deeserver.net http://www.siamphp.nethttp://www.thaicreate.comhttp://school.obec.go.th/prakr/clock/index.htm เวบนาฬกาhttp://www.codetukyang.com/java/button/index.htm refresh หนาเจอrefresh<form><input type=button value="Refresh" onClick="javascript:location.reload();"></form><META HTTP-EQUIV="REFRESH" CONTENT="1; URL=show.php">
if($_SESSION["username_id"] == "") {header("location: login.php");//print "<meta http-equiv=\"refresh\"
content=\"0;URL=login.php\">";}v
ผมเขยนสครปเชคการกรอก พอมนเชคแลว มนเตอนแลว กด โอเค แลวมนกรเฟรชทำาใหขอมลทกรอกมนหาย ตองกรอกใหมตงแตตน
ผมจะกรอกขอมลแลวสง พมพเลยครบ
<script >
<!--function chk_frm(){// ถาตวแรกไมม มนจะไมเชค นะจะ
if(document.frmdata.name.value == ""){alert("กรณากรอกชอผสงซอดวยนะครบ ");document.frmdata.name.focus();return false;}
if(document.frmdata.lastname.value == ""){alert("กรณากรอกนามสกล ");document.frmdata.lastname.focus();return false;}
if(document.frmdata.place.value == ""){alert("กรณากรอกสถานทเพอจดสงหนงสอ และเพอประโยชนของตวทานเอง");document.frmdata.place.focus();return false;}
}--></script>
<form method="post" enctype="multipart/form-data " name="frmdata" id="frmdata"><input name="name" type="text" id="name"><input name="lastname" type="text" id="lastname"><input name="place" type="text" id="place" size="70">
<input type="submit" name="Submit" value="พมพใบสงซอ" onClick="chk_frm()"><input type="reset" name="Submit2" value="ลบขอมล">
ทผมใช onClick เพราะผมลองยายมนมาจาก onsubmit ของฟอรมเฉยๆคดวามนจะหาย แตกเหมอนเดม
เฮอ !!!! เหนอย
ไมเหนยากเรยยคบ
<script ><!--function chk_frm(){// ถาตวแรกไมม มนจะไมเชค นะจะ
if(document.frmdata.name.value == ""){alert("กรณากรอกชอผสงซอดวยนะครบ ");document.frmdata.name.focus();return false;}
if(document.frmdata.lastname.value == ""){alert("กรณากรอกนามสกล ");document.frmdata.lastname.focus();return false;}
if(document.frmdata.place.value == ""){alert("กรณากรอกสถานทเพอจดสงหนงสอ และเพอ
ประโยชนของตวทานเอง");document.frmdata.place.focus();return false;}
return true;}--></script>
<form method="post" enctype="multipart/form-data " name="frmdata" id="frmdata" onsubmit="return chk_frm()"><input name="name" type="text" id="name"><input name="lastname" type="text" id="lastname"><input name="place" type="text" id="place" size="70">
<input type="submit" name="Submit" value="พมพใบสงซอ"><input type="reset" name="Submit2" value="ลบขอมล">
แคนกนาจะไดคบ
Set focus<body onload="Javascript:document.form2.text2.focus();"><form name="form2"><input type="text" name="text2"/></form> </body>
document.form.text1.focus();
<body onload="document.form1.txtname.focus();">
<script language="javascript">function test_option()
{var length = document.form1.text_search.value.length // ตวอกษรทกรอก for(var i=0;i<document.form1.select1.length;i++) // วนตามจำานวน option ใน select{ if(document.form1.text_search.value.substring(0, length) == document.form1.select1.options.value.substring(0, length)){ document.form1.select1.options.selected=true;return false} }
}
</script> <form name="form1" method="post"><input type="text" name="text_search" onkeyup="test_option()" onkeydown="test_option()" onkeypress="test_option()"/><select name="select1"><option value="">เลอก</option>
<option value="abd">abd</option><option value="abc">abc</option><option value="cea">cea</option><option value="bed">bed</option><option value="cdb">cdb</option> <option value="acd">acd</option><option value="bdc">bdc</option> </select></form>
//--------------------------------- Check box ------------------------------//<input name="ckdelete[]" type="checkbox" id="ckdelete[]" value="<?=id?>" />
// เมอทำาการกดสงขอมลมา ใหทำาการรบคาดงคำาสงขางลาง (ckdelete[] ตว [] ทใสดานหลงนจะทำาใหเปนขอมลอะเรย)
if(!empty($_POST['ckdelete'])){ //ถารบคามาแลวไมวาง$deletedata=$_POST['ckdelete']; //เกบคาทเลอกทงหมดไวในตวแปร $deletedatafor($i=0;$i<count($deletedata);$i++){ // วนลปตามจำานวนทเลอกมาทงหมด$IdDel=$deletedata[$i]; //เกบคา ID ของ
แตละตวไวในตวแปร $IdDel$sql="delete from table Where id=$IdDel";mysql_query($sql);}header("Location:index.php");
ลองดอนนนะครบ ศกษาดเองเดอ
<form name="form1" method="post" action="sample6.php"><script language="JavaScript">function checkAll( n ) {var f = document.all;var c = f.chall.checked;for (i=0; i < n; i++) {cb = eval( 'f.cb' + i );cb.checked = c;}}</script><?$host="localhost";$username="";$pass_word="";$db="mydatabase";
$tb="color";mysql_connect( $host,$username,$pass_word) or die ("ตดตอกบฐานขอมล Mysql ไมได ");mysql_select_db($db) or die("เลอกฐานขอมลไมได");
$sql="Select * From $tb order by color_no asc";$db_query=mysql_db_query($db,$sql);$num_rows=mysql_num_rows($db_query); /* นบ Reccord ทพบ */
?>เลอกรายการ<table width="30%" border="1" cellspacing="1" cellpadding="1"><tr> <td width="24%">รหสส</td><td width="44%">รายละเอยดส</td><td width="32%"><input type="checkbox" name="chall" onClick="checkAll(document.all.boxchecked.value)">ทงหมด</td></tr>
</table><?$a=0;while($a < $num_rows){$result = mysql_fetch_array($db_query);$color_no=$result[color_no];$color_name=$result[color_name];?><table width="30%" border="1" cellspacing="1" cellpadding="1"><tr> <td width="23%"> <?echo $color_no;?></td><td width="45%"> <?echo $color_name;?></td><td width="32%"><input type="checkbox" name="cb<?=$a;?>" id="cb[]" value="<?echo "$color_no"; ?>"></td></tr></table>
<?$a++;
}?><br><input name="boxchecked" type="hidden" id="boxchecked" value="<?=$a;?>"></form>
Chck box ลง array
<html><head><body bgcolor="#FFFFFF"><form name="form1" method="post" action="sample4.php"><?$host="localhost";$username="";
$pass_word="";$db="mydatabase";$tb="color";mysql_connect( $host,$username,$pass_word) or die ("ตดตอกบฐานขอมล Mysql ไมได ");mysql_select_db($db) or die("เลอกฐานขอมลไมได"); $sql="Select * From $tb order by color_no asc";$db_query=mysql_db_query($db,$sql);$num_rows=mysql_num_rows($db_query); /* นบ Reccord ทพบ */?><table width="24%" border="1" cellspacing="1" cellpadding="1"><tr> <td width="24%">รหสส</td><td width="52%">รายละเอยดส</td><td width="24%">เลอก</td></tr></table><?$a=0;while($a < $num_rows){
$result = mysql_fetch_array($db_query);$color_no=$result[color_no];$color_name=$result[color_name];?><table width="24%" border="1" cellspacing="1" cellpadding="1"><tr> <td width="24%"> <?echo $color_no;?></td><td width="52%"> <?echo $color_name;?></td><td width="24%"><input type="checkbox" name="color_array[]" value="<?echo "$color_no"; ?>"></td></tr></table><?$a++;}?><br><input type="submit" name="Submit" value="ตรวจสอบ"></form>
</body></html>
<html><head><body bgcolor="#FFFFFF"><?$host="localhost";$username="";$pass_word="";$db="mydatabase";$tb="color";mysql_connect( $host,$username,$pass_word) or die ("ตดตอกบฐานขอมล Mysql ไมได ");
mysql_select_db($db) or die("เลอกฐานขอมลไมได"); for ($i=0;$i<count($color_array);$i++) {
$color_no = $color_array[$i];$sql="Select * From $tb where color_no=$color_no"; $db_query=mysql_db_query($db,$sql);$result = mysql_fetch_array($db_query);$color_no=$result[color_no];$color_name=$result[color_name];echo "$color_no \t $color_name<br>";}?> </body></html><script type="text/javascript">var x=window.confirm("ขอความ")if (x)window.location.href = "index.php"elsewindow.location.href = "report.php"</script>
ขอรบกวนทกทานททราบกรณาชวยผมดวครบผมอยากไดjavascripts ท
เมอ tab หรอ Enter จะไป GetFocus ในชองใสขอมลถดไปdocument.all.name.focus();
แบบวาจะเขยนไหมน รอเวลา ประมาณ 5 วและจะมขอความขนหรอ รเซทหนาตางบาวเซอรหนะครบsleep(5);
<a href="#" class="style4" onClick="window.open('show_order.php?sale_id=<? echo $rs_list["sale_id"]?>','','scrollbars=yes,width=600,height=300')">
onclick="location.href='check_return.php?rt_id=<?echo $rs_list["rt_id"]?>'"
ใสไดเฉพาะ ตวเลขonKeyPress="if (event.keyCode < 48 || event.keyCode > 57){ alert('ใชไดแตตวเลขเทานน'); event.returnValue = false;}"
set focus echo “<script LANGUAGE='JavaScript'>ชอฟอรม.ชอ textbox.focus();alert('กรณากรอกสมาชกคนเดมทยงอยในรายการ')</script >”;
อยากให enter แลวลงไปบรรทดลาง ไมให submit
<SCRIPT language="Javascript">function check_data(){ if(!document.form1.text1.value){ document.form1.text1.focus();return false;
}else if(!document.form1.text2.value){ document.form1.text2.focus();return false;}else if(!document.form1.text3.value){ document.form1.text3.focus();return false;}}</SCRIPT>
<form method="post" name="form1"><input type="text" name="text1" onKeyPress="if (event.keyCode == 13){return check_data()}"/> <input type="text" name="text2" onKeyPress="if (event.keyCode == 13){return check_data()}"/> <input type="text" name="text3" onKeyPress="if (event.keyCode == 13){return check_data()}"/> <input type="submit" value="submit" /></form>
หรอ
คอคา event ของปม enterลองกดปมท textbox นดจะบอก event ของแตละปม<input type="text" name="text1" onKeyPress="alert(event.keyCode);return false;"/>
กำาลงโหลดขอมลกรณารอสกคร
ใสกอน </head>function waitPreloadPage() { //DOMif (document.getElementById){document.getElementById('prepage').style.visibility='hidden';}else{if (document.layers){ //NS4document.prepage.visibility = 'hidden';}else { //IE4document.all.prepage.style.visibility = 'hidden';}}}
แลวก ใสอนน
<body onload='waitPreloadPage()'><DIV id="prepage" style="position:absolute; font-family:arial; font-size:10px; left:0px; top:0px; background-color:white; layer-background-color:white; height:150%; width:100%;"> <TABLE width=100% style="position:absolute; left:0px; top:220px; "><TR><TD align="center"> <img src="images/animated_loading.gif"><B >Loading ... ... Please wait!</B></TD></TR></TABLE></DIV>
แปลงจำานวนตวเลข จำานวนเงนใหเปนอกษรภาษาไทย เชน 123 = หนงรอยยสบสามบานถวน 123.25 = หนงรอยยสามบาทยสบหาสตางค
<?function conv_num2str($number){$t1 = array("ศนย", "หนง", "สอง", "สาม", "ส", "หา", "หก", "เจด", "แปด", "เกา");
$t2 = array("เอด", "ย", "สบ", "รอย", "พน", "หมน", "แสน", "ลาน");$zerobahtshow = 0; // ในกรณทมแตจำานวนสตางค เชน 0.25 หรอ .75 จะใหแสดงคำาวา ศนยบาท หรอไม 0 = ไมแสดง, 1 = แสดง(string) $number;$number = explode(".", $number);if(!empty($number[1])){if(strlen($number[1]) == 1){$number[1] .= "0";}elseif(strlen($number[1]) > 2){if($number[1]{2} < 5){$number[1] = substr($number[1], 0, 2);}else{$number[1] = $number[1]{0}.($number[1]{1}+1);}}}for($i=0; $i<count($number); $i++){$countnum[$i] = strlen($number[$i]);if($countnum[$i] <= 7){$var[$i][] = $number[$i];}else{$loopround = ceil($countnum[$i]/6);for($j=1; $j<=$loopround; $j++){if($j == 1){$slen = 0;
$elen = $countnum[$i]-(($loopround-1)*6);}else{$slen = $countnum[$i]-((($loopround+1)-$j)*6);$elen = 6;}$var[$i][] = substr($number[$i], $slen, $elen);}}$nstring[$i] = "";for($k=0; $k<count($var[$i]); $k++){if($k > 0) $nstring[$i] .= $t2[7];$val = $var[$i][$k];$tnstring = "";$countval = strlen($val);for($l=7; $l>=2; $l--){if($countval >= $l){$v = substr($val, -$l, 1);if($v > 0){if($l == 2 && $v == 1){$tnstring .= $t2[($l)];}elseif($l == 2 && $v == 2){$tnstring .= $t2[1].$t2[($l)];}else{$tnstring .= $t1[$v].$t2[($l)];}}
}}if($countval >= 1){$v = substr($val, -1, 1);if($v > 0){if($v == 1 && $countval > 1 && substr($val, -2, 1) > 0){$tnstring .= $t2[0];}else{$tnstring .= $t1[$v];}}}$nstring[$i] .= $tnstring;}}$rstring = "";if(!empty($nstring[0]) || $zerobahtshow == 1 || empty($nstring[1])){if($nstring[0] == "") $nstring[0] = $t1[0];$rstring .= $nstring[0]."บาท";}if(count($number) == 1 || empty($nstring[1])){$rstring .= "ถวน";}else{$rstring .= $nstring[1]."สตางค";}
return $rstring;}
// เรยกใชฟงคชนecho conv_num2str(12345);?>
//คราวนลองแบบ INNER JOIN กนบาง$conn = mysql_connect("localhost", "root", ""); //เชอมตอกบฐานขอมลmysql_select_db("company"); //เลอกใชฐานขอมลชอ company$sql = "SELECT * FROM employee";$sql .= " INNER JOIN position_mstr ON (employee.pos_id = position_mstr.pos_id)";$sql .= " INNER JOIN department_mstr ON (employee.dept_id = department_mstr.dept_id)";$result = mysql_query($sql);LEFT JOINจรงๆ แลว INNER JOIN กบ LEFT JOIN นนกไมแตกตาง
กนหรอกครบ ประมาณวาเปลยนจากคำาวา INNER เปน LEFT เทานนเอง เอกๆ ดงนนโคดทเขยนดวย INNER JOIN ในหวขอขางตน เรานำามาแกเปน
<?php//คราวนลองแบบ LEFT JOIN กนบาง$conn = mysql_connect("localhost", "root", ""); //เชอมตอกบฐานขอมลmysql_select_db("company"); //เลอกใชฐานขอมลชอ company$sql = "SELECT * FROM employee";$sql .= " LEFT JOIN position_mstr ON (employee.pos_id = position_mstr.pos_id)";$sql .= " LEFT JOIN department_mstr ON (employee.dept_id = department_mstr.dept_id)";$result = mysql_query($sql);SELECT .A.d1,Ad2,A.d3,B.e1,C.f1,D.g1 ;FROM A ;LEFT JOIN B ON A.d1=B.d1 ;LEFT JOIN C ON A.d2=C.d2 ;LEFT JOIN C ON A.d3=D.d3 ;ORDER BY A.d1 ;ORselect A.custcode,A.custname,A.custaddr1,A.custaddr2,C.custtypecode,C.custtypename,B.vatgroupcode,
B.remark,F.custgroupcode,F.custgroupname,D.saleareacode,D.saleareaname,E.busitypecode,E.busitypename,A.billdiscfrom emcust ALEFT outer join emvatgroup B ON a.vatgroupid=b.vatgroupid LEFT outer join emcusttype C ON a.custtypeid=c.custtypeidLEFT outer join emsalearea D ON a.saleareaid=d.saleareaidLEFT outer join embusitype E ON a.busitypeid=e.busitypeidLEFT outer join emcustgroup F ON a.custgroupid=f.custgroupidorder by A.custcode
ทศนยม 2 ตำาแหนง
number_format($number,2);
alert ทำายงไงลองเอาไปประยกใหตรงตามความตองการดนะคบ
<script language="javascript">alert(" บนทกขอมลเรยบรอยแลว ")
ok=confirm(" ตองการทจะกลบไปหนาเดมหรอไม ") if(ok) {window.location="index.php?id="+<? echo $course->id ?>;}else{window.location="report.php?id="+<? echo $course->id ?>; } </script><script language="JavaScript">function submitForm(){if(st_id() && st_name() && st_lastname() ){return true;}return false}
function st_id() {if (document.form1.st_id.value == "") {alert ("\n ยงปอนขอมลไมครบ\n\n กรณาปอน[รหสนกศกษา]")document.form1.st_id.focus();return false;}return true;}
function st_name() {if (document.form1.st_name.value == "") {alert ("\n ยงปอนขอมลไมครบ\n\n กรณาปอน[ชอนกศกษา]")document.form1.st_name.focus();return false;}return true;}
function st_lastname() {if (document.form1.st_lastname.value == "") {alert ("\n ยงปอนขอมลไมครบ\n\n กรณาปอน[นามสกลนกศกษา]")document.form1.st_lastname.focus();return false;}return true;}
function phone_no() {if (document.form1.phone_no.value == "") {alert ("\n ยงปอนขอมลไมครบ\n\n กรณาปอน[ท
อยนกศกษา]")document.form1.phone_no.focus();return false;}return true;}</script>
OOP และ PHP: ตวอยางจากรปสเหลยมโดย อ.ทองจล ขนขาว
ความรพนฐาน HTML และ PHP เบองตน
OOP เปนการเขยน โปรแกรมเชงวตถ ทกำาลงเปนทนยมอยางแพรหลาย โปรแกรม ASP ในปจจบนไดเปลยนโฉมจาก ภาษา script เปน ASP.NET ซงเปนโปรแกรมเชงวตถอยางสมบรณ PHP กเชนเดยวกน เพราะ OOP ชวยใหเขยนโปรแกรมไดอยางสงางาม ตรวจสอบไดงาย สามารถนำาโคดทเขยนไว กลบมาใชใหมไดอยางประหยด จงสมควรอยางยงทจะตองทำาความเขาใจเรองของ OOP เพอเปนพนฐานในการเขยนโปรแกรมขนสงตอไป
ในบทความน จะพดถงหลกการของ OOP ทใชใน โปรแกรม PHP
ทกคนคงเคยเรยนเรองการหาพนทสเหลยมมาแลว สตรการหาพนท คอ ความยาว X ความสง ถาจะคำานวณหาเสนรอบรปกเอาความยาวกบความสงคณดวย 2 แลวเอามาบวกกน ทนสมมตวา ตองการเขยนโปรแกรมบนเวบ ใหคำานวณพนทสเหลยม โดยใช PHP วธทงายทสด คอ การสรางฟอรมเพอรบคาความยาว และ ความสง แลวใช PHP ใหคำานวณหาพนท ซงจะไดโคด ดงน
ใช HTML สรางฟอรมเพอรบคาความยาวและความสง บนทกและตงชอไฟลวา rectangle.html
<HTML><BODY><form action="rectangle.php" method="POST"> ความยาว: <input type="text" name="width"><br> ความสง: <input type="text" name="height"><br> <input type="submit" value=" สงขอมล "></form></BODY></HTML>
จากนนกจะสรางไฟล PHP เพอรบขอมลจาก rectangle.html โดยใชชอวา rectangle.php ดงน
<?php
$w=$_POST["width"]; //รบคาจากไฟล html$h=$_POST["height"];$area = $w * $h; // คำานวนพนท$perimeter = ($w + $h) * 2; // คำานวนเสนรอบรป ?> <html> <body> <p>ความยาว: <?=$w?><br />
ความสง: <?=$h?></p> <p>พนท: <?=$area?> ตารางหนวย<br />
เสนรอบรป: <?=$perimeter?></p> </body> </head>
จากตวอยางขางตนแทนทจะเขยนโปรแกรมใหคำานวณทกครง อาจจะเขยนเปนฟงชน ใหคำานวณหาพนทและเสนรอบรอบ แลวเรยกใชเมอตองการ
ตวอยางตอไปน เราจะสรางฟงชนเพอใหคำานวณพนทและเสนรอบรปสเหลยม โดยจะสรางไฟลใหมใชชอวา rect.php ในไฟลน เราจะเขยนฟงชนดงกลาว ดงน
<?php function rect_area($width,$height) {
return $width * $height; } function rect_perim($width,$height) {
return ($width + $height) * 2; } ?>
บนทกไว ในไฟลชอ rect.php
เราจะเรยกใชฟงชนนจากไฟล rectangle.php แตจะแกไขใหม ไมตองมการคำานวณ แตจะเรยกใชฟงชนจาก rect.php ดงน
<?php
require('rect.php'); // โหลดไฟลทมฟงชนทเราสรางแลว
$w=$_POST["width"];$h=$_POST["height"];
$area = rect_area($w,$h); //เรยกใชฟงชน คำานวณพนท$perimeter = rect_perim($w,$h); // เรยกใชฟงชนคำานวณเสนรอบรป?> <html> <!—แสดงผล --><body> <p>ความยาว: <?=$w?><br />
ความสง: <?=$h?></p> <p>พนท: <?=$area?><br />
เสนรอบรป: <?=$perimeter?></p> </body> </head>
การเขยนโปรแกรมเชงวตถ หรอ OOP คลายกบฟงชน เพยงแตเราจะสรางวตถตนแบบ หรอภาษาของ PHP เรยกวา CLASS เชน เราอาจจะสราง วตถทเรยกวา Rectangle โดยใหมนมการคำานวณหาพนท และคำานวณหาเสนรอบรป ดงนน ถาเราเรยกใช Rectangle และสงคา ความยาวและความสงให เราสามารถเรยกคาพนท และคาความยาวเสนรอบรปไดเลย
เราจะปรบไฟล rect.php เสยใหม เพอใหไฟลน สราง CLASS ทชอวา Rectangle ดงน
<?php class Rectangle { var $width; var $height;
function Rectangle($width, $height) { $this->width = $width; $this->height = $height; }
function area()
{ return $this->width * $this->height; }
function perimeter() { return ($this->width + $this->height) * 2; } } ?>
เรามาดรายละเอยดทละบรรทดเราเรมจากการบอกวา ตอไปนเราจะสรางแมแบบ หรอ CLASS ทชอวา Rectangle
class Rectangle {
ชอของ CLASS เรามกจะขนตนดวยอกษรตวใหญ PHP ไมบงคบวาจะตองเปนตวใหญ แตทำาใหเหมอน ๆ กบชาวบานกจะด เพราะจะเขาใจกนไดงายขน
ตอมาเรา ประกาศชอตวแปร คอ $width และ $height
var $width; var $height;
นเปนการประกาศตวแปรทอยในวตถทเรากำาลงสราง เทากบเปนการพดวา เจาสเหลยมทจะสรางขนจากแมแบบ (CLASS) อนนทกรป จะมความยาว และความสง ลกษณะทกำาหนดใหวตถอยางน ศพททาง PHP เรยกวาเปน Properties คอหมายถง คณสมบตของวตถหรอ Object ซงในทนกคอรปสเหลยมทเกดจากแมแบบทเรากำาลงสรางนทกรป ทจะตองม นนคอ รปสเหลยมทเกดจากแมแบบ หรอ CLASS นทกรป จะมความกวางและความยาว
ตอจากนนกเปนการสรางฟงชน ใหคำานวณ หาพนท
function Rectangle($width, $height) {
ขอใหสงเกตวา ชอฟงชน Rectangle เปนชอเดยวกบชอของแมแบบ หรอ CLASS พรอมทงกำาหนดตวแปรเอาไวดวย 2 ตว คอ $width และ $height ฟงชนทมชอเดยวกบชอ CLASS น เราเรยกวาเปน ตวสราง หรอ constructor
หนาทหลกของ constructor คอการกำาหนดคาตาง ๆ ใหวตถ และพรอมทจะเรยกใชงาน ในกรณของเรา เราจะกำาหนดคา ความยาวและความสงใหแก constructor ของเรา ดงน
$this->width = $width; $this->height = $height;
ตวแปร $this ในทนหมายถง วตถทเรากำาลงสรางขน ดงนน $this->width = $width; จงหมายถง ความกวางของวตถทเรากำาลงสรางขนน เทากบ ตวแปรความกวาง(ทกำาหนดไวขางตน ซงเปนตวแปรทรบคาทสงเขามา)
เครองหมาย -> แสดงความเปนเจาของ เชน $this->width หมายถง ความกวางของวตถทเรากำาลงสรางขนน
ตอจากนนกเปนฟงชนการหาพนทและคำานวณเสนรอบรป
function area() { return $this->width * $this->height; }
function perimeter() { return ($this->width + $this->height) * 2; }
ดเผน ๆ ฟงชนนคลายกบฟงขน rect_area และ rect_perim เพยงแตเราไมไดสงคาอะไรไปใหเทานน เพราะคาความยาวและความสงของวตถทเรากำาลงสรางมอยแลว เรยกใชไดทนท
ฟงชนของวตถทมอยประจำาตวในลกษณะน เราเรยกวาเปน methods หรอวธการ ซงเปนการคำานวณหาคาตาง ๆ โดยใชคณสมบตหรอ properties ของวตถทมอย
ทงหมดทกลาวมา เปนการวางแมแบบวา จะใชสรางรปสเหลยมทมคณสมบต (properties) 2 ประการคอ ความกวางและความสง และสเหลยมทสรางน จะมวธการ (methods) ใหคำานวณหาคาพนท และคาความยาวรอบรป
แตนนเปนเพยงแคแมแบบ ยงไมใชตวจรง การเรยกใชแมแบบ เรยกไดดงน
new Rectangle(20, 5)
เราใชคำาวา new เพอบอก PHP วา ใหสรางวตถใหม(new object) จากแมแบบ (class) ทชอวา Rectangle พรอมกนนนเรากสงคา ความยาว และ ความสง ไปใหดวย เพราะเปนคณสมบตทสเหลยมทเรากำาหนดวาจะตองมคณสมบตน คาทสงไปคอ ความกวาง 20 และความสง = 5
วตถทเราสรางจากแมแบบ Rectangle ตองมชอ เราจะเอาวตถใหมทสรางมาไวในตวแปรวตถขอบเรา ซงจะใชชอวา $myRectangle ดงน
$myRectangle = new Rectangle(20, 5);
ถาตองการเปลยนคาความกวาง กสามารถทำาไดเลย เชน ตองการเปลยนคาความกวางเปน 50 ทำาดงน
$myRectangle -> width = 50;ถาตองการหาพนท กสงไดโดยตรง เพราะรปสเหลยมใหมของเรา คอ myRectangle มวธการหาพนทอยแลว เพราะสรางมาจากแมแบบทเรากำาหนดไวตอนแรก
echo $myRectangle -> area(); // ในทนจะพมพ 250 คอ กวาง 50 และสง = 5เราสามารถสรางสเหลยมและหาพนทไดอกหลาย รป เชน
$rectangle1 = new Rectangle(10,20); $rectangle2 = new Rectangle(30,40); echo $rectangle1 ->area(); // ไดคา '200' echo $rectangle2->perimeter(); // ไดคา '140'
เพอใหเหนภาพรวมทงหมด จะเปลยนไฟลเดมคอ rectangle.php ใหสรางรปสเหลยมจากแมแบบ และใหพมพคาพนท พรอมทงเสนรอบรป ดงน
<?php
require('rect.php');
$w = $_POST["width"];$h = $_POST["height"];
$myRectangle = new Rectangle($w,$h); ?>
<html> <body> <p>ความยาว: <?=$myRectangle->width?><br />
ความสง: <?=$myRectangle->height?></p> <p>พนท: <?=$myRectangle->area()?><br />
เสนรอบรป: <?=$myRectangle->perimeter()?></p> </body> </html>
จะเหนวา OOP ชวยใหเราสามารถใชแมแบบ หรอ CLASS สราง วตถ (objects) ไดอกจำานวนมาก โดยใชโคดของเดม ทำาใหดงายไมยงยาก แตทงหมดนเปนเพยงพนฐานเบองตนเทานน แตเพยงพอทจะเปนพนฐานในการศกษาตอไป
กลบหนาแรก