ใน workshop จะประกอบไปด้วยฟอร์มเพิ่ม ลบ แก้ไข เรียกข้อมูลมาแสดงได้ ซึ่งน่าจะเป็นประโยชน์สำหรับผู้ที่กำลังศึกษาการเขียนโปรแกรม php OOP
ฐานข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
CREATE DATABASE IF NOT EXISTS `myshop` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `myshop`; -- -------------------------------------------------------- -- -- Table structure for table `categories` -- CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL, `name` varchar(256) NOT NULL, `created` datetime NOT NULL, `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- -- Dumping data for table `categories` -- INSERT INTO `categories` (`id`, `name`, `created`, `modified`) VALUES (1, 'Fashion', '2014-06-01 00:35:07', '2014-05-31 02:34:33'), (2, 'Electronics', '2014-06-01 00:35:07', '2014-05-31 02:34:33'), (3, 'Motors', '2014-06-01 00:35:07', '2014-05-31 02:34:54'); -- -------------------------------------------------------- -- -- Table structure for table `products` -- CREATE TABLE IF NOT EXISTS `products` ( `id` int(11) NOT NULL, `name` varchar(32) NOT NULL, `description` text NOT NULL, `price` int(11) NOT NULL, `category_id` int(11) NOT NULL, `created` datetime NOT NULL, `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8; -- -- Dumping data for table `products` -- INSERT INTO `products` (`id`, `name`, `description`, `price`, `category_id`, `created`, `modified`) VALUES (1, 'LG P880 4X HD', 'My first awesome phone!', 336, 3, '0000-00-00 00:00:00', '0000-00-00 00:00:00'), (2, 'Google Nexus 4', 'The most awesome phone of 2013!', 299, 2, '0000-00-00 00:00:00', '0000-00-00 00:00:00'), (3, 'Samsung Galaxy S4', 'How about no?', 600, 3, '0000-00-00 00:00:00', '0000-00-00 00:00:00'), (6, 'Bench Shirt', 'The best shirt!', 29, 1, '2014-06-01 01:12:26', '2014-05-31 03:12:21'), (7, 'Lenovo Laptop', 'My business partner.', 399, 2, '2014-06-01 01:13:45', '2014-05-31 03:13:39'), (8, 'Samsung Galaxy Tab 10.1', 'Good tablet.', 259, 2, '2014-06-01 01:14:13', '2014-05-31 03:14:08'), (9, 'Spalding Watch', 'My sports watch.', 199, 1, '2014-06-01 01:18:36', '2014-05-31 03:18:31'), (10, 'Sony Smart Watch', 'The coolest smart watch!', 300, 2, '2014-06-06 17:10:01', '2014-06-05 19:09:51'), (11, 'Huawei Y300', 'For testing purposes.', 100, 2, '2014-06-06 17:11:04', '2014-06-05 19:10:54'), (12, 'Abercrombie Lake Arnold Shirt', 'Perfect as gift!', 60, 1, '2014-06-06 17:12:21', '2014-06-05 19:12:11'), (13, 'Abercrombie Allen Brook Shirt', 'Cool red shirt!', 70, 1, '2014-06-06 17:12:59', '2014-06-05 19:12:49'); |
เชื่อมต่อ database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php class Database{ // specify your own database credentials private $host = "localhost"; private $db_name = "myshop"; private $username = "root"; private $password = "123"; public $conn; // get the database connection public function getConnection(){ $this->conn = new mysqli ($this->host, $this->username, $this->password, $this->db_name); if($this->conn){ $this->conn->set_charset("utf8"); } return $this->conn; } } ?> |
คลาส product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?php class product{ private $conn; // object properties public $id; public $name; public $price; public $description; public $category_id; public $timestamp; public function __construct($db){ $this->conn = $db; } function readAll(){ $query = "SELECT id, name, description, price, category_id FROM products ORDER BY name ASC"; $result = $this->conn->query($query); return $result; } function readOne(){ $query = "SELECT name, price, description, category_id FROM products WHERE id = " . $this->id ; $result = $this->conn->query($query); $row = $result->fetch_array(); $this->name = $row['name']; $this->price = $row['price']; $this->description = $row['description']; $this->category_id = $row['category_id']; } // create products function create(){ // to get time-stamp for 'created' field $this->getTimestamp(); //write query $query = "INSERT INTO products(name, price, description, category_id, created) VALUES ('" . $this->name. "', '" . $this->price. "', '" . $this->description. "', '" . $this->category_id. "', '" . $this->timestamp. "' )"; if($this->conn->query($query)){ return true; }else{ return false; } } // used for the 'created' field when creating a products function getTimestamp(){ date_default_timezone_set('Asia/Bangkok'); $this->timestamp = date('Y-m-d H:i:s'); } function update(){ $query = "UPDATE products SET name = '" . $this->name . "', price = '" . $this->price . "', description = '" . $this->description . "', category_id = '" . $this->category_id . "' WHERE id = '" . $this->id . "'"; if($this->conn->query($query)){ return true; }else{ return false; } } // delete the products function delete(){ $query = "DELETE FROM products WHERE id = '" . $this->id . "'"; if($this->conn->query($query)){ return true; }else{ return false; } } } ?> |
คลาส Category
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php class Category{ private $conn; // object properties public $id; public $name; function __construct($db){ $this->conn = $db; } function readName(){ $query = "SELECT name FROM categories WHERE id = " . $this->id ; $result = $this->conn->query($query); $row = $result->fetch_array(); $this->name = $row['name']; } function readAll(){ $query = "SELECT id, name FROM categories ORDER BY name ASC"; $result = $this->conn->query($query); return $result; } } ?> |
หน้าแรก
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $page_title = "Read Products"; include_once "header.php"; ?> <?php include_once 'config/database.php'; include_once 'product.php'; include_once 'category.php'; // instantiate database and product object $database = new Database(); $db = $database->getConnection(); //query product $product = new Product($db); $result = $product->readAll(); $num = $result->num_rows; // display the products if there are any if($num>0){ $cate = new Category($db); echo"<h3 align='center'><a href='create_product.php'> ADD </a> </h3>"; echo "<table class='table table-hover table-responsive table-bordered'>"; echo "<tr><th>Product</th><th>Price</th><th>Description</th> <th>Category</th><th>Actions</th></tr>"; while($row = $result->fetch_array()) { extract($row); echo "<tr>"; echo "<td>{$name}</td><td>{$price}</td><td>{$description}</td>"; echo "<td>"; $cate->id = $category_id; $cate->readName(); echo $cate->name; echo "</td>"; echo "<td>"; echo "<a href='update_product.php?id={$id}'>Edit</a> / "; echo "<a href='delete_product.php?id={$id}' onclick='del({$id})'>del</a>"; echo "</td>"; echo "</tr>"; } echo "</table>"; } else{ echo "<div>No products found.</div>"; } include_once "footer.php"; ?> |
ฟอร์มเพิ่มข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php // set page headers $page_title = "Create Product"; include_once "header.php"; echo "<div class='right-button-margin'>"; echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>"; echo "</div>"; // get database connection include_once 'config/database.php'; $database = new Database(); $db = $database->getConnection(); // if the form was submitted if($_POST){ // instantiate product object include_once 'objects/product.php'; $product = new Product($db); // set product property values $product->name = $_POST['name']; $product->price = $_POST['price']; $product->description = $_POST['description']; $product->category_id = $_POST['category_id']; // create the product if($product->create()){ echo "<div class=\"alert alert-success alert-dismissable\">"; echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>"; echo "Product was created."; echo "</div>"; } // if unable to create the product, tell the user else{ echo "<div class=\"alert alert-danger alert-dismissable\">"; echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>"; echo "Unable to create product."; echo "</div>"; } } ?> <!-- HTML form for creating a product --> <form action='create_product.php' method='post'> <table class='table table-hover table-responsive table-bordered'> <tr> <td>Name</td> <td><input type='text' name='name' class='form-control' required></td> </tr> <tr> <td>Price</td> <td><input type='text' name='price' class='form-control' required></td> </tr> <tr> <td>Description</td> <td><textarea name='description' class='form-control'></textarea></td> </tr> <tr> <td>Category</td> <td> <!-- categories from database will be here --> <?php // read the product categories from the database include_once 'objects/category.php'; $category = new Category($db); $result = $category->readAll(); // put them in a select drop-down echo "<select class='form-control' name='category_id'>"; echo "<option>Select category...</option>"; while($row_category = $result->fetch_array()) { echo "<option value='{$row_category[id]}'>{$row_category[name]}</option>"; } echo "</select>"; ?> </td> </tr> <tr> <td></td> <td> <button type="submit" class="btn btn-primary">Create</button> </td> </tr> </table> </form> <?php include_once "footer.php"; ?> |
ฟอร์มแก้ไขข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
<?php $page_title = "Update Product"; include_once "header.php"; echo "<div class='right-button-margin'>"; echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>"; echo "</div>"; // get ID of the product to be edited $id = isset($_GET['id']) ? $_GET['id'] : die('ERROR: missing ID.'); // include database and object files include_once 'config/database.php'; include_once 'objects/product.php'; // get database connection $database = new Database(); $db = $database->getConnection(); // prepare product object $product = new Product($db); // set ID property of product to be edited $product->id = $id; // read the details of product to be edited $product->readOne(); ?> <?php // if the form was submitted if($_POST){ // set product property values $product->name = $_POST['name']; $product->price = $_POST['price']; $product->description = $_POST['description']; $product->category_id = $_POST['category_id']; // update the product if($product->update()){ echo "<div class=\"alert alert-success alert-dismissable\">"; echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>"; echo "<script>"; echo "window.location = 'index.php' "; echo "</script>"; echo "</div>"; } // if unable to update the product, tell the user else{ echo "<div class=\"alert alert-danger alert-dismissable\">"; echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>"; echo "Unable to update product."; echo "</div>"; } } ?> <form action='update_product.php?id=<?php echo $id; ?>' method='post'> <table class='table table-hover table-responsive table-bordered'> <tr> <td>Name</td> <td><input type='text' name='name' value='<?php echo $product->name; ?>' class='form-control' required></td> </tr> <tr> <td>Price</td> <td><input type='text' name='price' value='<?php echo $product->price; ?>' class='form-control' required></td> </tr> <tr> <td>Description</td> <td><textarea name='description' class='form-control'><?php echo $product->description; ?></textarea></td> </tr> <tr> <td>Category</td> <td> <!-- categories select drop-down will be here --> <?php // read the product categories from the database include_once 'objects/category.php'; $category = new Category($db); $result = $category->readAll(); // put them in a select drop-down echo "<select class='form-control' name='category_id'>"; echo "<option>Please select...</option>"; while($row_category = $result->fetch_array()) { if($product->category_id==$row_category[id]){ echo "<option value='$row_category[id]' selected>"; }else{ echo "<option value='$row_category[id]'>"; } echo "$row_category[name]</option>"; } echo "</select>"; ?> </td> </tr> <tr> <td></td> <td> <button type="submit" class="btn btn-primary">Update</button> </td> </tr> </table> </form> |
ไฟล์ลบข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php // include database and object file include_once 'database.php'; include_once 'product.php'; // get database connection $database = new Database(); $db = $database->getConnection(); $product = new Product($db); // set product id to be deleted $product->id = $_GET['id']; // delete the product if($product->delete()){ $msg = "Object was deleted."; } else{ $msg = "Unable to delete object."; } ?> <script> alert("<?php echo $msg ?>"); window.location = "index.php"; </script> |
ตัวอย่างหน้าโปรแกรม