Amazon Best Products with minimum price

Tuesday 25 April 2017

CodeIgniter Web Framework.

Code Igniter is a lightweight Framework (Released in 2006). Code Igniter is very easy to install, within half an hour you can run your program by CodeIgniter. It works nicely on almost all Shared and Dedicated hosting Platforms.Currently required PHP version is 5.2.4.

Based on the 2017 survey, Code Igniter was used 14.9% (i.e. 1,085) in the projects by developers.It provides more flexibility to develop the Complex Websites and Cloud Apps.


Codeigniter is an application development framework which is used for making Website, Web Application etc. We can make CMS (Content Management System) from Codeigniter Framework faster than Scratch PHP. It has a very qualitative functionality which is why it is used for making the good Website in a faster way.

If you has the experience or know the basics of PHP so you can develop any website or Web App in Codeigniter framework. Codeigniter Framework save lots of time for developing the Website or Web App instead of Scratch PHP. If you are using it then, framework provides you security also from various attacks.

Codeigniter has many features:

Some of the Important features are:
  • MVC (Model-View-Controller) Based System.
  • Light Weight compare to other Frameworks.
  • Active Records
  • Own Form and Data Validation
  • Security and XSS Filtering
  • Easily Session Management
  • Email Sending Class
  • File Download Class
  • File Uploading Class
  • FTP Class
  • Pagination
  • Data Encryption
  • Benchmarking
  • Template Engine Class
  • Search Engine Friendly URLs
  • Helper Function
Codeigniter is based on MVC (Model-View-Controller) System. MVC is a system that separates business logic from presentation. In MVC there is no need to write the code again and again.


Description of Model, View, and Controller.

Model: The Model represents data structure of the database. In Model, you can insert, Delete, Retrieve and Update the table in database thereby, the data passes to the View through the Controller.

The model folder is located in Application Folder of Codeigniter framework. You can write your code and save as .php extension and save in the model folder.

View: It's the information that user can see on the Screen. A view is normally a web page but in Codeigniter, the View can be full page or part of the page. It can be any type of page.In View, you can write your front end code as well as scripting code which can easily be seen by the user on the screen.

Controller: The Controller is intermediate between Model and View. Which transfer's the function between Model and View, it interacts with both Model and View, Normally, Controller plays the main Role in Passing the data into the Systems.

That's it from my side..

If you have any questions, then write in the comments down below OR send me your questions and suggestions to my Email id: aziz.10786@gmail.com.

Stay tuned for more coding!!

Thank You...

Thursday 20 April 2017

Top 5 PHP Content Management System (CMS) .

CMS stands for Content Management System that allows users to create, publish and manage all the content of  PAGES and POST. This powerful tool allows users to create an effective and well-structured website within a day. These days most of the companies prefer CMS (Content Management System) website because it is easy to maintain the website as it is to create, edit, publish the content on the website.

Top 5 PHP CMS List:

1) Wordpress.
2) Drupal.
3) Joomla.
4) MODX.
5) Concrere5

Let's start with all Top 5 PHP CMS Content Management System one by one:

1) Wordpress:




Wordpress is web software we can use it to create website and blogs. Nowadays, it's most popular blogging platform for bloggers. Wordpress is a better choice for bloggers. There is no technical need to create any blog and website in WordPress. WordPress was initially released on 27th may 2003 by Matt Mullenweg and Mike Little, it has grown with time and now it has become the most popular CMS in PHP.

Features:


  • User Management
  • Media Management
  • Theme System
  • Extend with Plugins
  • Serch Engine Optimization
  • Multilingual
  • Importers

2) Drupal:




Drupal is another best CMS that is used to create millions of websites and applications. Drupal is versatile and powerful it gives lots of freedom to developers. Drupal is free and open source Content Management System (CMS) that allows organizing, managing, and publishing content. Drupal was initially released in January 2011 by Dries Buytaerti.

Features:

  • It helps easily to create and manage your site.
  • Drupal is open source software hence require no licensing cost.
  • Drupal helps in easily creating or managing blog or website.
3) Joomla:



Joomla is an award-winning Content Management System based on PHP. Like Drupal, it is also most popular CMS amongst developer. It is highly recommended for building online stores and large social networks and other web application. It is free and extendable which is separated into front-end and back-end templates. 

Joomla is based on Mambo CMS which was developed by an Australian company in 2001 and initially released on August 17, 2005.

