Simple php pagination

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.
<?php 
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 <tablename> ") or die (mysql_error());
$total_rows     mysql_num_rows($query);

//select the record with limitation
$query         mysql_query(" SELECT * FROM <tablename> limit $eu, $limit ") or die (mysql_error());

//code for previous
if($back >=0) {
echo 
"<a href='yourpage.php?start=$back'><font face='Verdana' size='2'>PREV</font></a>&nbsp;&nbsp;";
}

//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 
"<a href='yourpage.php?start=$i'><font face='Verdana' size='2'>$x</font></a> ";
}else { 
    echo 
"<font face='Verdana' size='4' color=red>$x</font>";
// Current page is not displayed as link and given font color red

$x    $x+1;
}
//code for next
if($whathis $total_rows) {
echo 
"<a href='yourpage.php?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";
}    
?>

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...

<?php
header 
("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$im0000$w$h$sw$sh);
// Output resized image
   
@ImageJPEG ($thumb);
}
?>

save the above code as image_thumb.php.

Usage:
<--without specifying the width and height-->
<img src="http://www.example.com/image_thumb.php?img=example.jpg" />

<--with height and width-->
<img src="http://www.example.com/image_thumb.php?img=example.jpg&w=200&h=200" />

How to create a dynamic bar graph using PHP and GD

Create a bar graph using PHP:  code is here

How to create a dynamic line graph using PHP and GD

Create a line graph using PHP:  code is here

Simple PHP Image Watermark

Have you ever wanted to add an alpha-transparent watermark to an image that you post on your website?
Here is a simple PHP script that watermarks JPEG and PNG images.

<?php
function watermark($sourcefile, $watermarkfile) {
# $sourcefile = Filename of the picture to be watermarked.
# $watermarkfile
= Filename of the 24-bit PNG watermark file.
//Get the resource ids of the pictures
$watermarkfile_id = imagecreatefrompng($watermarkfile);
imageAlphaBlending($watermarkfile_id, false);
imageSaveAlpha($watermarkfile_id, true);

$fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

switch(
$fileType) {
case(
"gif"):
$sourcefile_id = imagecreatefromgif($sourcefile);
break;

case(
"png"):
$sourcefile_id = imagecreatefrompng($sourcefile);
break;

default:
$sourcefile_id = imagecreatefromjpeg($sourcefile);
}

//Get the sizes of both pix
$sourcefile_width = imageSX($sourcefile_id);
$sourcefile_height = imageSY($sourcefile_id);
$watermarkfile_width = imageSX($watermarkfile_id);
$watermarkfile_height = imageSY($watermarkfile_id);

$dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
$dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

// if a gif, we have to upsample it to a truecolor image
if($fileType == "gif") {
// create an empty truecolor container
$tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

// copy the 8-bit gif into the truecolor image
imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);// copy the source_id int
$sourcefile_id = $tempimage;
}
imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height);

//Create a jpeg out of the modified picture
switch($fileType) {
// remember we do not need gif any more, so we use only png or jpeg.
// See the code above to see how we handle gifs
case("png"):
header("Content-type: image/png");
imagepng ($sourcefile_id);
break;

default:
header("Content-type: image/jpg");
imagejpeg ($sourcefile_id);
}
imagedestroy($sourcefile_id);
imagedestroy($watermarkfile_id);
}
watermark("main.jpg","watermark.png");
?>

Sample output below...No comment on the picture pls... its sacred y know..
main.jpg
watermark.png
watermarked output jpeg

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:

abs

acosh

acos

addslashes

array

array_change_key_case

array_chunk

array_combine

array_count_values

array_diff

array_diff_assoc

array_diff_key

array_fill

array_fill_keys

array_filter

array_flip

array_keys

array_key_exists

array_map

array_merge

array_merge_recursive

array_pad

array_pop

array_product

array_push

array_rand

array_reduce

array_reverse

array_search

array_shift

array_slice

array_sum

array_unique

array_unshift

array_values

array_walk

array_walk_recursive

asinh

asin

atanh

atan

base64_decode

base64_encode

basename

base_convert

bin2hex

bindec

call_user_func_array

ceil

checkdate

chop

chr

chunk_split

compact

constant

cosh

cos

count

count_chars

crc32

create_function

date

decbin

dechex

decoct

defined

define

deg2rad

dirname

echo

empty

end

explode

exp

filesize

file

file_exists

file_get_contents

floatval

floor

fmod

function_exists

getdate

getrandmax

get_class

get_headers

get_html_translation_table

get_included_files

hexdec

htmlentities

htmlspecialchars

htmlspecialchars_decode

html_entity_decode

http_build_query

hypot

implode

include

include_once

intval

in_array

ip2long

isset

is_array

is_bool

is_finite

is_infinite

is_int

is_nan

is_null

is_numeric

is_object

is_string

join

krsort

ksort

lcg_value

levenshtein

log10

log

long2ip

ltrim

max

md5

md5_file

microtime

min

mktime

mt_getrandmax

mt_rand

nl2br

number_format

octdec

ord

parse_str

pi

pow

preg_quote

printf

print_r

quotemeta

rad2deg

rand

range

require

require_once

reset

round

rsort

rtrim

serialize

setcookie

sha1

sha1_file

shuffle

sinh

sin

sizeof

sleep

sort

soundex

split

sprintf

sqrt

strcasecmp

strchr

strcmp

stripos

stripslashes

strip_tags

stristr

strlen

strnatcmp

strncasecmp

strpbrk

strpos

strrev

strripos

strrpos

strstr

strtolower

strtoupper

str_ireplace

str_pad

str_repeat

str_replace

str_rot13

str_split

substr

substr_count

tanh

tan

time

trim

ucfirst

ucwords

unserialize

urldecode

urlencode

utf8_decode

utf8_encode

var_export

wordwrap