Paging using PHP and MySQL

I have coded a class for paging, you may find it here.

### Code for Paging Starts ###
//index no of the page
$pageIndex = ((int)$_REQUEST['pageIndex'] == 0) ? 1 : (int)$_REQUEST['pageIndex'];
 
//number of rows
$rowPerPage = 10;
 
//counting total number of rows
$sql = 'SELECT COUNT(*)
        FROM {your_table_name}
        WHERE {IF ANY CONDITION}';
 
$query = mysql_query($sql, $link_identifier);
$row = mysql_fetch_row($query);
 
// total records
$totalRecords = $row[0];
 
// number of pages
$totalPages = ceil($totalRecords/$rowPerPage);
 
if($pageIndex > $totalPages)
{
    $pageIndex = $totalPages;
}
 
$startIndex = ($pageIndex - 1) * $rowPerPage;
 
if($totalRecords > $rowPerPage)
{
    $endIndex = $startIndex + $rowPerPage;
}
else
{
    $endIndex = $totalRecords;
}
 
if($pageIndex == $totalPages)
{
    if($totalRecords > $totalPages * $rowPerPage)
    {
        $endIndex = $totalRecords;
    }
}
 
if($totalRecords > 0)
{
    $rangeStart = $startIndex+1;
 
    if(($totalRecords > $rowPerPage) || ($pageIndex == $totalPages))
    {
        $rangeEnd = $totalRecords;
    }
    else
    {
        $rangeEnd = $endIndex;
    }
 
    $range = 'Showing '  . $rangeStart.' - '.$rangeEnd.' of '.$totalRecords;
}
 
// link for next page
$nextLink = 'page_name.php?pageIndex='.($pageIndex + 1);
 
// link for previous page
$previousLink = 'page_name.php?pageIndex='.($pageIndex - 1);
 
// fetching data using pagination
$sql = "SELECT *
            FROM {your_table_name}
            WHERE {IF ANY CONDITION}
            LIMIT $startIndex, $rowPerPage";
 
$query = mysql_query($sql, $link_identifier);
 
### Code for Paging Ends ###

Now use the resource ‘$query’ and do what you want to do.

Share

Regular Expression Patterns

From various sources I have collected the following regular expression patterns. They helped me a lot in front-end and back-end validation.

Alpha:

Pattern: ^[a-zA-Z_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, space ( ‘ ‘ ) and underscore ( ‘_’).

Alphanumeric:

Pattern: ^[a-zA-Z0-9_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, ‘0-9’, space ( ‘ ‘ ) and underscore ( ‘_’).

Email:

Pattern: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

Numeric:

Pattern: ^\d+$

Decimal:

Pattern: ^\d+(?:\.\d{0,2})?$

Date – 1:

Pattern: ^(0[1-9]|[1,2][0-9]|3[0-1])-(0[1-9]|1[0,1,2])-\d{4}$
Description: Date format DD-MM-YYYY

Date – 2:

Pattern: ^(0[1-9]|1[0,1,2])-(0[1-9]|[1,2][0-9]|3[0-1])-\d{4}$
Description: Date format MM-DD-YYYY

US Zip Code:

Pattern: ^\d{5}$

US Telephone Number:

Pattern: ^1?[\-]?\(?\d{3}\)?[\-]?\d{3}[\-]?\d{4}$

Color Code:

Pattern: ^#{1}?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$

Any HTML Tag:

Pattern: <([^">]+(?:”[^”]+”)*[^>]+)>

Share

FS Snake & Lader 1.0

MSH Relation Calculator

This game is based on a paper game, Snake and Ladder. You may have played this game before. I have added nothing more. I have just programmed it for my nephews and nieces. They give me the inspiration for designing and programming this game. Hope you will like it.

Click this link if you want to download it Download

Share
blog