Php oop product management product class
Objective
Create product class to access the product table, and we use the singleton class for database connection.
product.php
<?php require_once 'Singleton.php'; class product { public $id, $pname, $price, $seller; protected $connect; protected $sql; public function __construct() { $this->connect = Singleton::getconnect(); } public function __destruct() { $this->connect = null; } public function insert_db() { $this->sql = 'INSERT INTO product (id, pname, price, seller) VALUES(NULL,"' . addslashes($this->pname) . '",' . $this->price . ',"' . addslashes($this->seller) . '")'; $return_id = $this->connect->mysql_execute($this->sql); if (!empty($return_id)) { return $return_id; } else { return false; } } public function update_db() { if (!empty($this->id)) { $this->sql = 'UPDATE product SET pname="' . addslashes($this->pname) . '",price=' . $this->price . ',seller="' . addslashes($this->seller) . '" WHERE id=' . $this->id; $aff_rows = $this->connect->mysql_execute($this->sql); if (!empty($aff_rows)) { return $aff_rows; } else { return false; } } else { return false; } } public function delete_db() { $this->sql = 'DELETE FROM product WHERE id=' . $this->id; $aff_rows = $this->connect->mysql_execute($this->sql); if (!empty($aff_rows)) { return $aff_rows; } else { return false; } } public function get_products() { $this->sql = 'SELECT * FROM product'; $return_arr = $this->connect->mysql_execute($this->sql); if (!empty($return_arr)) { return $return_arr; } else { return false; } } public function get_products_page($start, $total) { $this->sql = "SELECT * FROM product LIMIT {$start}, {$total}"; $return_arr = $this->connect->mysql_execute($this->sql); if (!empty($return_arr)) { return $return_arr; } else { return false; } } public function count_product() { $this->sql = 'SELECT COUNT(*) as total FROM product'; $return_arr = $this->connect->mysql_execute($this->sql); if (!empty($return_arr)) { return $return_arr[0]['total']; } else { return false; } } public function get_product_by_id() { $this->sql = 'SELECT * FROM product WHERE id=' . $this->id; $return_arr = $this->connect->mysql_execute($this->sql); if (!empty($return_arr)) { return $return_arr; } else { return false; } } } ?>
_construct method is to initialize the connection
We create the class constructor to initialize the private class's property $connect and assign the singleton class object to it.
_destruct method is to destroy the already initialize connection
Use the destructor class to delete the singleton class object and release the memory back to the CPU.
update_db method is to update a product
The update method requires the class properties (id, pname, price, and seller) to initially assign before it can make the update. Update the table is based on the product id.
delete_db method is used to delete product
The delete_db method is used to delete a product based on the product id.
get_products() method is to get all product in the table
The get_products() method gets all products.
get_products_page($start, $total): get product for pagination
We use the get_products_page($start, $total) for our the pagination. Please check MySQL LIMIT to get detail.
count_product() method gets all product
The count_product() returns all product from the product table.
get_product_by_id() method gets product by the product id
we retrieve product based on the product id and this is for single product only.