In this article, we will show you how to create a simple inventory program in PHP that can list barang (items) from a database or a file system. We will use some libraries to make the coding easier and faster.
The inventory program will have the following features:
Display a list of barang with their name, image, price and description.
Allow the user to filter the list by name or price.
Allow the user to add, edit or delete barang from the database or file system.
To create this program, we will need the following tools:
PHP 5.6 or higher and Composer (a free library manager).
A web server such as Apache or Nginx.
A database such as MySQL or MariaDB (optional).
A text editor or IDE such as Visual Studio Code or Sublime Text.
Let's get started!
Step 1: Install the libraries
We will use Composer to install the libraries that we need for this project. Composer is a tool that allows us to manage the dependencies of our PHP projects. It will download and install the libraries for us and keep them updated.
To install Composer, you can follow the instructions on https://getcomposer.org/download/.
Once you have Composer installed, you can create a new folder for your project and open a terminal or command prompt in that folder. Then, you can run the following command to initialize your project:
This command will create a file called composer.json that contains some information about your project. You can edit this file later if you want.
Next, we will install the libraries that we need for this project. We will use three libraries:
eftec/bladeone for the templates. This library will allow us to use the Blade template engine, which is the same one that Laravel uses. It will help us to separate the logic from the presentation of our program.
eftec/daoone for the database (if we want to use one). This library will allow us to connect and interact with a MySQL or MariaDB database using a simple and fluent syntax.
eftec/documentstoreone if we want to use the file system (instead of a database). This library will allow us to store and retrieve data from JSON files using a NoSQL approach.
To install these libraries, you can run the following commands in your terminal:
composer require eftec/bladeone
composer require eftec/daoone
composer require eftec/documentstoreone
These commands will download and install the libraries in a folder called vendor. They will also create a file called composer.lock that contains information about the versions of the libraries that you are using.
Step 2: Create the application file (app.php)
In this step, we will create a file called app.php that will contain the main logic of our program. This file will do the following things:
Include the autoload file from Composer. This file will load all the classes and functions from the libraries that we installed.
Create an instance of the BladeOne class. This class will allow us to render our templates using the Blade syntax.
Create an instance of the DaoOne class (if we are using a database) or the DocumentStoreOne class (if we are using files). These classes will allow us to access and manipulate our data source.
Handle the requests from the user and call the appropriate functions to display or modify the data.