{"id":70009,"date":"2021-08-23T04:10:07","date_gmt":"2021-08-23T08:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/a-sample-php-class-16\/"},"modified":"2021-08-23T04:10:07","modified_gmt":"2021-08-23T08:10:07","slug":"a-sample-php-class-16","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=70009","title":{"rendered":"A Sample PHP Class #16"},"content":{"rendered":"<p>A sample PHP class is provided below. How the class is used? <\/p>\n<p>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=&gt;value pair, as formed with form submitted data) and you will get an Ticket object created with the inserted row\/data.<\/p>\n<p>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. <\/p>\n<p>To search call the static search method with a list\/array of field=&gt;value pair. <\/p>\n<p>$dbh (read-only) and $dbhWrite(read-write) are global variables that represent connections to the database. <\/p>\n<pre>\/\/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-&gt;dbh_ = $dbh;      $this-&gt;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-&gt;dbh_-&gt;query($query))           if($result-&gt;num_rows==1)         {               $row=$result-&gt;fetch_object();                $this-&gt;id_ = $id;                $this-&gt;type_=$row-&gt;type;              $this-&gt;subject_ = $row-&gt;subject;              $this-&gt;timestampCreated_ = $row-&gt;timestampCreated;                $this-&gt;timestampUpdated_ = $row-&gt;timestampUpdated;                $this-&gt;status_ = $row-&gt;status;                $this-&gt;parentTicketId_ = $row-&gt;parentTicketId;                if($this-&gt;parentTicketId_&gt;0)                  $this-&gt;parentTicket=$row-&gt;parentTicketId; \/\/new Ticket($this-&gt;parentTicketId_);                else $this-&gt;parentTicketId=0;                $this-&gt;categoryId_ = $row-&gt;categoryId;                $this-&gt;categoryName_ = $row-&gt;name;                $this-&gt;internalNotifyList_ = $row-&gt;internalNotifyList;                $this-&gt;externalNotifyList_ =$row-&gt;externalNotifyList;             $this-&gt;customerId_ = $row-&gt;customerId;                $this-&gt;creatorId_ = $row-&gt;creatorId;              $this-&gt;ownerId_ =$row-&gt;ownerId;               $this-&gt;assignedTo_ =$row-&gt;assignedTo;             return true;            }           else                return false;   }   \/\/methods to retrieve\/set data\/member variables \/\/retrieve id   public function getId() {       if($this-&gt;id_&gt;0) return $this-&gt;id_;        else return false;  }   \/\/set id    public function setId($id)  {       if (is_numeric($id))        {           if ($this-&gt;setField('id',$id)) return true;          else return false;      }       else return false;  }   public function getCustomerId() {       if($this-&gt;id_&gt;0) return $this-&gt;customerId_;        else return false;  }   public function setCustomerId($customerId)  {       if (is_numeric($customerId))        {           if ($this-&gt;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-&gt;escape_string($value).\"' where id=$this-&gt;id_\";     $result = $dbhWrite-&gt;query($query);      if ($result==true)      {           if($dbhWrite-&gt;affected_rows==1)          {               $this-&gt;setTimestampUpdated();                $this-&gt;refreshValues($this-&gt;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 =&gt; $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-&gt;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-&gt;query($insertStr);      if ($result == true)        {           if ($dbhWrite-&gt;affected_rows==1)         {               $ticket=new Ticket($dbhWrite-&gt;insert_id);                return $ticket;         }           else return false;      }       else return false;  }   \/\/searches the entire ticket table based on supplied field=&gt;value pairs  static public function searchFields($fields,$orderBy='id')  {       global $dbh;        $query=\"select * from Ticket where \";       foreach($fields as $field =&gt; $value)     {           if($value[0]==\"!\") \/\/checking for not equal condition           {               $value=substr($value,1);                $query.=\"`\".$dbh-&gt;escape_string($field).\"`!='\".$dbh-&gt;escape_string($value).\"' and \";          }           else                $query.=\"`\".$dbh-&gt;escape_string($field).\"`='\".$dbh-&gt;escape_string($value).\"' and \";       }       $query=substr($query,0,-5);     $query.=\" order by \".$dbh-&gt;escape_string($orderBy);      $result = $dbh-&gt;query($query);       if ($result)        {           if ($dbh-&gt;affected_rows&gt;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-&gt;num_rows &gt;= 1)        {           $tickets=array();           while($row=$result-&gt;fetch_object())          {               $tickets[] = new Ticket($row-&gt;id);           }           return $tickets;        }       else return false;  }}<\/pre>\n<p>From: http:\/\/sitestree.com\/?p=4808<br \/> Categories:16<br \/>Tags:<br \/> Post Data:2009-10-22 12:58:33<\/p>\n<pre><code>    Shop Online: &lt;a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\"&gt;https:\/\/www.ShopForSoul.com\/&lt;\/a&gt;\n    (Big Data, Cloud, Security, Machine Learning): Courses: &lt;a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"&gt; http:\/\/Training.SitesTree.com&lt;\/a&gt; \n    In Bengali: &lt;a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\"&gt;http:\/\/Bangla.SaLearningSchool.com&lt;\/a&gt;\n    &lt;a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\"&gt;http:\/\/SitesTree.com&lt;\/a&gt;\n    8112223 Canada Inc.\/JustEtc: &lt;a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\"&gt;http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) &lt;\/a&gt;\n    Shop Online: &lt;a href='https:\/\/www.ShopForSoul.com'&gt; https:\/\/www.ShopForSoul.com\/&lt;\/a&gt;\n    Medium: &lt;a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"&gt; https:\/\/medium.com\/@SayedAhmedCanada &lt;\/a&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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=&gt;value &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=70009\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-70009","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":70025,"url":"http:\/\/bangla.sitestree.com\/?p=70025","url_meta":{"origin":70009,"position":0},"title":"OOP concepts in PHP 5 in brief #16","author":"Author-Check- Article-or-Video","date":"August 23, 2021","format":false,"excerpt":"OOP concepts in PHP 5 in short Why this short - note? if you are familiar with OOD and any OOP language such as Java\/C++, this short note will give you enough information to start with PHP 5 OOP Class Class definition starts with the keyword class, followed by a\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16206,"url":"http:\/\/bangla.sitestree.com\/?p=16206","url_meta":{"origin":70009,"position":1},"title":"OOP concepts in PHP 5 in brief","author":"Sayed","date":"September 21, 2019","format":false,"excerpt":"OOP concepts in PHP 5 in brief OOP concepts in PHP 5 in brief OOP concepts in PHP 5 in short Why this short\u200a\u2014\u200anote? if you are familiar with OOD and any OOP language such as Java\/C++, this short note will give you enough information to start with PHP 5\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26828,"url":"http:\/\/bangla.sitestree.com\/?p=26828","url_meta":{"origin":70009,"position":2},"title":"DatabaseUtilities.java: Several general-purpose utilities discussed and used in the chapter. #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"package cwp; import java.sql.*; \/** Three database utilities: * 1) getQueryResults. Connects to a database, executes * a query, retrieves all the rows as arrays * of strings, and puts them inside a DBResults * object. Also places the database product name, * database version, and the names of all\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65914,"url":"http:\/\/bangla.sitestree.com\/?p=65914","url_meta":{"origin":70009,"position":3},"title":"How to upload data from an excel file to database using servlets? #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 17, 2021","format":false,"excerpt":"Use third party API bundles like jexcelApi. http:\/\/jexcelapi.sourceforge.net\/.---create an XML file to define the structure of your excel file like how many columns, column names, corresponding database table\/class column\/variable name, Create a Java class that can retrieve information from that xml file. You can use XPATH and DOM for the\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65652,"url":"http:\/\/bangla.sitestree.com\/?p=65652","url_meta":{"origin":70009,"position":4},"title":"Database Programming in .Net: ADO.Net Overview #Misc .Net","author":"Author-Check- Article-or-Video","date":"July 10, 2021","format":false,"excerpt":"Overview of ADO.net Purpose Components of ADO.net and their functions ADO.net provides disconnected database access to make minimum resource (RAM,Page Table,Heap) use. Database connections are open as long as the connection is required. Afterwards, the connection is just closed. If connections are kept active and if new connections are used\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69922,"url":"http:\/\/bangla.sitestree.com\/?p=69922","url_meta":{"origin":70009,"position":5},"title":"Database Programming in .Net: ADO.Net Overview #18","author":"Author-Check- Article-or-Video","date":"August 22, 2021","format":false,"excerpt":"Overview of ADO.net Purpose Components of ADO.net and their functions ADO.net provides disconnected database access to make minimum resource (RAM,Page Table,Heap) use. Database connections are open as long as the connection is required. Afterwards, the connection is just closed. If connections are kept active and if new connections are used\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/70009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70009"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/70009\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70009"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}