Posts tagged ‘php’

Cheat sheets are blessed invention; a handy reference for those times when your code calls for something common, but for the life of you, you can’t remember the details.  Usually I just Google whatever I need, but I’ve came across these cheat sheets and have them handy.

Magento Events:

http://activecodeline.com/wp-content/uploads/2009/03/magento-events-cheat-sheet.pdf

PHP Cheat Sheet:

http://www.gosquared.com/images/help_sheets/PHP%20Help%20Sheet%2001.pdf

WordPress Cheat Sheet:

http://www.gosquared.com/images/help_sheets/WordPress-Help-Sheet.pdf

…and a whole crapload more can be found on GoSquared’s site.

While using Magento Connect Manager, you may come across the following error:


Error: Please check for sufficient write file permissions

Your Magento folder does not have sufficient write permissions, which this web based downloader requires.

If you wish to proceed downloading Magento packages online, please set all Magento folders to have writable permission for the web server user (example: apache) and press the “Refresh” button to try again.


This means that Magento does not have the proper permissions on the web server to be able to updates itself (permissions such as write).  Unfortunately, you can’t do this via FTP (although you may think you can).  FTP just fakes permissions.  You will need to change the file permissions for the following folders, subfolders, and files to 777:

  • magento/app/etc
  • magento/app/code
  • magento/var
  • magento/media

Whilst reading some of the PHP RSS feeds, I saw this handy tutorial for some quick and dirty PHP caching. This obviously doesn’t work well for things like GET, POST, etc…but if you are handling categories, or frequently requested data, this may be a great solution.

Here is the full tutorial, but the basic code is:

// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->\n";
exit;
}
ob_start(); // start the output buffer

// Your normal PHP script and HTML content here

// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

Usage

Pass an array of arrays, and will return an HTML table. This is useful for returning database records in a “pretty” format. I could never find anything that would work quite the way i wanted it to, so i wrote my own.

$arr = array();
$arr[] = array('Name','SomeValue');
$arr[] = array('Greg','12345');
echo renderArrayToTable($arr,'true');

Code

function renderArrayToTable($array_of_arrays,$first_row_headers=false) {
	$rstr = '';
	$i=0;
	$rstr .= '<table border="1" cellspacing="0" cellpadding="5">';
	foreach ($array_of_arrays as $arr) {
		if (is_array($arr)) {
			$pre = (($first_row_headers)&&($i==0))?'<th>':'<td>';
			$post = (($first_row_headers)&&($i==0))?'</th>':'</td>';
			$rstr .= '<tr>';
			foreach ($arr as $key => $value) {
				$rstr .= $pre.$value.$post;
			}
			$rstr .= '</tr>';
		}
		$i++;
	}
	$rstr .= '</table>';
	return $rstr;
}