Thursday, July 29, 2010

Disable links Via jQuery

<script>

$(function(){

//grab all a tags

$('a').each(function(){

//click on a link....

$(this).click(function(){

alert($(this).attr('href')+' is not available');

//disable link

return false;

});

});

});

</script>

Wednesday, July 7, 2010

PHP pagination class


[PHP]
class pagination
{
public function __construct()
{
}
public function calculate_pages(&$total_rows, $rows_per_page, &$page_num)
{
$arr = array();
// calculate last page
$last_page = ceil($total_rows / $rows_per_page);
// make sure we are within limits
$page_num = (int) $page_num;
if ($page_num < 1)
{
$page_num = 1;
}
elseif ($page_num > $last_page)
{
$page_num = $last_page;
}
$upto = ($page_num - 1) * $rows_per_page;
$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
$arr['current'] = $page_num;
if ($page_num == 1)
$arr['previous'] = $page_num;
else
$arr['previous'] = $page_num - 1;
if ($page_num == $last_page)
$arr['next'] = $last_page;
else
$arr['next'] = $page_num + 1;
$arr['last'] = $last_page;
$arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
$arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
return $arr;
}
function get_surrounding_pages($page_num, $last_page, $next)
{
$arr = array();
$show = 5; // how many boxes
// at first
if ($page_num == 1)
{
// case of 1 page only
if ($next == $page_num) return array(1);
for ($i = 0; $i < $show; $i++)
{
if ($i == $last_page) break;
array_push($arr, $i + 1);
}
return $arr;
}
// at last
if ($page_num == $last_page)
{
$start = $last_page - $show;
if ($start < 1) $start = 0;
for ($i = $start; $i < $last_page; $i++)
{
array_push($arr, $i + 1);
}
return $arr;
}
// at middle
$start = $page_num - $show;
if ($start < 1) $start = 0;
for ($i = $start; $i < $page_num; $i++)
{
array_push($arr, $i + 1);
}
for ($i = ($page_num + 1); $i < ($page_num + $show); $i++)
{
if ($i == ($last_page + 1)) break;
array_push($arr, $i);
}
return $arr;
}
}
[/PHP]

call

$p = new pagination();

$arr = $p->calculate_pages(70, 10, 1);

Display random image


[PHP]
# Init Array
$files = array();
# Get Folder
if($_GET['folder']) {
$folder = $_GET['folder'];
} else {
# Set Default Folder
$folder = '/img/';
}
# Set Full Path
$path = $_SERVER['DOCUMENT_ROOT'] . '/' . $folder;
# Open Directory
if($handle = opendir($path)) {
# Loop Through Directory
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file, -3) == 'gif' || substr($file, -3) == 'png' || substr($file, -3) == 'jpg' || substr($file, -4) == 'jpeg') $files[count($files)] = $file;
}
}
}
# Close Handle
closedir($handle);
# Init Random
$rand = rand(0, count($files)-1);
# Check Header Type
# GIF
if(substr($files[$random], -3) == 'gif') header("Content-type: image/gif");
# JPEG
elseif(substr($files[$random], -3) == 'jpg') header("Content-type: image/jpeg");
elseif(substr($files[$random], -4) == 'jpeg') header("Content-type: image/jpeg");
# PNG
elseif(substr($files[$random],-3) == 'png') header("Content-type: image/png");
# Read File
readfile($path . '/' . $files[$random]);
[/PHP]

Get text height, width

  1. define("F_SIZE", 8);
  2. define("F_FONT", "arial.ttf");
  3.  
  4.  
  5. function get_bbox($text){
  6.  return imagettfbbox(F_SIZE, 0, F_FONT, $text);
  7.  }
  8.  
  9. function text_height ($text) {
  10.  $box = get_bbox($text);
  11.  $height = $box[3] - $box[5];
  12.  return $height;
  13.  }
  14.  
  15. function text_width ($text) {
  16.  $box = get_bbox($text);
  17.  $width = $box[4] - $box[6];
  18.  return $width;
  19.  }

Monday, July 5, 2010

Text Resizing With jQuery


$(document).ready(function(){
// Reset Font Size
var originalFontSize = $('html').css('font-size');
$(".resetFont").click(function(){
$('html').css('font-size', originalFontSize);
});
// Increase Font Size
$(".increaseFont").click(function(){
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNum*1.2;
$('html').css('font-size', newFontSize);
return false;
});
// Decrease Font Size
$(".decreaseFont").click(function(){
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNum*0.8;
$('html').css('font-size', newFontSize);
return false;
});
});