Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ต่อยอด OOP php ให้ทีครับ กำลังหัดเขียน งงมากครับ ช่วยทีๆ



 

ต่อยอด OOP php ให้ทีครับ กำลังหัดเขียน งงมากครับ ช่วยทีๆ

 



Topic : 048996



โพสกระทู้ ( 160 )
บทความ ( 0 )



สถานะออฟไลน์




ผมก็กำลังหัดเขียนตามเลยครับ จาก http://www.scribd.com/doc/2887367/OOP-in-PHP-part3
แต่ก็งงๆ พยายามนั่งดูไปเรื่อยๆ ทำตามก็ได้บ้างไม่ได้บ้าง สุดท้าย copy มาเลย มันก็ได้
แต่พอจะลองมาหัดเขียน insert delete เอง ไหงมันยากจังหว่า
ผมก็เลยกลับมาที่นี่อีกรอบ อยากให้ช่วยแนะนำการสร้าง class ในการจัดการข้อมูลด้วยครับ เช่น เพิ่ม ลบ แก้ไข

ใครที่อ่านแล้วช่วยนิดนะครับ เอาเป้นแนวทางก็ได้ ยังดี ให้ผมมองภาพออก
อันนี้ของเขาที่ผมหยิบมาใช้
Code (PHP)
<?PHP
	class mysql_config{
		var $dbconfig= array(
							"hostname" => NULL,
							"username" => NULL,
							"password" => NULL,
							"database" => NULL,						
							"collation_connection" => NULL,
							"character_set" => NULL
							);
		function mysql_config(){
			$GLOBALS['global_mysql_querydb_timer'] = 0;
			$GLOBALS['global_mysql_querydb_counter'] = 0;
		}
		function get_configdb(){
			return $this->dbconfig;
		}
		function set_querydb_counter($value){
			global $static_querydb_count;
			
			if(isset($static_querydb_count)){
					$static_querydb_count= $value;
			}else{
				$GLOBALS['global_mysql_querydb_counter']= $value;
			}			
		}
		function get_querydb_counter(){
			global $static_querydb_count;
			
			if(isset($static_querydb_count)){
				return $static_querydb_count;				
			}else{
				return $GLOBALS['global_mysql_querydb_counter'];
			}
		}		
		function set_querydb_timer($value){
			global $static_query_timer;
			
			if(isset($static_query_timer)){
				$static_query_timer=$value;
			}else{
				 $GLOBALS['global_mysql_querydb_timer']=$value;
			}
		}
		function get_querydb_timer(){
			global $static_querydb_timer;
			
			if(isset($static_querydb_timer)){
				return $static_querydb_timer;
			}else{
				return $GLOBALS['global_mysql_querydb_timer'];
			}
		}
		function set_character_set($value){
			$this->dbconfig['character_set']= $value;
		}
		function set_collation_connection($value){
			$this->dbconfig['collaction_connection']= $value;
		}
		function set_database($value){
			$this->dbconfig['database']= $value;
		}
		function set_hostname(){
			$this->dbconfig['hostname']= $value;
		}
		function set_username($value){
			$this->dbconfig['username']= $value;
		}
		function set_password($value){
			$this->dbconfig['password']= $value;
		}
		
		
	}
	class mysql_operator extends mysql_config{
		var $link = NULL;
		var $result= NULL;
		var $mysql_version = NULL;
		var $string_error = array();
		
		function set_configdb(&$array_dbconfig){
			$this->set_hostname($array_dbconfig['hostname']);
			$this->set_username($array_dbconfig['username']);
			$this->set_password($array_dbconfig['password']);
			$this->set_database($array_dbconfig['database']);
			$this->set_collation_connection(
										   $array_dbconfig['collation_connection']
										   );
			$this->set_character_set($array_dbconfig['character_set']);
						
		}
		function set_error_msg($string){
			array_push($this->string_error, $string);
}
		function get_error_msg(){
			while(count($this->string_error)-1)
				$string_output='<b>MySql Error:</b>'.
								array_pop($this->string_error).'<br/>';
				return $string_output;
		}
		function mysql_operator(&$dbconfig){
			if(is_array($dbconfig))$this->set_configdb($dbconfig);
			else $this->set_configdb($dbconfig->get_configdb());	
			
		}
		function set_mysql_version($value){
			$this->mysql_version=$value;			
		}
		function get_mysql_version(){
			return $this->mysql_version;
			
		}
		function opendb($newlink=FALSE){
			$this->link=mysql_connect(
									  $this->dbconfig['hostname'],
									  $this->dbconfig['username'],
									  $this->dbconfig['password'],
									  $newlink);
			if(!$this->link)
				$this->set_error_msg(
									 mysql_errno($this->link).':'.
									 mysql_error($this->link)
									 );				
				$this->set_mysql_version(mysql_get_server_info());
						
			if(!mysql_select_db($this->dbconfig['database']))
				$this->set_error_msg(
									 mysql_errno($this->link).':'.
									 mysql_error($this->link)
									 );
			if(!empty($this->dbconfig['character_set'])){
				$this->querydb("SET CHARACTER SET".
							   $this->dbconfig['character_set'].";"
							   );
				$this->querydb_counter_decrement();
			}
			if(!empty($this->dbconfig['collation_connection'])){
				$this->querydb("SET collation_connection=".
							   $this->dbconfig['collation_connecttion'].";");
				$this->querydb_counter_decrement();
			}
		}
		function closedb(){
			if(!mysql_close($this->link))
				$this->set_error_msg(
									mysql_errno($this->link).':'.
									mysql_error($this->link)
									);
		}
		function querydb($string_query){
			$start_time=array_sum(explode(chr(32),microtime()));
			$this->result=mysql_query($string_query);
			$end_time= array_sum(explode(chr(32),microtime()));
			
			if(!$this->result)
				$this->set_error_msg(
									 mysql_errno($this->link).':'.
									 mysql_error($this->link)
									 );
			$this->set_querydb_timer(
									 $this->get_querydb_timer()+($end_time-$start_time)
									 );
			$this->querydb_counter_increment();
			
		}
		function list_fields($table){
			$fields=mysql_list_fields(
									  $this->dbconfig['database'],$table,$this->link
									  );
			$columns = mysql_num_fields($fields);
			for($i=0;$i<$columns;$i++)
				$array_fields_in_table[$i]=mysql_fields_name($fields,$i);
				return $array_fields_in_table;
			
		}
		function querydb_counter_increment(){
			$this->set_querydb_counter($this->get_querydb_counter()+1);
		}
		function querydb_counter_decrement(){
			$this->set_querydb_counter($this->get_querydb_counter()-1);
		}
		function querydb_transac_begin(){
			if($this->get_mysql_version()>3)
				$this->querydb("START TRANSACTION;");
			
		}
		function querydb_transac_commit(){
			if($this->get_mysql_version()>3)
				$this->querydb("COMMIT;");
		}
		function querydb_transac_rollback(){
			if($this->get_mysql_version()>3)
				$this->querydb("ROLLBACK;");
		}
		function querydb_transac_set_autocommit_to_zero(){
			if($this->get_mysql_version()>3)
				$this->querydb("SET AUTOCOMMIT=0;");
		}
		function querydb_transac_check_for_rollback_or_commit(){
			if(!($this->get_resultdb()))
				$this->querydb_transac_rollback();
			else
				$this->querydb_transac_commit();						
		}
		function num_rows_in_table($table,$condition=NULL){
			$num_rows=new mysql_operator($this->get_configdb());
			$num_rows->opendb();
			$num_rows->querydb("SELECT COUNT(*) FROM $table $condition;");
			$num_rows->closdb();
			return mysql_result($num_rows->get_resultdb(),0);
						
		}
		function has_data_in_table($table,$field,$value){
			$value= trim($value);
			
			if(empty($field) or empty($value))
				$sql_condtion=NULL;
			else
				$sql_condtion="WHERE ".$field."='".$value."'";					
			
			
			if($this->num_rows_in_table($table,$sql_condition)>0){
				return TRUE;
			}
			else{
				return FALSE;					
			}
			
		}
		function reset_querydb(){
			$this->set_resultdb(NULL);
			
		}
		function set_resultdb($resource){
			$this->result=$resource;
			
		}
		
		function get_resultdb(){
			return $this->result;
		}
		
		function get_resultdb_fetch_data(){
			return mysql_fetch_array($this->get_resultdb());
						
		}
		
		function get_resultdb_num_rows(){
			return mysql_num_rows($this->get_resultdb());
		}
		
		function get_resultdb_num_fields(){
			return mysql_num_fields($this->get_resultdb());
		}
		
		function get_last_insert_id(){
			return mysql_insert_id();	
		}
				
		function get_resultdb_atrow($rows=0){
			return mysql_result($this->get->resultdb(),$row);
			
		}
		
	}

