Develop a Job portal with CodeIgniter

Today we’re developing a simple job portal using CodeIgniter framework.There will be two pages – a list of available jobs, and a detailed summary page for each job. We have a database named jobs_ms which contain two tables named ‘jobs‘ and ‘job_details‘. Jobs will be pre-populated in ‘jobs‘ table and related job details are pre-stored into job_details table. Lets start working.

Prepare setup

I think you already have Apache-powered PHP+MySQL server. First, download CodeIgniter. Extract it into your web directory and rename it Jobs.

Open up system/application/config.php in your CodeIgniter folder. You’ll need to set $config['base_url'] to $config[‘base_url’]= “http://127.0.0.1/Jobs/”;. Fire up your web browser and point it at the location of your CI installation http://localhost/Jobs/ you should see a welcome page with a few basic instructions.

Now we have to configure database access. open up system/application/database.php and scroll to the bottom. You probably only need to edit these entries:

system/application/config/database.php

$db[‘default’][‘hostname’] = “localhost”;
$db[‘default’][‘username’] = “root”;
$db[‘default’][‘password’] = “”;
$db[‘default’][‘database’] = “
jobs_ms“;

...

CodeIgniter helps you quickly move code between servers with different database configuration details. Make sure the database you specify exists.

Application design

For our job portal, we’ll have one controller, one model and two views. The controller will handle both the main job list and the job summary pages, with a model method and view for each. In this way, we’ll clearly seperate the different parts of our application.

The controller

A controller is basically a class with a few methods that handles incoming requests and directs them accordingly. The class extends CodeIgniter’s Controller class, and in doing so gains access to all the functionality of the framework. In our controller, we’ll load the various resources we’re going to need – specifically, a model and some views. Here’s the code for the controller:

system/application/controllers/jobs.php

<?php
class Jobs extends Controller {
  function index() {
    // This gives us redirect()
    $this->load->helper('url');
    $this->list_jobs(); // by default show the list   jobs
  }

  function list_jobs() {
    $this->load->database();
    $this->load->model('jobs_model','', TRUE);
    $data['jobs'] = $this->jobs_model->get_jobs();

    // This gives us anchor() - see the view at the end
    $this->load->helper('url');
    $this->load->view('jobs_view', $data);
  }

  function view_job($id = false) {
    $this->load->helper('url');
    if (!$id) redirect('jobs/list_jobs');

    $this->load->database();
    $this->load->model('jobs_model','', TRUE);
    $data['jobs'] = $this->jobs_model->getJobDetails($id);

    $this->load->view('job_view', $data);
  }
}
?>

The controller is simple, merely taking input, fetching data from the model and sending it to the view.

The model

Similar to controllers, models extend the Model class. Once the database is initialised, through a call to $this->load->database(), they have access to the universal database class of CodeIgniter. Our model will have three methods, one to fetch all jobs in the database – get_jobs() – and one to fetch details for a particular job – get_job($job_id) and another fetch job details from job_details table. The CodeIgniter manual has an excellent quick start guide for the database library that you should read through before proceeding. Here’s the code for our model:

system/application/models/jobs_model.php

<?php
class Jobs_model extends Model {
  function get_jobs() {
    $query = $this->db->get('jobs');
    foreach ($query->result_array() as $row)
    {
      $result[] = $row;
    }
    return $result;
  }

  function get_job($job_id) {
    $query = $this->db->where('id', $job_id)->get('jobs');
    $result = $query->row_array();
    return $result;
  }

  function getJobDetails($job_id){
	$sql='SELECT  DISTINCT jobs.id, jobs.title,
jobs.company, job_details.vacancy_no, 
job_details.description, job_details.educational_req, 
job_details.experience_req,job_details.additional_job_req,
 job_details.salary_rng, 
job_details.other_ben, job_details.location FROM jobs,
 job_details WHERE job_details.id = jobs.id';

	$sql .= " AND jobs.id=" .$job_id;
	$query = $this->db->query($sql);

    	return $query;
	}
}
?>

Let’s take a moment to examine this, as it’s probably the most complicated section of the tutorial. CodeIgniter has an (optional) Active Record class that allows developers to access their databases in an Active Record-style manner. You can write your own raw SQL queries and pass them to $this->db->query(), but there are a number of advantages to using the Active Record class, not the least of which is CodeIgniter will handle your table prefixes for you.

Create database

handle your table prefixes for you.

Go to http://localhost/phpmyadmin/ now create a database named jobs_ms. Enter into the database. Now here we have to create two tables named jobs and job_details.

CREATE TABLE jobs (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title TEXT NOT NULL,
company TEXT NOT NULL)   

CREATE TABLE job_details (
jd_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
id INT UNSIGNED NOT NULL,vacancy_no INT UNSIGNED NOT NULL,
description TEXT NOT NULL,
educational_req TEXT NOT NULL,experience_req TEXT NOT NULL,
additional_job_req TEXT NOT NULL,
salary_rng VARCHAR(150) NOT NULL,
other_ben TEXT NOT NULL,location TEXT NOT NULL
)