Features:

  • User Manager
  • Content Manager
  • Banner Manager
  • Template Manager
  • Menu Manager
  • Weblink Manager
  • Media Manager
4) MODX



MODX is one of the most creative and flexible Content Management System for web Platform.MODX is very easy to install and manage.It is also a Popular CMS but comparatively it is used less for creating websites. MODX has been awarded Packt Publishing's Most Promising Open Source Content Management System in 2017.                                    

5) Concrete5:



Concrete is another best CMS (Content Management System). Concrete is more powerful and easy to use. The admin panel of concrete is easy to use and it looks nice. It also provides many plug-in, add-ons and themes. The Concrete user interface is easy to use.

Follow me or you can also contact me via mail: aziz.10786@gmail.com

Stay tuned for more coding!!

Thank You...


Saturday 8 April 2017

Top 5 PHP Frameworks for Developers.

PHP is the most popular server-side scripting language in the world.These days developers build very complex websites and cloud apps. From scratch or core PHP, it's hard to develop good and complex websites and cloud apps.

Top 10 Framework in PHP pie chart according to 2017 survey:



PHP Framework works on the basis of MVC (Model-View-Controller), it is more structured.In this post, we show Top 5 PHP Frameworks.

First thing, that comes in mind why every developer prefers to use a framework instead of scratch PHP and what feature does PHP Framework have.

Following are the points to justify the above statement:

a) The framework makes developer speed up in coding.
b) It provides well-organised structure, reusable and maintainable code.
c) Provides scalable in making any Web-app
d) Provides a high-level security from any attacks.
e) Follow MVC(Model-View-Controller) pattern that separates the presentation and logic.
f) Supports object-oriented programming tools.
h) Lightweight package.

1) Laravel.

Laravel is a new PHP Framework (Released in 2011), it is most popular Framework among all developers. Laravel has many features that help developers to make Rapid application development. Laravel has it's own lightweight templating engine called "BLADE", Syntax provides you with the frequent task to do, such as authentication, querying, caching, session, routing.

Based on the 2017 survey, Laravel was used 43.7% (3,177) in the Projects by developers.It provides more Flexibility to develop the Complex Websites and Cloud Apps.



2) Code Igniter.

Code Igniter is a lightweight Framework (Released in 2006). Code Igniter is very easy to install, within half an hour you can run your simple program by CodeIgniter. It works nicely on almost all Shared and Dedicated hosting Platform.Currently required PHP version is 5.2.4.

Based on the 2017 survey, Code Igniter was used 14.9%(1,085) in the Projects by developers.It provides more Flexibility to develop the Complex Websites and Cloud Apps.



3) Symfony.

 Symfony is a lightweight Framework (Released in 2005). Symphony Framework is mainly used in projects such as Drupal CMS(Content Management System) or PhpBB forum Software. Symfony has a wide developer community and Laravel also relies on it.

Symfony components are Usable PHP Libraries that can be used with a different task like object configuration, routing, form creation templating etc.

Based on the 2017 survey, Symfony used 13.6% (990) in the Projects by developers.



4) Zend.

Zend is a robust and stable PHP Framework (Released in 2006). It is usually not recommended for small projects because it is packed with lots of configuration but good for complex projects. Following are the partners of Zend i.e IBM, Microsoft, Google, and Adobe.The Latest Released Zend Framework 3 will be optimized for PHP 7 but it supports PHP 5.5 onwards.

 Based on the 2017 survey, Symfony used 12.6% (907) in the Projects by developers.



5) Yii 2.

Yii Framework is faster than other Framework (Released in 2011). It increases the performance of your Site and it's Faster than other Frameworks because it uses the lazy loading techniques. Yii 2 is purely object-oriented technique and its work based on the Don't repeat yourself (DRY) Coding Concept.

Yii is integrated with Jquery and set of Ajax-enabled features, It's good for those who prefer frontend background because it has own themes of frontend design.

Based on the 2017 survey, Symfony used 5.1% (373) in the Projects by developers.


Follow me or you can also contact me via mail: aziz.10786@gmail.com

Stay tuned for more coding!!

Thank you....

Wednesday 5 April 2017

Integrate Ion-auth Authentication and Roles in Codeigniter Framework

Ion-Auth is a Library which manage Roles and authentication of the User in codeigniter framework.

In this Tutorial, we are going to learn how to integrate Ion-Auth Library in codeigniter framework.This Library is great, it has group of User Table , Roles table, login attempt to failure table and CSRF which avoids "form" from Internal attack.

