A sample PHP class is provided below. How the class is used?
When the submit button from the interface (web-page) will be clicked the code in the form-action web-page will call the the create method (static) to create a Ticket data/row in the database table Ticket. The method call will look like Ticket::create(array with field=>value pair, as formed with form submitted data) and you will get an Ticket object created with the inserted row/data.
To update a Ticket, you can create the Ticket object (constructor will do it for you) as you will know the Ticket id at this time. Then use the appropriate set method to update the value in the database.
To search call the static search method with a list/array of field=>value pair.
$dbh (read-only) and $dbhWrite(read-write) are global variables that represent connections to the database.
//represents a customer submitted Ticketclass Ticket{ public $parentTicket; private $dbh_; //member variables, named after the column names of the databse table - Ticket private $id_; private $type; private $subject_; private $timestampCreated_; private $timestampUpdated_; private $status_; private $parentTicketId_; private $categoryId_; private $categoryName_; private $internalNotifylist_; private $externalNotifyList_; private $customerId_; private $customerName_; private $creatorId_; private $creatorName_; private $ownerId_; private $assignedTo_; //constructor, creates a Ticket object with the table row with id = $id public function __construct($id) { global $dbh; $this->dbh_ = $dbh; $this->refreshValues($id); } public function refreshValues($id) { $query="Select Ticket.*, TicketCategory.name from Ticket left join TicketCategory on Ticket.categoryId = TicketCategory.id where Ticket.id=$id"; if ($result=$this->dbh_->query($query)) if($result->num_rows==1) { $row=$result->fetch_object(); $this->id_ = $id; $this->type_=$row->type; $this->subject_ = $row->subject; $this->timestampCreated_ = $row->timestampCreated; $this->timestampUpdated_ = $row->timestampUpdated; $this->status_ = $row->status; $this->parentTicketId_ = $row->parentTicketId; if($this->parentTicketId_>0) $this->parentTicket=$row->parentTicketId; //new Ticket($this->parentTicketId_); else $this->parentTicketId=0; $this->categoryId_ = $row->categoryId; $this->categoryName_ = $row->name; $this->internalNotifyList_ = $row->internalNotifyList; $this->externalNotifyList_ =$row->externalNotifyList; $this->customerId_ = $row->customerId; $this->creatorId_ = $row->creatorId; $this->ownerId_ =$row->ownerId; $this->assignedTo_ =$row->assignedTo; return true; } else return false; } //methods to retrieve/set data/member variables //retrieve id public function getId() { if($this->id_>0) return $this->id_; else return false; } //set id public function setId($id) { if (is_numeric($id)) { if ($this->setField('id',$id)) return true; else return false; } else return false; } public function getCustomerId() { if($this->id_>0) return $this->customerId_; else return false; } public function setCustomerId($customerId) { if (is_numeric($customerId)) { if ($this->setField('customerId',$customerId)) return true; else return false; } else return false; } //used by methods to set member variables private function setField($field, $value) { $dbhWrite=getDbhWrite(); $query="update Ticket set $field='".$dbhWrite->escape_string($value)."' where id=$this->id_"; $result = $dbhWrite->query($query); if ($result==true) { if($dbhWrite->affected_rows==1) { $this->setTimestampUpdated(); $this->refreshValues($this->id_); return true; } else return false; } else return false; } //used to create an entry into the Ticket table (database). //After insertion this row is used to form a Table object and returned to the caller static public function create($fields) { $dbhWrite=getDbhWrite(); $timestamp = time(); $parentTicketId='null'; $customerId='null'; $ownerId='null'; $assignedTo='null'; $categoryId='null'; $type='null'; foreach($fields as $field => $value) { switch($field) { case "type": if(self::isPermittedType($value)) $type=$value; else return false; break; case "status": if(self::isPermittedStatus($value)) $status=$value; else return false; break; case "subject": if (is_string($value)) $subject=$dbhWrite->escape_string($value); else return false; break; case "parentTicketId": if(is_numeric($value)) $parentTicketId=$value; break; case "categoryId": if(is_numeric($value)) $categoryId=$value; break; case "customerId": if(is_numeric($value)) $customerId=$value; else return false; break; case "creatorId": if(is_numeric($value)) $creatorId=$value; else return false; break; case "ownerId": if(is_numeric($value)) $ownerId=$value; break; case "assignedTo": if(is_numeric($value)) $assignedTo=$value; break; } } $insertStr = "insert into Ticket (type,subject, timestampCreated, timestampUpdated, status, parentTicketId, categoryId, customerId, creatorId, ownerId, assignedTo) values ($type, '$subject', $timestamp, $timestamp, $status, $parentTicketId, $categoryId, $customerId, $creatorId,$ownerId,$assignedTo)"; $result = $dbhWrite->query($insertStr); if ($result == true) { if ($dbhWrite->affected_rows==1) { $ticket=new Ticket($dbhWrite->insert_id); return $ticket; } else return false; } else return false; } //searches the entire ticket table based on supplied field=>value pairs static public function searchFields($fields,$orderBy='id') { global $dbh; $query="select * from Ticket where "; foreach($fields as $field => $value) { if($value[0]=="!") //checking for not equal condition { $value=substr($value,1); $query.="`".$dbh->escape_string($field)."`!='".$dbh->escape_string($value)."' and "; } else $query.="`".$dbh->escape_string($field)."`='".$dbh->escape_string($value)."' and "; } $query=substr($query,0,-5); $query.=" order by ".$dbh->escape_string($orderBy); $result = $dbh->query($query); if ($result) { if ($dbh->affected_rows>0) { $tickets = array(); $tickets = Ticket::processResult($result); return $tickets; } else return false; } else return false; } //used by searchFields method. Converts a set of retrieved data rows into array of objects. static private function processResult($result) { if($result->num_rows >= 1) { $tickets=array(); while($row=$result->fetch_object()) { $tickets[] = new Ticket($row->id); } return $tickets; } else return false; }}
From: http://sitestree.com/?p=4808
Categories:16
Tags:
Post Data:2009-10-22 12:58:33
Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a>
In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
<a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>