I have this problem before when I tried to search a keyword from a database using jQuery-AJAX-PHP. Where in the query is fired up multiple times which slows down process. After searching reading various solution for this problem. I've came up with the jQuery function below.
This handles the keypress event of the textbox with an id keyword.
$('#keyword').live('keypress', KeywordSearch);
Javascript Function:
function KeywordSearch(event) {
if (event.handled !== true) { if (event.keyCode == 13){ var type = $('#searchType').val(); var keyword = $('#keyword').val();
var result = $.ajax({ type: "GET", url: "search.php?keyword=" + keyword + "&type=" + type, async: false}).responseText;
In this tutorial, I'll be showing you a basic example on how to create Pagination on your website. I'm not really a Pro Coder so please bare with my coding. The purpose of this is just to give basic logic of how it was created. So, let's get it started.
First, we will create the PHP file to print out the page ranges. We have two parameters $LIMIT and $RANGE that is passed by an Ajax request via jQuery.
$LIMIT - This is the limit of rows per page to be displayed. $RANGE - Initially, this is set to 1. This is the pages range that will be displayed. It will decrement/increment as you click the Next or Previous buttons.
Additional parameters...
$MAX - The maximum number of pages to be shown in a range. This example shows 10. $RECORDS - Get the total number of records in the database table. $PAGES - This is the quotient of the $RECORDS divided by the $LIMIT.
Then we do some conditional statement to print out the pages, next and prev button. PHP File (pages.php)
<?php include "js/pagination.js"; require_once("includes/functions.php");
# Declare variable of the class $PI = new PartsInventory;
# Get parameter values passed by the jQuery-Ajax script $_GET['limit'] == "" ? $LIMIT = 10 : $LIMIT = $_GET['limit']; $RANGE = $_GET['page'];
# Get the Total Number of Records $RECORDS = $PI->InventoryCount();
# Calculate the number of pages $PAGES = number_format($RECORDS / $LIMIT);
# This will be the range of pages to be displayed # e.g. 1 2 3 4 5 6 7 8 9 10 ... 20 > $MAX = 10;