kreoton web development

Archive for the ‘Coding’ Category

Digg.com style pagination function

Tuesday, June 5th, 2007

I introduce nice Digg.com style pagination function. This pagination function is very flexible and easy to implement in all kind of scripts. All you have to do is to set current page parameter, total pages parameter and pagination format. I will give you an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?
//include paginate funtion
include('paginate.php');
 
//get total pages number
$total = $db['result']['total_pages'];
 
//get curent page
$page = $_GET['p'];
 
//formate pagination layout
 
$format_pg = array (
	'start_tag'	=> '<ul>',
	'close_tag'	=> '</ul>',
	'a_open'	=> '<li>',
	'a_close'	=> '</li>',
	'a_curent'	=> ' style="color:red"',
	'url_q'		=> '?p=%d',
	'lang_next'	=> 'NEXT',
	'lang_previous'	=> 'PREVIOUS',
	'a_space'	=> '...',					
	);
 
//finaly print pagination
 
echo paginate($page, $total, $format_pg);
 
?>

Paginate function is free. To download paginate function hit following link:

Download paginate Rate it On HotScripts.com

Smarty included file caching

Wednesday, May 30th, 2007

Smarty is very good templating system for every PHP application. Once I coded one application using Smarty and Apache mod rewrite. I had one template file i witch i included other template files. After some time my project had very high traffic so i needed to on cache. You probably know that Smarty does not have cache future for included file, so i googled for some time and found some useful smarty hack. I added this hack to smarty core files and wow it worked, all i had to do is just add variable to template file inclusion in example:

1
{include file='includedfile.tpl' __cache__=120}

Where __cache__ shows that i start cache for 120 second everything worked fine until i browsed to other pages. Main bug of this hack is you can not use paging on your application. To solve this problem i edited smary code and added __id__ variable to Smarty included template files. It works very simple in example:

index.php file:

1
2
3
4
5
6
7
<?php
if ($_GET['page']) {
$smarty->assign('page', $_GET['page']);
//getting dynamic content
}
$smarty->display('main.tpl');
?>

main.tpl

1
2
3
4
5
6
7
8
<html>
<head>
<title>{$pagetitle}</title>
</head>
<body>
{include file='dynamiccontent.tpl' __cache__=3600 __id__=$page}
</body>
</html>

Variable __id__ assigns in witch dynamic content page I am.

You can download hacked version of Smarty by clicking of following link http://kreoton.net/downloads/samrty.rar