Display results from a database on multiple pages

when Displaying result in webpage there may be thousands of search result in one page.So it effect to page loading time and user friendliness. So Have have to split result to multiple pages.
This example is to view result in multiple pages. Here I am using CodeIgniter-3.0.4 to create the web pages.

create a model called Adertisement_model.php in model folder. and use this code.
----------------------------------------------------------------------------------------------------------------------------
     
class AllAds_model extends CI_Model{

public function _construct(){

parent::_construct();

}

        /*
*load all advertisements data that belongs to the current login user
*@parameters
*@param integer $page-page no
*@param integer $ads -no of ads

*@param  integer $id-user id

*@returns array of advertismnet data that belogns to the user
*/
       public function limit_Ads( $page, $ads ){
$this->load->database();

$this->db->select("*");
$this->db->from("advertisements");
$this->db->where("adstatus",'confi');
$start=($page-1)*$ads;
$this->db->limit($ads,$start);

$query=$this->db->get();
return $query->result();
}

        /*
*get the page count from view advertisements
*@param string $all-all no of ads
*@param int $ads -advertisements per page
*
*@returns the no of page that result will dispalyed
*/
public function set_pagecount( $all, $ads ){


return $all/$ads;
}

        /*
*get the Current active advertisements cont

*@returns no of advertisment that in results
*/
public function ads_count(){
$this->load->database();
$query = $this->db->query("select count(adid) as counts from advertisements where adstatus='confi'");

foreach ($query->result() as $row)
{
return $row->counts;
}

}
}

----------------------------------------------------------------------------------------------------------------------------
*limit_ad function limiting the advertisement result to value that $ads variable gives. $page variable to which page you are in.
 In $this->db->limit($ads,$start); $ads number of advertisement in a page and $start -to first result row number in page.
*set_pagecount get the how many page that need to show the all results.
*ads_count get the count of all results.


now create a controller called Advertisement.php in controller folder. and use this code.
----------------------------------------------------------------------------------------------------------------------------

class Advertisement extends CI_Controller {
var $mapp=5; //set of max advertisement per one page
//store the current sql query


// constructor
public function _construct(){

parent::_construct();
$this->load->helper('url');
$this->load->library('session');


}

/*
*view the Advertisements that in page
*@param int $page - page number
*/
public function ads_page( $page ){

$this->load->helper('url');
//load the Advertisement_model model
$this->load->model("Advertisement_model","Ads");

//get Ad information for the page number
$car=$this->Ads->limit_Ads($page,$this->mapp);

// get number of Advertisements
$count=$this->Ads->ads_count();

//data array for send data to AllAds_view
$data['vehicle']=$car; //vehicle information
$data['count']=$count; //Advertisements count

$data['pages']=$this->Ads->set_pagecount($count,$this->mapp); //get the no of pages that have


//load the views
$this->load->view("Advertisement_view",$data);
$this->load->view('AdsPages_view',$data);



}
}
----------------------------------------------------------------------------------------------------------------------------

<blockquote>now create a View called AdsPages_view.php in Views folder. and use this code.</blockquote>

here you can go to 1 to n page that your result generated
---------------------------------------------------------------------------------------------------------------------------
<blockquote><div class="body" align="center" style='min-width:80%'>

< ? php

if(0<$pages){
echo '<';
}
for($i=0;$i<$pages;$i++)
{
echo  '<a href=';

echo site_url('AllAds/ads_page/'.($i+1));


echo' >'.($i+1)."</a>,";
}
if(0<$pages){
echo '>';
}
? >
</div>
</blockquote>
----------------------------------------------------------------------------------------------------------------------------
now create a View called Advertisement_view.php in Views folder. and use this code.

here you limited result will show in table
-----------------------------------------------------------------------------------------------------------------------------
<blockquote><div class="body" align="center" style='min-width:80%'>
<table>
< ? php
for ($i = 0; $i < count($vehicle); $i++) {
echo "<tr><td>".$vehicle[$i]->modelName."</td><td".$vehicle[$i]->year."></td><td>".$vehicle[$i]->category."</td><tr>";

? >
</table>
</div></blockquote>
-----------------------------------------------------------------------------------------------------------------------------

Comments

Popular Posts