Archive for the ‘PHP’ Category.

PHP Set format date

Just a function to change the format of a mysql date to the one you want to.

function setDate($L_date,$L_dateFormat="dd-mm-yyyy"){//sets a date in a format
	if(strlen($L_date)>0){
		$L_arrTemp = split(" ",$L_date);
		$L_strDate = $L_arrTemp[0]; // 2007-07-21 year month day
		$L_arrDate = split("-",$L_strDate);// split date 
		$L_strYear =  $L_arrDate[0];
		$L_strMonth = $L_arrDate[1];
		$L_strDay = $L_arrDate[2];
 
		if($L_dateFormat == 'yyyy-mm-dd'){//default
		    return $L_arrTemp[0];
        }
		elseif($L_dateFormat == "dd-mm-yyyy"){//day month year
			$returnDate = $L_strDay."-".$L_strMonth."-".$L_strYear;
			return $returnDate;
		}
		elseif($L_dateFormat == "mm-dd-yyyy"){//month day year
			$returnDate = $L_strMonth."-".$L_strDay."-".$L_strYear;
			return $returnDate;
		}
	}
	else return false;
}

PHP sitemap.xml generator

Any web page should use a sitemap.xml, it’s really important for the bots to crawl your site properly.

But how to make it work using PHP?

This is the way I do it for Open Classifieds:

First of all I use to defines, for the path of the file, and when it would expire:

define(SITEMAP_FILE,"sitemap.xml.gz");
define(SITEMAP_EXPIRE,3600); //seconds

At the beginning of any of your scripts we check if the sitempa it’s expired, if it’s expired we generate another time the sitemap :
Continue reading ‘PHP sitemap.xml generator’ »

phpMyDB – Data base class for MySql

phpMyDB is not only a class handler for MySql written in PHP, it’s also has a query cache built in, a great debugger and is damn easy to use!

I’ve wrote this class just few weeks a go since I need it for the new version of Open Classifieds (coming soon I promise, many changes).

This class uses fileCache class to be able (if you want to) to cache the query result in a really simple way (transparent to the programmer).

First Download

Usage with examples:

Constructor:
DB Constructor – connects to the server and selects a database

$ocdb = new phpMyDB(DB_USER, DB_PASS, DB_NAME,DB_HOST,DB_CHARSET);

In case of error will print using the private function print_error

Debugger:
Logs all the connection or action inside the class, you can set it on whenever you want.

$ocdb->setDebug(true);
$ocdb->returnDebug();

We set it on, and at the end of our script(or whenever you want) we should returnDebug, by default in “HTML”, but can return as an “array”.
Also you can add new “logs” with $ocdb->addLog(“any string”);

Activate cache:
Uses fileCache class. As parameter we have:
1: true or false to activate or deactivate the cache, by default false
2: Time in seconds by default 3600 (10 seconds expires in the example)
3: Path for the cache, by default cache/

$ocdb->setCache(true,10);

Normal query:
Just performs a normal query, but keeps the query counter and logs the action if debugger it’s on

$result =$ocdb->query($query);
if (mysql_num_rows($result)){
     while ($row=mysql_fetch_assoc($result)){
          echo 	$row['title'];
     }
}

getRows
Returns in rows the target query. If cache is activated automatically cache the result as an array.
Parameters:
1: Query to return values
2: Returning as “assoc” (default), “row” , “object” ,”value” (returns first field form the query)
3: Cache type “cache” or “APP” by default “cache”
Example: Cached query, returning the row as an object (cache must be activated):

$result=$ocdb->getRows($query,"object");
if ($result){//more than 1 result
	foreach ( $result as $row ){
           echo $row->title;
       }
}

Continue reading ‘phpMyDB – Data base class for MySql’ »

PHP Class for better cache – fileCache

Last days I’m working making many changes in Open Classifieds and one of them is this new class to handle the cache.

Few days a go I wrote about a cache class and longer a go about application variables for php. This is a mix of both in just one powerful class.

Explanation:

In this class we have 2 different kinds of cache.

First the normal file cache, where we store values in a single file. This is good for example to cahe an entire page.

Second we have an APPLICATION kind integrated in the cache. This means that whatever you store in the APP cache it would be kept in the same file as all the other APP. Really useful to store small amount of data, for example menus, counters etc… Remember that this file is loaded everytime yo create a new object fileCache, you need to be careful to not store many things on it.

Usage:

$cache= new fileCache();
//$cache= new fileCache(180);//second
//$cache= new fileCache(180,'cache/');//seconds and path
 
////cahe application in the same file!!!
$test= $cache->APP("test");
if (!$test){
	$cache->APP("test","value test application cache<br />");
} else echo $test;
$cache->APP("test2","value test 2 application cache<br />"); //same file as variable test
 
//end cache app
 
//normal cache in different files
$test = $cache->cache("test");//getting values from cache
if (!$test) {	//not value from cache found
	$cache->cache("test", "value test normal cache<br />");	//save cache			
}else echo $test;	
//end normal cache
 
$cache->deleteCache(60);//deletes any cache older than X seconds xD

The Class: (Download)
Continue reading ‘PHP Class for better cache – fileCache’ »

Add PDF files inside other PDF in PHP

Scenario:

We have some data sotred in a data base that must be returned as PDF.

Sounds easy but what if I tell you that between that data you need to attach other PDF files?

It makes everything more complicated. We already saw how to convert from Word to PDF in PHP and Concatenate PDF in PHP but that’s not enough…

First download in your work space TCPDF and FPDI.

Then you can use this class I did for this.
Continue reading ‘Add PDF files inside other PDF in PHP’ »

Follow me