Here are the following steps which we should follow to integrate the Ion-Auth Library into Codeigniter framework.



1) Install the Codeigniter Framework.

Go to the Website of Codeigniter and download the codeigniter, There are two version available (version 2 and version 3). Always download the latest version (i.e.Version 3) because latest version has more features, thereby download and save the zip file in your computer and Extract it in root directory, c:\xampp\htdocs. After Extraction we can change the file name to whatever one want's.

2) Install the Ion-Auth.

Go to the repository in github and download the zip file of Ion-Auth library.

Once we are done with downloading, we will extract the zip file in folder and pass the necessary file to our respective project.

Which are:

a) Copy the file taht is config called ion_auth.php into config folder.
b) Copy the file that is in controller called auth.php into controller folder.
c) Copy the file that is in library Bycrypt.php and Ion_auth.php into library folder.
d) Copy the file that is in Model ion_auth_model.php into Model folder.
e) Copy the file that is in View ion_auth.php into view folder.

3) Database:

Now let's create the database in mysql called ion_auth and import the following tables.
These tables work on the following authentication and roles:

DROP TABLE IF EXISTS `groups`;

#
# Table structure for table 'groups'
#

CREATE TABLE `groups` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `description` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Dumping data for table 'groups'
#

INSERT INTO `groups` (`id`, `name`, `description`) VALUES
     (1,'admin','Administrator'),
     (2,'members','General User');



DROP TABLE IF EXISTS `users`;

