kreoton web development

[PHP] building recursive menu with MySQL and without it

March 13th, 2008

Today I decided to share with you with two my function that I use in my code to build navigation menu for websites. First function is to build menu formated in PHP array. First we must format our menu in array like this way:

$menu = array(
	'Home' => $base.'/',
	'Videos' => array (
		'_link' => $base.'/videos/',
		'_submenu' => array (
			'Categories' => array (
				'_link' => $base.'/videos/categories',
				'_submenu' => array (
					'Fun videos' => $base.'/videos/categories/fun_videos',
					'Home videos' => $base.'/videos/categories/home_videos',
				),
			),
			'Popular' => $base.'/videos/popular',
			'Best rated' => $base.'/videos/best-rated',
		),
	),
	'Articles' => array (
		'_link' => $base.'/articles',
		'_submenu' => array (
			'Newest' => $base.'/articles/newest',
			'Archive' => $base.'/articles/archive',
		),
	),
	'Contact Us'=> $base.'/contact_us'
);

Variable $base is holding url to our website and is defined like this:

$base = 'http://www.example.com';

Read the rest of this entry »

XX ways to improve your PHP scripts execution time

February 12th, 2008

1. Many same kind of articles says that ‘require_once is expensive’. Ok we know that but what to do? Here is some ways how to include your php files not using ‘require_once’:

  • a) If you need to include object class file check if class not exists and then include:
  • <?php
    	if (!class_exists('testClass'))
    		require '../testClass.php';
    ?>
  • b) If you including simple script or template, define something in included file then check if it is not defined and then include it.
  • <?php
    //include.php
    define('INCLUDE', 1);
    #code ...
    ?>
    <?php
    //script where include file should be included
    if (!defined('INCLUDE'))
    	require 'include.php';
    ?>

2. If you can do not call functions in loop. Top programmers mistake is calling count() or sizeof() functions in for() loop. How to fix this you can find in every ‘XX ways to optimise your PHP code’ articles, but do not do like this:

<?php
	for ($i=0; $i<count($somearray); $i)
	{
		#code
	}
?>

You can fix this code like this:

<?php
	$counter = count($somearray);
	for ($i=0; $i<$counter; $i++)
	{
		#code
	}
?>

Or like this:

<?php
	for ($i=0, $counter = count($somearray); $i<$counter; $i++)
	{
		#code
	}
?>

Not only count() or sizeof() slows your code, I can say that every function called in loop slows yor script. Take a look at example bellow:

<?php
 
function build_home_path ()
{
	$sql = "SELECT value FROM settings WHERE key = 'path' LIMIT 1";
	$res = mysql_query($sql);
	$row = mysql_fetch_assoc($res);
	return $row['value'];
}
 
//build menu
$menu_items = array('home'=>'Home Page', 'about'=>'About Page');
foreach ($menu_items as $path => $title)
{
	echo '<li><a href="'.build_home_path().'/'.$path.'">'.$title.'</a></li>';
}
 
?>

How you see this code is absolutly good, and works fine, but can it be a little more faster? Yes it can be just look at build_home_path() function call. Function build_home_path() is called every loop interation, thats no good because it do just one thing it gets home path ( like http://www.kreoton.net/). Here is solution how to fix it:

<?php
//we can rewrite our loop contents in this way
$menu_items = array('home'=>'Home Page', 'about'=>'About Page');
$home_path = build_home_path();
foreach ($menu_items as $path => $title)
{
	echo '<li><a href="'.$home_path.$path.'">'.$title.'</a></li>';
}
?>

Thats all for this time come back for part 2.

Sublime Text another Windows alternative for TextMate

January 24th, 2008

Hello folks,

You probably heard about great text editor TextMate on MAC machines. Some time ago i found great alternative in Windows systems it is called Sublime Text. It is really great and quite powerful text editor with nice features:

  • minimap - great for fast code browsing, you can see more of your code;
  • automation - sniplet, macros and smart repeat is everything you need for fast coding;
  • multiselection - one more great feature;
  • beautiful user interface - it has unique user interface;

Here are some screens of this great editor:

You can split screen in 2 or more parts.

Editing with Sublime

Faster your coding with code sniplets

Editing with Sublime

Just great editor for fast coding.

Faster web development with free tools

December 7th, 2007

Web Server

You don’t really need a web hosting to start coding or designing your new project. You can run simple web server on your own machine. There many software to run server on your machine like:

  • EasyPHP - for small web projects or testing simple scripts (only Windows)
  • XAMPP - you can do with this anything that you can do with real web server (Windows, Mac, Linux and Solaris)
  • WAMP - another server for small projects.

I recommend to install XAMPP this one is best and would fit all your needs.

HTML/PHP editor

There are a lot of editors free to download from internet. I tried many of them:

  • Notepad++ - very popular text editor, with many add ons
  • PSPad - great editor for HTML PHP, there is FTP support and many other features
  • Intype - this is free analog of popular Mac OS text editor TextMate for windows

And many more other my recommendation is Intype, this editor is really has big power for coding.

Mini programs

These mini but powerful programs can help you a lot making design for your new web project.

  • Pixie - use this tool to pick up colors from any location of your screen just point your mouse pointer on any screen element and hit Ctrl+Alt+C and paste color code direct to CSS or PhotoShop
  • JRuler - want to measure on the screen then you need JRuler, with this tool you can measure everything on your screen or web browser

Testing your project

It is good to test your project design on all popular web browsers. Firefox and Opera is free for download. Internet Explorer is already on MS Windows, but there are two versions of Internet Explorer how to test project in both 7 and 6 (and even older) you would need multiple IE’s on your machine.

If you know more great free tools feel free to write of them in comments.

File saving on FTP with gEdit in Linux (Ubuntu)

October 29th, 2007

Last weekend i spend searching the net for good php editor in Linux. I downloaded about 10 of php editors: Quantana, Vim, Kate and others, but the best editor i found is gEdit. But it has one little problem in base configuration it can not save files directly to ftp but this can be sloved:

1. Hit Alt + F2 on your keybord
2. Enter gconf-editor
3. Navigate to  /apps/gedit-2/preferences/editor/save/
4. To  writable_vfs_schemes parameter add ftp

Now you can directly save files to your ftp.