Creating a Simple Business Directory using CodeIgniter and MySQL
A walkthrough of how to build a simple web directory using CodeIgniter and MySQL. The code is available in this github repo. The Coronavirus has spread throughout the world forcing millions of people to stay at home and that includes me. The first few days are fine but then it gets boring. To tackle this infinite level of boredom, I decided to learn something new, something that I have never used but relatively popular and easy to grasp so I won’t get bored learning it. And that is when I met CodeIgniter. I just googled for a while looking for an easy backend framework to learn and CodeIgniter was everywhere. It is a lightweight MVC framework and very easy to get started with. I have heard of CodeIgniter but never used it. In fact the last time I used PHP was back in my college around 5 years ago and I have no professional experience with PHP. Installation I am assuming you have PHP, MySQL and Apache installed. The easiest way to install all of these is to get XAMPP which comes packaged with Apache, MySQL and PHP. Or you can try installing each component separately. Setting up CodeIgniter is fairly simple, go to their download page, get the latest version (3.1.11 in my case) and then follow the installation instructions. Basically all you have to do is unzip the folder into your server’s root which is htdocs for XAMPP or any custom folder if you have installed apache separately. Rename the folder to whatever name you prefer to give, in my case it’s bizdir. Don’t get overwhelmed by the lot of files and folders, we will get to them. To verify everything is working, go to http://localhost/bizdir/ and you will see a welcome message like this Database Setup Create a database bizdir: Within that database create a table directory: create table if not exists directory ( id int auto_increment primary key, business_name varchar(255) not null, category varchar(255) not null, address varchar(255) not null, phone varchar(255), website varchar(255), email varchar(255), image varchar(255) ); Go to application/config/database.php and set up proper configuration for your database by changing the value in $db[‘default’] array. You will need to add username, password and database. $db[‘default’] = array( ‘dsn’ =”, ‘hostname’ =’localhost’, ‘username’ =’php_noob’, // ‘easy_password’, // ‘bizdir’, // TRUE ); Here comes the code! As I have already mentioned, CodeIgniter is a MVC framework. So we will have models, views and controllers. In simple terms, models are related to the database, view is the HTML part that you see in the browser and controller is the logic that connects your models with views. The business logic of an application lies in the controller. Model Let’s start by defining our models. Create a file Bizdir_model.php inside application/models and add the following code. : A complete business directory application/views/templates/footer.php © 2020 Bizdir. All rights reserved. application/views/bizdir/index.php Business Name Category Address Phone Website There is not much going on in header and footer. In the header.php file, we are just printing the title passed from the controller. Inside business/index.php, we are looping through the list of businesses that are in our database. One each iteration, a new is created and all the values are printed inside . » Read More
Like to keep reading?
This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.