RSS

Monthly Archives: January 2010

Coding Example of Pagination In PHP With MySQL

As any database grows, showing all the results of a query on a single page is no longer practical. This is where pagination comes in very useful. One can display his results over a number of pages, each linked to the next, and to allow your users to browse your content in bite sized pieces.

Here I have a class to do the entire pagination job. One has to just use it properly.

Here is the procedure how to use this class.

  • First one has to know how many database entry he got. He can do it easily by using a COUNT function of database.
  • Then he has to create an object of the PageNavigation class. For this will have to pass three arguments
    • first one is for Total number of element
    • second one is for which page you want to show now
    • And the last is for size of the page.

Such as

$objPageNev=new PageNavigation(120, 2, 25);

  • Use $objPageNev->initialMsg and $objPageNev->maxElement to limit the result form start element to max element.

Such as

LIMIT $objPageNev->initialMsg , $objPageNev->maxElement;

  • To display the nevagain bar at bottom or top just use the method PagingShowHTML with supplying the root link.

$objPageNev->PagingShowHTML(“http://example.com?index.php?action=cat”)

Negation class code:

<?php

class PageNavigation

{

var $totalPages =null;

var $page =null;

var $totalElement =null;

var $initialMsg =null;

var $threadStart =null;

var $threadEnd =null;

var $maxElement =null;

function PageNavigation($totalElement,$page,$maxElement)

{

if( $totalElement <= $maxElement )

$this->totalPages = 1;

else if( $totalElement % $maxElement == 0 )

$this->totalPages = $totalElement / $maxElement;

else

$this->totalPages = ceil( $totalElement / $maxElement );

if( $page > $this->totalPages || $page<=0 )

$page = 1;

if( $totalElement == 0 )

$this->threadStart = 0;

else

$this->threadStart = $maxElement * $page – $maxElement + 1;

if( $page == $this->totalPages )

$this->threadEnd = $totalElement;

else

$this->threadEnd = $maxElement * $page;

$this->initialMsg = $maxElement * $page – $maxElement;

$this->page=$page;

$this->totalElement=$totalElement;

$this->maxElement=$maxElement;

}

function PagingShowHTML($link)

{

if( $this->page == 1 )

echo “&lt;&lt;&nbsp;Start&nbsp; “;

else

echo “<a href=\”$link&page=1\”>&lt;&lt;&nbsp;Start</a>&nbsp; “;

if( $this->page == 1 )

echo “&lt;&nbsp;Prev&nbsp; “;

else

{

$prv=$this->page-1;

echo “<a href=\”$link&page=$prv\”>&lt;&nbsp;Prev</a>&nbsp; “;

}

for( $i = 1; $i <= $this->totalPages; $i++ )

{

if( $this->page == $i )

echo “[$i]&nbsp; “;

else

echo “<a href=\”$link&page=$i\”>$i</a>&nbsp; “;

}

if( $this->page == $this->totalPages)

echo “Next&nbsp;&gt;&nbsp; “;

else

{

$next=$this->page+1;

echo “<a href=\”$link&page=$next\”>Next&nbsp;&gt;</a>&nbsp; “;

}

if( $this->page == $this->totalPages )

echo “End&nbsp;&gt;&gt;&nbsp; “;

else

echo “<a href=\”$link&page=”.$this->totalPages.”\”>End&nbsp;&gt;&gt;</a>&nbsp; “;

}

}

?>

Thanks
(Faisal Sikder)

 
Leave a comment

Posted by on January 8, 2010 in PHP, Programming

 

Tags: , ,

Use TryParse Instead of Parse In C-Sharp

Exceptions are expensive to deal with, so we should always try to avoid it. By using TryParse instead of Parse we can avoid exception of FormatException.

Suppose one want to parse a date string to DateTime. If he uses Parse his source code will be like this.

DateTime objDate=new DateTime();

try{

objDate=DateTime.Parse(“01 january 2009”);

//do other works here

}Catch(FormatException){

//error coding

}

Or if one want to parse Integer string then code will be like this

int value=0;

try{

value=Int.Parse(“01”);

//do other works here

}Catch(FormatException){

//error coding

}

But if we use TryParse method then there is no need to use Try/Catch exception handling block. Because TryParse don’t throw any exception. General prototype of the TryParse method is as follows

bool TryParse(string, out targetObject);

Here we have to use out keyword with the targetObject indicating that it will get the string’s value after parse. And this method will return true if the parse operation was successful and false if the parse operation was not successful. So it doesn’t throw FormatException. Sample Code of TryParse method.

DateTime objDate=new DateTime();

if (DateTime.TryParse(“01 january 2009”, out objDate)){

//do other works here if successfully parse

}else{

//error handling

}

For Integer string

int value=0;

if(Int.Parse(“01”, out value)){

//do other works here if successful

}else{

//error handling

}

So it’s better to use TryParse in place of Parse.

You can also read: Use Convert Class Instead of Parse or TryParse method in C-Sharp(C#).NET

Thanks
(Faisal Sikder)

 
3 Comments

Posted by on January 4, 2010 in .NET, Programming

 

Tags: , ,

Some Useful Property Of Request Class To Get Server & Client IP And Other Info.

Here are some property of Request class which tells us about clients and server’s information such as client IP, Proxy, User agent, Server address etc.

  • To get Browser of the client use

Request.ServerVariables["http_user_agent"];


  • To get Client IP address ( if no proxy exist) use

Request.ServerVariables["remote_addr"];


  • To get DNS lookup of the IP address use

Request.ServerVariables["remote_host"];


  • If proxy exists use the following value to get client IP address

Request.ServerVariables["http_x_forwarded_for"];


  • The method used to call the page use

Request.ServerVariables["request_method"];


  • To get server’s domain name use

Request.ServerVariables["server_name"];


  • To get server’s port use

Request.ServerVariables["server_port"];


  • To get server’s software use

Request.ServerVariables["server_software"];

Thanks
(Faisal Sikder)

 
Leave a comment

Posted by on January 2, 2010 in .NET, Programming

 

Tags: , ,

 
Follow

Get every new post delivered to your Inbox.