This will create a very basic table to store our job data in. Populate it with some sample rows – be creative and come up with some job postings you’d like to see. The table structure isn’t meant to be at all representative, and a live job database would have far more data for each job record, however as we don’t need any more data to demonstrate CodeIgniter we’ll stick with this. Finally, we now have a database layer and some actual data to send through it! We also have to input some dummy data to display the result properly. Now, we work on to the view!

The view

This is the final stretch – the presentation of your application. We’ve taken all the data we need from the database, and we’ve passed it to our views to be ready. These views won’t be pretty – in fact, they may not even be HTML valid – but they’re just to demonstrate the basics of MVC in CodeIgniter, so please bear with me here. Let’s start with the standard job view.

Now, our view is going to be called jobs_view. We decided this when we coded this line of our controller:

$this->load->view('jobs_view', $data);

This basically tells CodeIgniter to load up the view at system/application/views/jobs_view.php, and to take the associative array $data and create a variable available within the view for each element.

system/application/views/jobs_view.php

<html>
<body>
 <table border="1" style="border-collapse: collapse;">
  <thead>
   <tr>
    <th>Job Title</th>
    <th>Company</th>
    <th>View</th>
   </tr>
  </thead>
  <tbody>
<?php foreach ($jobs as $job): ?>
   <tr>
    <td><?=$job['title']?></td>
    <td><?=$job['company']?></td>
    <td><?=anchor('jobs/view_job/'.$job['id'], 'View')?></td>
   </tr>
<?php endforeach; ?>
  </tbody>
 </table>
</body>
</html>

All pretty simple stuff, and very standard PHP. Our final view, for individual jobs, is even easier:

system/application/views/job_view.php

<html>
<head>
<?
	if(isset($jobs)):
  		   $row = $jobs->row(0);
		   $title = $row->title;
		   $company = $row->company;
  		   $vacancy_no = $row->vacancy_no;
  		   $description = $row->description;
		   $educational_req = $row->educational_req;
  		   $experience_req = $row->experience_req;
  		   $additional_job_req = $row->additional_job_req;
		   $salary_rng = $row->salary_rng;
  		   $other_ben = $row->other_ben;
		   $location = $row->location;
		endif;
?>
<h1>Viewing Job: <?=$title; ?></h1>
<p><strong><?=$company; ?></strong></p>
<p>Vacancy No. <?=$vacancy_no;?></p>
<p><strong>Job Description / Responsibility</strong></p>
<p><?=$description;?></p>
<p><strong>Educational Requirements</strong></p>
<p><?=$educational_req;?></p>
<p><strong>Experience Requirements</strong></p>
<p><?=$experience_req;?></p>
<p><strong>Additional Job Requirements</strong></p>
<p><?=$additional_job_req;?></p>
<p><strong>Salary Range</strong></p>
<p><?=$salary_rng;?></p>
<p><strong>Other Benefits</strong></p>
<p><?=$other_ben;?></p>
<p>Job Location <?=$location;?></p>
</head>
</html>

Now we complete our work. A database driven PHP web application built on CodeIgniter. Load up http://localhost/Jobs/index.php/jobs/list_jobs and you should see something like this:

jobs

And Job details page look like this:

job_details

13 Responses

  1. Nice one..

  2. Thanks… it’s helpfull

  3. nice and pretty useful, thanks for this in-depth review 🙂

    By the way can we share each others Blog Links? It ‘ll be good idea to link two blogs if they are producing same content. what do you think? Please let me know.

    Thanks
    Sach
    (sachinkraj@gmail.com)
    (sachinkraj.com)
    (sachinkraj.wordpress.com)

  4. Im new with CI…. i lov u tutorial 🙂
    thanks

  5. I found your tutorial very helpfull, simple and clear.
    Thank you for sharing.

  6. Can i get any idea about creating job portal in asp.net or c#.net

  7. Thanks for sharing this nice and simple tutorial.

  8. Very Helpful tutorial, Thanks for sharing

  9. Hope you will create a more advance tutorial for this job portal.

    suggestion:
    User Profile
    Reply to a job
    History
    upload CV

    thanks

  10. A new job portal has been launched, where you can find only women related jobs.
    A job portal which is dedicated to Indian women job seekers. To find out more Please visit http://www.naukriforwomen.com

  11. Thanks,,,this is very helpful for my skill

  12. Nice tutorial to learn MVC. MVC looks quite tough in which things to call at which place.

    I am new to CI. trying to put hands on CI. Thinking to develop normal cms site with 4 links which have header file included, footer file included. A page can have TWO submit buttons. how to handle two forms in single page?

    Please help.

    Thanks,
    Ravindra.

  13. Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

Leave a comment