Wednesday, August 18, 2010

What is Pagination?

Think about if you have a mysql table with a thousand rows, and you want to allow the user to browse through the entire table. Displaying all the records in that table in one page would not be a good idea. Instead you should break the table up into smaller parts and let the user navigate through it. This is what pagination is, it allows you to break up large results from a database query, and displays a better navigation for the users.

The following code is a quick and dirty example of php/mysql pagination but if you are familiar with CSS, you can easily style the way the page navigation looks.

if(!empty($_GET["start"])){
$start = $_GET['start'];// To take care global variable if OFF}else{
$start = 0;
}
if(!(
$start > 0)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start - 0);$limit = 5; // No of records to be shown per page.$whathis = $eu + $limit;$back = $eu - $limit;$next = $eu + $limit;
// to check the total number of records$query = mysql_query(" SELECT * FROM ") or die (mysql_error());$total_rows = mysql_num_rows($query);
//select the record with limitation$query = mysql_query(" SELECT * FROM limit $eu, $limit ") or die (mysql_error());

//code for previousif($back >=0) {
echo
"PREV ";
}

//code for the number of page with links$i = 0;$x = 1;
for(
$i=0;$i < $total_rows;$i=$i+$limit){
if(
$i != $eu){
echo
"$x ";
}else {
echo
"$x";
}
// Current page is not displayed as link and given font color red
$x = $x+1;
}
//code for nextif($whathis < $total_rows) {
echo
"NEXT";
}
?>

CREATE IMAGE THUMBNAILS USING PHP and GD

The following PHP code will create thumbnail images on the fly and since it uses the PHP GD2 library, you will need an installation of PHP with at least GD 2.0.1 enabled.. Some says that the only drawback to using PHP for image creation is that the thumbnails don’t look as good as thumbnails created in Photoshop or GIMP but this simple block of code would prove them wrong...
("Content-type: image/jpeg");$image = $_GET['img'];
if(!isset($w) && !isset($h)){$w = 100; //default width if $w is not set$h = 125; //default height if $h is not set}
$x = @getimagesize($image);// get image size$sw = $x[0];// width$sh = $x[1];// height$im = @ImageCreateFromJPEG ($image) or // Read JPEG Image$im = false; // If image is not JPEG

if (!$im)readfile($image);// return the actual message if error occurs.else {// Create the resized image destination
$thumb = @ImageCreateTrueColor ($w, $h);// Copy from image source, resize it, and paste to image destination
@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);// Output resized image
@ImageJPEG ($thumb);
}
?>


JAVASCRIPT EQUIVALENT FOR PHP FUNCTIONS

Found this from Kevin van Zonneveld site and I just feel to post it here. List of PHP Functions are below: