ad

Friday, 17 April 2015

Overwhelming data on page? write a pagination script.

Imaging you've 100 rows in database, and you're wanna show them all. What're you gonna do? Well obvious answer would be to just throw all of it on one page or.. you can divide the content into small chunks. 100 rows, what about we show just 10 rows one first page, then other 10 on another, and so on.. 1st page shows 10, 2nd another 10, 3rd another 10.. and like so until we reach 100.

Whenever you're ready, go ahead and try it out!  All code's been commented in details. Enjoy!


//including  database file.
include("connection_to_db.php");

//We're getting page number from url, using get variable.
if(isset($_GET["page_num"])){
    $page_number = $_GET["page_num"];
}else{
    $page_number = 1;
}

//Select all the data.
$select_city_data_query = $conn->prepare("SELECT * FROM city LIMIT 100");
$select_city_data_query->execute();


//Lets get how rows are there in total.
$row_count = count($select_city_data_query->fetchAll());

//first page.
$first_page = 1;

//Alright I wanna show 5 chunks per page. We also need this number to divide the total pages so we know how many pages there will be in total.
$chunks_per_page = 5;

//Lets get what number would be our last page, we'll need that so when user clicks on last page link it sends him to it. You see that ceil function? well it rounds fractions up, imagine if we had unknown number of rows, lets say 121 and we wanted to show 5 per page. That if divided would be 24.2 rows per page? huh? Well yeah we cant have that, so ceil will just round the fraction up. It'll then be 25 rows per page, not 24.2..!
$last_page = ceil($row_count/$chunks_per_page);

//Okay, what do we need now? Well we needa way to display right messages on every page. If we just went now and tried our script it'd show same messages on every page. It's just grab value from GET variable do nothing with it..

//We needa do something so it shows right messages on each page. 1st page, 1st 5 messages, 2nd page, 2nd five messages and so on.. We're on 1st page, we wanna show first five messages, we all we do is make a query and limit it by 5. But what about 2nd page..? Here's what we're going to do. We start from certain messages and end it after 5 messages. We need to know where to start.
//well we got our starting point, according to our formula on 1st page we start from 0, 2nd page from 5, 3rd from 10...
$limit_starting = ($page_number - 1) * 5;

//We also needa to know where to end. According to this forumula 1st page ends at 5, 2nd at 10, 3rd at 15, 4th at 20, 5th at 25, 6th at 30...
$limit_ending = $page_number * 5;

//So now lets make query and show the result.
$select_limited_message_query = $conn->prepare("SELECT * FROM city ORDER BY ID LIMIT $limit_starting, 5");
$select_limited_message_query->execute();


//Basic while loop to show all the results and now just first one.
while($data = $select_limited_message_query->fetch(PDO::FETCH_ASSOC)){
    echo $data["Name"] . "</br>";
}

//Aren't we missing something? Yeah, we need actual link 1, 2, 3, 4, 5, 6, 7...
for($i=1; $i<=$last_page; $i++){
    echo "<a href=\"?page_num=$i\"> $i </a>";
}

//Lets show link, which when clicked sends us to first page.
echo "<a href='?page_num=".$first_page."'>First page</a> ";
echo "<a href='?page_num=".$last_page."'>Last page</a> ";

No comments:

Post a Comment