Previously, we learned how to program with PHP and MySQL CRUD tutorial for beginners. This time, we will learn object-oriented programming with PHP & MySQL. This is the second part of the series. Thank you for being a part of this journey!
This post will include the following contents:
1.0 Overview
2.0 Program Output
3.0 Database Table Structure
2.0 Program Output
3.0 Database Table Structure
4.0 Create the Template Files
4.1 Download jQuery, Bootstrap, and Bootbox.js
4.2 Header Code with header.php
4.3 Footer Code with footer.php
4.1 Download jQuery, Bootstrap, and Bootbox.js
4.2 Header Code with header.php
4.3 Footer Code with footer.php
5.0 Creating Record in PHP the OOP Way
5.1 Create a file: create_product.php
5.2 Create a “Read Products” Button
5.3 Get a Database Connection
5.4 Create the Database Configuration Class
5.5 Create a Form in create_product.php
5.6 Loop Through the Categories Records to show as Drop-down
5.7 Create the Object Class for Categories
5.8 Prepare readName() method
5.9 Code when the Form was Submitted
5.10 Create the Object Class for Products
5.1 Create a file: create_product.php
5.2 Create a “Read Products” Button
5.3 Get a Database Connection
5.4 Create the Database Configuration Class
5.5 Create a Form in create_product.php
5.6 Loop Through the Categories Records to show as Drop-down
5.7 Create the Object Class for Categories
5.8 Prepare readName() method
5.9 Code when the Form was Submitted
5.10 Create the Object Class for Products
6.0 Reading and Paging Record in PHP the OOP Way
6.1 Create File: index.php
6.2 Add a “Create Product” button
6.3 Configure Pagination Variables
6.4 Retrieve Records from the Database
6.5 Add readAll() Method in product.php
6.6 Display data from the database
6.7 Put the Read, Edit and Delete Action Buttons
6.8 Create paging.php for Paging Buttons
6.9 Add the countAll() method in product.php
6.10 Include paging.php in index.php
6.1 Create File: index.php
6.2 Add a “Create Product” button
6.3 Configure Pagination Variables
6.4 Retrieve Records from the Database
6.5 Add readAll() Method in product.php
6.6 Display data from the database
6.7 Put the Read, Edit and Delete Action Buttons
6.8 Create paging.php for Paging Buttons
6.9 Add the countAll() method in product.php
6.10 Include paging.php in index.php
7.0 Updating Record in PHP the OOP Way
7.1 Create File: update_product.php
7.2 Create a “Read Products” Button
7.3 Retrieve One Product Information Based on the Given ID.
7.4 Add readOne() method in the Product Object Class.
7.5 Put the Values in the Form.
7.6 Loop Through the Categories Records to show as Drop-down
7.7 Code When Form was Submitted
7.8 Update Code in the Product Class
7.1 Create File: update_product.php
7.2 Create a “Read Products” Button
7.3 Retrieve One Product Information Based on the Given ID.
7.4 Add readOne() method in the Product Object Class.
7.5 Put the Values in the Form.
7.6 Loop Through the Categories Records to show as Drop-down
7.7 Code When Form was Submitted
7.8 Update Code in the Product Class
8.0 Read One Record in PHP the OOP Way
8.1 Create read_one.php file
8.2 Read one record based on given record ID
8.3 Display record on HTML table
8.1 Create read_one.php file
8.2 Read one record based on given record ID
8.3 Display record on HTML table
9.0 Deleting Record in PHP the OOP Way
9.1 Put this JavaScript Code in footer.php
9.2 Create delete_product.php
9.3 Delete Code in Product Class
9.1 Put this JavaScript Code in footer.php
9.2 Create delete_product.php
9.3 Delete Code in Product Class
10.0 Search Records in PHP the OOP Way
10.1 Change index.php
10.2 Create read_template.php
10.3 Create core.php in “config” folder
10.4 Change paging.php code
10.5 Include core.php and read_template.php
10.6 Create search.php
10.7 Add search() and countAll_BySearch() methods
10.8 Output
10.1 Change index.php
10.2 Create read_template.php
10.3 Create core.php in “config” folder
10.4 Change paging.php code
10.5 Include core.php and read_template.php
10.6 Create search.php
10.7 Add search() and countAll_BySearch() methods
10.8 Output
11.0 Download LEVEL 1 Source Code
12.0 Download LEVEL 2 Source Code
13.0 Download LEVEL 3 Source Code
14.0 Download ALL LEVELS in Separate Packages
12.0 Download LEVEL 2 Source Code
13.0 Download LEVEL 3 Source Code
14.0 Download ALL LEVELS in Separate Packages
15.0 What’s Next?
16.0 Related Source Codes
17.0 Some Notes
16.0 Related Source Codes
17.0 Some Notes
1.0 OVERVIEW
Our tutorial for today is about creating a simple database application. We can achieve it with the help of this PHP OOP CRUD tutorial. You can use this knowledge in your current or future projects.
We use Bootstrap so that our application will have a decent UI. If you’re not yet familiar what Bootstrap is, and you want to learn how to use it in few steps, I highly recommend following our Bootstrap tutorial first.
There are so many PHP object oriented programming tutorials on the web today, they have different examples and implementations. Some might be completely correct, some maybe not.
I’m writing this tutorial with a clear goal: to give the best PHP OOP CRUD tutorial for beginners. I welcome your comments and suggestions to help me achieve this.
We want to learn the correct PHP OOP implementation. There are PHP frameworks such as CakePHP, CodeIgniter, and Laravel that correctly do it.
Those things are one step higher. For now, we will learn object oriented programming with PHP & MySQL. Working with a PHP framework should be easy after following this tutorial.
2.0 PROGRAM OUTPUT – PHP OOP CRUD TUTORIAL
We usually have three LEVELS of source code output. But WHY? Because I believe in "Learning Progression" to ensure efficient learning. Learn more
Below are some screenshots of our script’s output. You can click an image to view the larger version of it. Use the left and right arrow to navigate through the screenshots.
Please note that the following images are just output previews. New features might be added already the time you are reading this.
2.1 LEVEL 1 Source Code Output
image 1 2 3
The LEVEL 2 and LEVEL 3 source code outputs proves that you can add and customize more features. It’s easier and faster if you will learn by following our tutorial below.
Downloading our source codes is your huge advantage as well. For now, let’s proceed to the step by step tutorial of our LEVEL 1 source code. Enjoy!
3.0 DATABASE TABLE STRUCTURE
The files products.sql and categories.sql are also included in the code download, located at the sql/ folder.
3.1 Products Table
We use products as objects in this PHP OOP CRUD Tutorial. Database table and dummy data were provided below, you can instantly run this in your PhpMyAdmin after creating your database.
-- Table structure for table `products`
CREATE
TABLE
IF
NOT
EXISTS `products` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`
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
,
PRIMARY
KEY
(`id`)
) ENGINE=MyISAM
DEFAULT
CHARSET=latin1 AUTO_INCREMENT=38 ;
-- 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,
'2014-06-01 01:12:26'
,
'2014-05-31 17:12:26'
),
(2,
'Google Nexus 4'
,
'The most awesome phone of 2013!'
, 299, 2,
'2014-06-01 01:12:26'
,
'2014-05-31 17:12:26'
),
(3,
'Samsung Galaxy S4'
,
'How about no?'
, 600, 3,
'2014-06-01 01:12:26'
,
'2014-05-31 17:12:26'
),
(6,
'Bench Shirt'
,
'The best shirt!'
, 29, 1,
'2014-06-01 01:12:26'
,
'2014-05-31 02:12:21'
),
(7,
'Lenovo Laptop'
,
'My business partner.'
, 399, 2,
'2014-06-01 01:13:45'
,
'2014-05-31 02:13:39'
),
(8,
'Samsung Galaxy Tab 10.1'
,
'Good tablet.'
, 259, 2,
'2014-06-01 01:14:13'
,
'2014-05-31 02:14:08'
),
(9,
'Spalding Watch'
,
'My sports watch.'
, 199, 1,
'2014-06-01 01:18:36'
,
'2014-05-31 02:18:31'
),
(10,
'Sony Smart Watch'
,
'The coolest smart watch!'
, 300, 2,
'2014-06-06 17:10:01'
,
'2014-06-05 18:09:51'
),
(11,
'Huawei Y300'
,
'For testing purposes.'
, 100, 2,
'2014-06-06 17:11:04'
,
'2014-06-05 18:10:54'
),
(12,
'Abercrombie Lake Arnold Shirt'
,
'Perfect as gift!'
, 60, 1,
'2014-06-06 17:12:21'
,
'2014-06-05 18:12:11'
),
(13,
'Abercrombie Allen Brook Shirt'
,
'Cool red shirt!'
, 70, 1,
'2014-06-06 17:12:59'
,
'2014-06-05 18:12:49'
),
(25,
'Abercrombie Allen Anew Shirt'
,
'Awesome new shirt!'
, 999, 1,
'2014-11-22 18:42:13'
,
'2014-11-21 19:42:13'
),
(26,
'Another product'
,
'Awesome product!'
, 555, 2,
'2014-11-22 19:07:34'
,
'2014-11-21 20:07:34'
),
(27,
'Bag'
,
'Awesome bag for you!'
, 999, 1,
'2014-12-04 21:11:36'
,
'2014-12-03 22:11:36'
),
(28,
'Wallet'
,
'You can absolutely use this one!'
, 799, 1,
'2014-12-04 21:12:03'
,
'2014-12-03 22:12:03'
),
(30,
'Wal-mart Shirt'
,
''
, 555, 2,
'2014-12-13 00:52:29'
,
'2014-12-12 01:52:29'
),
(31,
'Amanda Waller Shirt'
,
'New awesome shirt!'
, 333, 1,
'2014-12-13 00:52:54'
,
'2014-12-12 01:52:54'
),
(32,
'Washing Machine Model PTRR'
,
'Some new product.'
, 999, 1,
'2015-01-08 22:44:15'
,
'2015-01-07 23:44:15'
);
3.2 Categories Table
We are going to have “Fashion”, “Electronics” and “Motors” as categories in our example. I got those three category ideas from eBay, haha!
4.0 CREATE THE TEMPLATE FILES
To reduce some mess from our code, we will create these two template files: header.php and footer.php.
I’m actually not sure what these files are called so for now we’ll call them “template files”. It wraps the main content of our web pages. We can imagine it like this:
<?php
include_once
'header.php'
;
?>
// main content of web page must be here!
<?php
include_once
'footer.php'
;
?>
4.1 Download jQuery, Bootstrap, and Bootbox.js
First, please create a “libs” folder. This folder will contain our library files.
Inside the “libs” folder, create a “js” folder. This will contain our JavaScript library files.
Still inside the “libs” folder, create a “css” folder. This will contain our user interface, style or any CSS related files.
jQuery will make Bootstrap and Booxbox work. Download it here and put it inside “/libs/js/” folder. If you don’t know jQuery yes, please learn our tutorial here.
Bootstrap will make our simple app look visually good. Download it here, extract and put it inside “/libs/css/” folder. If you don’t know Bootstrap yet, please learn from our tutorial here.
Bootbox will help us. Download it here and put it inside “/libs/js/” folder.
4.2 Header Code with header.php
This header.php file will be included at the beginning of some of our core files so that we won’t have to write the same header codes every time. Very useful and time saver for this PHP OOP CRUD Tutorial.
<!
DOCTYPE
html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
meta
http-equiv
=
"X-UA-Compatible"
content
=
"IE=edge"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
>
<
title
><?
php
echo $page_title; ?></
title
>
<!-- some custom CSS -->
<
style
>
.left-margin{
margin:0 .5em 0 0;
}
.right-button-margin{
margin: 0 0 1em 0;
overflow: hidden;
}
/* some changes in bootstrap modal */
.modal-body {
padding: 20px 20px 0px 20px !important;
text-align: center !important;
}
.modal-footer{
text-align: center !important;
}
</
style
>
<!-- Bootstrap CSS -->
<
link
href
=
"libs/css/bootstrap/dist/css/bootstrap.css"
rel
=
"stylesheet"
media
=
"screen"
/>
</
head
>
<
body
>
<!-- container -->
<
div
class
=
"container"
>
<?
php
// show page header
echo "<div
class
=
'page-header'
>";
echo "<
h1
>{$page_title}</
h1
>";
echo "</
div
>";
?>
4.3 Footer Code with footer.php
This footer.php file will be included at the end of some of our core files so that we won’t have to write the same footer codes every time.
</
div
>
<!-- /container -->
<!-- jQuery library -->
<
script
src
=
"libs/js/jquery.js"
></
script
>
<!-- bootstrap JavaScript -->
<
script
src
=
"libs/css/bootstrap/dist/js/bootstrap.min.js"
></
script
>
<
script
src
=
"libs/css/bootstrap/docs-assets/js/holder.js"
></
script
>
<!-- bootbox library -->
<
script
src
=
"libs/js/bootbox.min.js"
></
script
>
<!-- HTML5 Shiv and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</
body
>
</
html
>
5.0 CREATING RECORD IN PHP THE OOP WAY
5.1 Create a file: create_product.php
First, create a file with a name “create_product.php” and put the following code inside it.
Many thanks for sharing! PHP CRUD
ReplyDelete