//แทรกของผมเอง ในส่วนนี้ 
?>


และ "//แทรกของผมเอง ในส่วนนี้ "
Code (PHP)
class database_operator extends mysql_operator{
	
	function set_insert_data_to_database($table,$field,$condition){
			$obj_insert_data_to_database=new mysql_operator($this->get_configdb());
			$obj_insert_data_to_database->opendb();
			$obj_insert_data_to_database->querydb("INSERT INTO $table($field) VALUES($condition);");
			$obj_insert_data_to_database->closdb();
			return $obj_insert_data_to_database;
		
	}
	
	
}




หน้านี้ที่ผมเขียน รับค่า และเรียก obj เพื่อ create
Code (PHP)
<?PHP
include("../../Models/dbconfig.php");
include("../../Models/Student_Models.php");
	$title=$_POST["title"];
	$firstName=$_POST["firstName"];
	$lastName=$_POST["lastName"];
	$nickName=$_POST["nickName"];
	$tel_1=$_POST["tel_1"];
	$tel_2=$_POST["tel_2"];
	$mobile_1=$_POST["mobile_1"];
	$mobile_2=$_POST["mobile_2"];
	$email=$_POST["email"];
	$address=$_POST["address"];
	$city=$_POST["city"];
	$state=$_POST["state"];
	$country=$_POST["country"];
	$zipcode=$_POST["zipcode"];
	

	$obj_create_student= new database_operator();
	$obj_create_student->set_insert_data_to_database("students","Title_ID,First_Name,Last_Name,Nick_Name,Sex,Address,City,State,Country_ID,Zipcode,Tel_1,Tel_2,Mobile_1,Mobile_2","$title,'$firstName','$lastName','$nickName','$tel_1','$tel_2','$mobile_1','$mobile_2','$email','$address','$city','$state',$country,'$zipcode'");
	
	
	if ($obj_create_student){
		echo "Create success ";
	}else {
		echo "Create faild";
	}
	