#
# Table structure for table 'users'
#

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ip_address` varbinary(16) NOT NULL,
  `username` varchar(100) NOT NULL,
  `password` varchar(80) NOT NULL,
  `salt` varchar(40) DEFAULT NULL,
  `email` varchar(100) NOT NULL,
  `activation_code` varchar(40) DEFAULT NULL,
  `forgotten_password_code` varchar(40) DEFAULT NULL,
  `forgotten_password_time` int(11) unsigned DEFAULT NULL,
  `remember_code` varchar(40) DEFAULT NULL,
  `created_on` int(11) unsigned NOT NULL,
  `last_login` int(11) unsigned DEFAULT NULL,
  `active` tinyint(1) unsigned DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `company` varchar(100) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


#
# Dumping data for table 'users'
#

INSERT INTO `users` (`id`, `ip_address`, `username`, `password`, `salt`, `email`, `activation_code`, `forgotten_password_code`, `created_on`, `last_login`, `active`, `first_name`, `last_name`, `company`, `phone`) VALUES
     ('1',0x7f000001,'administrator','59beecdf7fc966e2f17fd8f65a4a9aeb09d4a3d4','9462e8eee0','admin@admin.com','',NULL,'1268889823','1268889823','1', 'Admin','istrator','ADMIN','0');


DROP TABLE IF EXISTS `users_groups`;

#
# Table structure for table 'users_groups'
#

CREATE TABLE `users_groups` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned NOT NULL,
  `group_id` mediumint(8) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_users_groups_users1_idx` (`user_id`),
  KEY `fk_users_groups_groups1_idx` (`group_id`),
  CONSTRAINT `uc_users_groups` UNIQUE (`user_id`, `group_id`),
  CONSTRAINT `fk_users_groups_users1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `fk_users_groups_groups1` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `users_groups` (`id`, `user_id`, `group_id`) VALUES
     (1,1,1),
     (2,1,2);


DROP TABLE IF EXISTS `login_attempts`;

#
# Table structure for table 'login_attempts'
#

CREATE TABLE `login_attempts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ip_address` varbinary(16) NOT NULL,
  `login` varchar(100) NOT NULL,
  `time` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 These tables are very simple, see the description down:

a) user table: it stores all information of user like user_name, email_id, password, first_name, last_name etc.
b) login_attempt table: 1 to 1 with users, ip_address is foreign key.
c) Group table: Allow to create new group based on the group it assigns the role.
d) users_groups table: many to many between users and groups.

For running the program call the ion_auth.php file which is located in  application/controller folder.

I hope you can now easily integrate the Ion_Auth library int codeigniter framework.

If anyone has any issues, feel free to write in the comments down below.

Thank you
 


Tuesday 4 April 2017

Auto-complete search in codeigniter using Ajax.


The Auto complete search box provides,suggestion when you enter any data into the text field.
In this tutorial, data will be fetched from database and displayed in text box when any one types any data in to the text field.

Nowadays, every web app needs to be easy and simple to use. Instead of using drop-down by select tag.we can create the auto search box which fetches the data from database and display in search box.

1) First we are going to create pages in "View." Let's create the task.php in application/view and load the CDN of bootstrap and Jquery in <head> tag in html page.

<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Pagination</title>
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
</body>
</html>

In the above code, we have loaded the bootstrap CDN , Jquery CDN and jquery_ui CDN for displaying the data in searchbox.
Now, we will create one "form" in <body> tag.
After this,we are going to create one "form" with some textfield, assign the ID or CLASS in text field and call it by Ajax.

<?php
$attributes = array('class' => 'form-horizontal','id'=>'');
echo form_open('', $attributes);
?>
<div class="row">
<div class="col-sm-12">
<div class="col-sm-3">
<div class="input-group">
<span>User Name</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'name','name'=>'name')]); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Date</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'date','name'=>'date']); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Task Name</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'t_name','name'=>'t_name']); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Partner Name</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'partner','name'=>'partner']); ?>
</div>
</div>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="submit">
<?php 
echo form_close();
?>

Here in the above code, we have created one form with some text field and assign them ID. Based on the text field ID we called the Ajax and fetched the data from controller and passed it to "View" inside the form text field.

If we write something related to word, you will find here the ajax code which is applied on text field thereby passing the url to controller and catching the data from database and showing it in the textbox.

 $(function(){ 
    $(document).on("keydown.autocomplete","#t_name",function(e){ 
      $(this).autocomplete({ 
      source : '<?php echo base_url();?>task_search',
      selectFirst: true,
          change: function (event, ui) {
           if (ui.item == null){ 
            //here is null if entered value is not match in suggestion list
            $(this).val((ui.item ? ui.item.id : ""));
            }
           } 
      }); 
    }); 
  });

a) In the above Ajax query, we used keydown.autocpmlete which is used for the selecting word to fetch from database and display like dropdown.
b) function (event, ui) is used for selecting only the fetched data from database.If you write you own text then system will show you blank data.

2) Now let's create the controller task_search.php in application/controller.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class task_search extends CI_Controller{ 

public function __construct()
{ 
parent::__construct();
/*Load the task of model*/
$this->load->model('pojo'); 
}


public function index(){

if (!isset($_GET['term']))
{
  exit;
}
  
  $qs = strtolower($this->input->get('term'));     
  $this->pojo->search_task($qs);

} 
}

 a) $this->load->model('pojo'); load the model pojo.

b) $qs = strtolower($this->input->get('term'));    
  $this->pojo->search_task($qs);

 Get the variable from "View" and passed to the model pojo method search_task.

3) Here is the method search_task in application/model/pojo

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pojo extends CI_Model{  

public function search_task($qs){
  /*comparing data with the text box*/  
  $query = $this->db->query("SELECT t_name FROM task_name WHERE t_name LIKE ('$qs%') ORDER BY t_name LIMIT 5");
  /* It checks the row in database table*/
    if($query->result_id->num_rows > 0)
        {
        foreach ($query->result_array() as $row)
              {
                $row_set[] = htmlentities(stripslashes($row['t_name']));
              }
             echo json_encode($row_set); 
        }       
  }
  }

Above is the logic code, which is used to fetch the data from database and passed by foreach by using json_encode.

That's it from my side, if you have any queries, please feel free to write in the comments down below.

Thank you!!

Monday 3 April 2017

How to use Pagination with Codeigniter ??

This Blog Post is about : How to use pagination in codeigniter Framework?

Codeigniter has its own library pagination which is used for enable pagination on the table.
Pagination is used for the displaying data one by one in page wise (For example: If there are 100 records in table and you want to display it in a page, thereby it takes the whole page and users keep only scrolling down for the record.Instead of scrolling down the page, Pagination divides the record into multiple pages and provides the link. So user can jump direct to any one page where he wants to.)

In this tutorial, we are going to fetch data from database and display it in "View" after that we will write a code in controller of pagination and display the same data in table pagination format.

1) First we are going to create pages in "View". Let's create the task.php in application/view and load the CDN of bootstrap and Jquery in <head> tag in html page.

<!doctype html>
<html lang = "en">
<head>
  <meta charset = "utf-8">
  <title>Pagination</title>
  <script src = "https://code.jquery.com/jquery-1.10.2.js"></script></script>
 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
</head>
<body>
</body>
</html>

In the above code, we load the CDN of Bootstrap and Jquery for running the data very well.
2) Let's create task_con.php in application/controller.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task_con CI_Controller
{
 
 function __construct()
 {
  parent::__construct();
  //Loading the model
  $this->load->model('pojo');
 }

 public function index(){
  /* Pagination of Data */
  $this->load->library('pagination');

  $config = [
   'base_url' => base_url('task_con'),
   'uri_segment'  => 3,
   'per_page' => 10,
   'total_rows'=> $this->pojo->task_pagination(),
   'full_tag_open'  =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
  ];

  $this->pagination->initialize($config); 
  $data['result']=$this->pojo->task($config['per_page'], $this->uri->segment(3));
  
  $this->load->view('task.php', $data); 
  
 }

In the above controller, we have covered the following points and here is the explanation of each line code:

a) $this->load->model('pojo'); For loading the pojo model in which we are going to create the two methods one is task method and another one is task_pagination method.
b) $this->load->library('pagination'); Is used to load the pagination library without which it does not work thereby, we need to load the pagination library first.
c) After that we create the $config array in that we are going to pass all the syntax of Pagination library.
d) 'base_url' => base_url('task/index'), Pass here the path of the method.
e) 'uri_segment'       => 3, this is the number for the path of URL(domain-name/controller/method). So write the number where current method comes.
f) 'per_page'=> 10, how many records show on the page.
g) 'total_rows'        => $this->pojo->task_pagination(),  this is used to count the number of records available in the system based on that it divides the numbers.

Let's create task_pagination method in application/model/pojo

public function task_pagination(){
    /* This display all task of Employees from database*/
    $this->db->select('*');
    $this->db->from('task');
    $query=$this->db->get();
    return $result=$query->num_rows();
  }


In the above method, business logic get all record of task and is passed to the controller.

 return $result=$query->num_rows();  it returns the number of all rows to the pagination.

h) 'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',


All the above are for pagination link.

i) $this->pagination->initialize($config);  This code is to initialise the $config syntax.
j) $data['result']=$this->pojo->task($config['per_page'], $this->uri->segment(3));

This line is passes the ($config['per_page'], $this->uri->segment(3))  to the hodm method in pojo.php model.

Let's create the hodm method in application/model/pojo.php


public function task( $limit, $offset){
    $this->db->select('*');
    $this->db->from('task');
    $this->db->limit($limit, $offset);
    $query=$this->db->get();
    return $result=$query->result();
  }


public function task( $limit, $offset) $limit and $offset is passed for the pagination.


k) $this->load->view('task.php', $data) At last, fetch data are stored in $data array and $data passed to the task.php view file.

Here, is the code of View file(task.php)

<body>
<div class="col-md-12">
<input class="search" type="search" data-column="all">
<div class="table-responsive" id="task">
<table class="tablesorter">
<thead>
<tr class="">
<th>Time</th>
<th>Task</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $row) { ?>
<tr>
<td>
<td><?php echo $row->duration;?></td>
<td><?php echo $row->task;?></td>
<td><?php echo $row->status;?></td>
</tr>
<?php } ?>
</tbody>
</table> 
<?= $this->pagination->create_links(); ?>
</div>     

</div>
</body>

 $this->pagination->create_links() pass this tag after table tag for creating pagination.

That's it. If you have any queries, feel free to write in comments down below!!

Stay tuned for some more coding!!

Thank you.




Saturday 1 April 2017

Fetch data from database and display in HTML page without refreshing page in CodeIgniter

Nowadays in making Website, Web App, Cloud App etc. everyone wants his/her system fast on the server. For making the system fast and responsive we use Ajax and Bootstrap.

Here, is my blog post which is based on Fetching data from database and display it in html page but without refreshing page in php. 

If any one clicks any button, then it only refreshes the part of that page not the whole page.

If you have the basic knowledge of codeigniter framework then you are at right place. If you don't, not to worry just click on this link to gather the required knowledge:  https://www.tutorialspoint.com/codeigniter/

So Let's start and create the following file in codeigniter framework.

1) application/controllers/task.php
2) application/model/pojo.php
3) application/view/task/index.php

1) First, create the index.php file in application/view/task/ and load all CDN of ajax and bootstrap in <head> tag.

<!doctype html>
<html lang = "en">
<head>
  <meta charset = "utf-8">
  <title>Ajax Request</title>
  <script src = "https://code.jquery.com/jquery-1.10.2.js"></script></script>
 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
</head>
<body>
</body>
</html>

 Now, we are going to create Nav bar and div tag in body tag of html.

In Nav bar, we are going to write 2 or 3 names so as when any one clicks on Nav bar word, then it only changes the div tag content by fetching data from database.

<!doctype html>
<html lang = "en">
<head></head><body>
 <div>
<ul class="nav nav-tabs" style="background-color:black;">
<li><a href="" id="employee">EMPLOYEE</a></li>
<li><a href="" id="director">DIRECTOR</a></li>
<!-- <li><a href="" id="analytics">ANALYTICS</a></li>-->
</ul>
</div>
<br>
<div id="search_change">
</div>
 
</body>

</html>

2) Ajax Query Request.

Now write this, code in ajax to call the controller. Here we call by the ID (#employee) and pass the URL of the controller, where all the actual code are there and the data is fetched from database and displayed to the div tag which is call by the search_change ID.


<!doctype html>
<html lang = "en">
<head></head><body>
 <script>
   $(document).ready(function() {
    $('#employee').click(function (event) {
        dataString = $("#employee").serialize();
        $.ajax({
            type:"POST",
            url:"<?php echo base_url(); ?>task/employee",
            data:dataString,

            success:function (data) {
                $('#search_change').html(data);
            }

        });
        event.preventDefault();
    });
});

   $(document).ready(function() {
    $('#director').click(function (event) {
        dataString = $("#director").serialize();
        $.ajax({
            type:"POST",
            url:"<?php echo base_url(); ?>task/director",
            data:dataString,

            success:function (data) {
                $('#search_change').html(data);
            }

        });
        event.preventDefault();
    });
});
 </script>
</body>

</html>


 In the above script, ajax call has occurred twice:
a) One is call employee by calling controller task/employee &
b) Second is call director by calling controller task/director

Here, we are creating On file in "View" which displays data after fetching data from database.
Creating all_task.php in application/view/task

<table class="table table-striped table-hover table-responsive">
  <thead>
    <tr class="">
      <th>Director</th>
      <th>Time</th>
      <th>Task</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
  <?php $total_time=0; ?>
    <?php foreach($result as $row) { ?>
        <tr>
        <td><?php echo $row->director;?></td>
        <td><?php echo $row->duration;?></td>
        <td><?php echo $row->task;?></td>
        <td><?php echo $row->status;?></td>
        </tr>
        <?php $total_time += $row->duration; ?>
    <?php } ?>
        <tr>
          <td>Total</td>
          <td></td>
          <td><?php echo "$total_time";?></td>
          <td></td>
          <td></td>
        </tr>
  </tbody>
</table> 

After Fetching the data from controller through the model it shows in the "View". Here we used foreach($result as $row) to display the all data with the help of table.

3) Now we are going to create task.php file in application/controller and inside the task.php controller, create two method employee and director respectively.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class task extends CI_Controller{ 

 public function __construct()
 {
  parent::__construct();
 }

 public function index(){

 }

 public function employee(){
  
 }

 public function director(){
  
 }
 }

a) In construct, we will load model pojo
b) In employee method, we will call the business logic from the model and pass it to the View by ajax
c) In director method, we will call the business logic from the model and pass it to the View by ajax

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class task extends CI_Controller{ 

public function __construct()
{
 parent::__construct();
 /*Load the task of model*/
 $this->load->model('pojo'); 
}

public function index(){
}

public function employee(){
 $data['result']=$this->pojo->employee();

 $this->load->view('task/all_task',$data); 
}

public function director(){
 $data['result']=$this->pojo->directors();

 $this->load->view('task/all_task',$data); 
}


4) Lastly, we are going to create pojo.php in application/model for business logic.
We are creating two method in pojo model one is employee and another one is director. Both the methods fetch the data from database by respective tables in database and pass the fetch data to the controller and controller stores all the fetch data into one variable array and pass it to the "View".

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pojo extends CI_Model{  

public function employee(){
    /* This display all task by filtering data*/
    $this->db->select('*');
    $this->db->from('employee');
    $query=$this->db->get();
    return $result=$query->result();
  }
  
  public function director(){
    /* This display all task by filtering data*/
    $this->db->select('*');
    $this->db->from('director');
    $query=$this->db->get();
    return $result=$query->result();
  }
  
 } 


That's it from my side. If you have any problem, feel free to write in the comments down below..

Stay tuned for more coding!!!

Thank You..