Thursday, November 5, 2009

Pagination using PHP,Smarty And MySql

$sql="SELECT * FROM `fieldname1` WHERE is_active='Y' ORDER BY fieldmame2";
$rs=mysql_query($sql);
$num=mysql_num_rows($rs);
$i=0;
/*while ($row = mysql_fetch_array($rs)) {
$arr1[$j] = $row;
$j++;
}*/
////////////////////////////////////////////
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query

$rows = $num;

//This is the number of results displayed per page
$page_rows = 2;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$SqlQuery=$sql." LIMIT ". ($pagenum - 1) * $page_rows.",$page_rows";
$data_p=mysql_query($SqlQuery);

////////////////////////////////////////////
while($row1 = mysql_fetch_array($data_p)) {
$arr[$i] = $row1;
$i++;
}
if ($pagenum == 1)
{
}
else
{
$previous = $pagenum-1;
}
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;

}
$smarty->assign("arr",$arr);
$smarty->assign("pagenum",$pagenum);
$smarty->assign("previous",$previous);
$smarty->assign("last",$last);
$smarty->assign("next",$next);

Creating Thumbnail in PHP

Step 1. Create a folder and named  as createThumb.  Create a file named thumbnail_Class.php in the createThumb folder and paste the code given below into it.

<?

function createThumb($srcname,$destname,$maxwidth,$maxheight)
{
$oldimg = $srcname;
$newimg = $destname;


list($imagewidth,$imageheight,$imagetype)=@getimagesize($oldimg);

$shrinkage = 1;
if ($imagewidth > $maxwidth)
$shrinkage = $maxwidth/$imagewidth;
if($shrinkage !=1)
{
$dest_height = $shrinkage * $imageheight;
$dest_width = $maxwidth;
}
else
{
$dest_height=$imageheight;
$dest_width=$imagewidth;
}
if($dest_height > $maxheight)
{
$shrinkage = $maxheight/$dest_height;
$dest_width = $shrinkage * $dest_width;
$dest_height = $maxheight;
}
if($imagetype==2)
{
$src_img = imagecreatefromjpeg($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagejpeg($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}

elseif ($imagetype == 3)
{
$src_img = imagecreatefrompng($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagepng($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}
else
{
$src_img = imagecreatefromgif($oldimg);
$dst_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $imagewidth, $imageheight);
imagegif($dst_img, $newimg, 75);
imagedestroy($src_img);
imagedestroy($dst_img);
}
}
?>

Step 2. Create a file named index.php with in this folder and paste the code given below inside index.php. This is the file which you need to run from the browser. Place your image (e.g., grapes.jpg) to create thumbnail within the createThumb folder.

<?
include('thumbnail_Class.php');

$filePath    =    'large.jpg';
$destPath    =    'thumb.jpg';
$maxwidth    =    250;
$maxheight   =    150;
createThumb($filePath,$destPath,$maxwidth,$maxheight);

echo 'Thumbnail Created.';
?>

Calculate date difference in JavaScript

In this article you will learn how to calculate the number of days between two different date range in javascript.

<script language="javascript" type="text/javascript">

var start_date = "2009-10-01".split("-");
var end_date = "2009-10-06".split("-");


var date1 = new Date(start_date[0], start_date[1]-1, start_date[2]);
var date2 = new Date(end_date[0], end_date[1]-1, end_date[2]);

var ONE_DAY = (1000 * 60 * 60 * 24);
// The number of milliseconds in one day

// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();

var difference_ms = Math.abs(date1_ms - date2_ms);  // Calculate the difference in milliseconds
var date_diff = Math.round(difference_ms/ONE_DAY)+1;
// Convert back to days and return

alert (date_diff);

</script>

JavaScript Form Validation

The validate_form() function
<script type="text/javascript">
<!--

function validate_form ( )
{
valid = true;

if ( document.contact_form.contact_name.value == "" )
{
alert ( "Please fill in the 'Your Name' box." );
valid = false;
}

return valid;
}

//-->
</script>



Html Code:

<form name="contact_form" method="post"
action="#"
onsubmit="return validate_form ( );">


<h1>Please Enter Your Name</h1>

<p>Your Name: <input type="text" name="contact_name"></p>
<p><input type="submit" name="send" value="Send Details"></p>

</form>