?>




Tag : PHP









ประวัติการแก้ไข
2010-09-19 13:50:28
2010-09-19 14:42:47
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-09-19 13:49:38 By : chon2008 View : 1239 Reply : 2
 

 

No. 1



โพสกระทู้ ( 21 )
บทความ ( 0 )



สถานะออฟไลน์


class database_operator extends mysql_operator{

function set_insert_data_to_database($table,$field,$condition){
$obj_insert_data_to_database=new mysql_operator($this->get_configdb()); //ตรงนี้ เรากำลังสร้างคลาสส ไม่ใช่สร้างอินสแตนต์ไม่น่าจะใช้ new ได้ นะครับ เมื่อเป็น คลาสที่ขยายจาก mysql_operator และฟังชัน mysql_operator เมธอดของคลาสแม่ เมื่อจะเรียกใช้ฟังก์ชันของแม่ น่าจะเรียก parent::mysql_operator แทน
$obj_insert_data_to_database->opendb(); // เมื่อจะเรียกใช้ฟังก์ชันของแม่ น่าจะเรียก parent::opendb เป็นต้น
$obj_insert_data_to_database->querydb("INSERT INTO $table($field) VALUES($condition);");
$obj_insert_data_to_database->closdb();
return $obj_insert_data_to_database;

}

}

ลองอ่านเรื่องการ ส้ราง extend class ดูครับ
ผมเองก็ยังใหม่หัดเขียนอยู่ครับ อย่าเชื่อมากนัก ผิดพลาดขออภัย ด้วย
ลองนี่ดูครับ
https://www.thaicreate.com/php/forum/027334.html






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-09-19 20:43:07 By : สกล
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Go to : กระทู้รณรงค์ ตอน andquot;มาเขียน php แบบ OOP กันเถอะandquot;

Go to : สอนการเขียน PHP กับ OOP (Object Oriented Programming) ผ่าน VDO ออนไลน์
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-09-19 21:18:40 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ต่อยอด OOP php ให้ทีครับ กำลังหัดเขียน งงมากครับ ช่วยทีๆ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่