N-level menu using recursion

Recently I needed to come up with a code that would help me create a n-level menu and breadcrumb for that. So for that I wrote this code which works with great ease and provides huge flexibility. Also I kept it really low so that it is easy to embedded it in any kind of design. This is just the half part of my code which prints the menu.

Function:

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
30
$table	= 'menu';
 
function showMenu($ulclass, $liclass, $father) {
	$showmenu_query = "select * from $table where menu_father='" . $father . "' order by menu_order";
	$exec_showmenu = mysql_query($showmenu_query);
 
	if ( !$exec_showmenu) {
		echo "Query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
		return false;
	}
 
	$toAppend = '<ul class="' . $ulclass . '">';
 
	while($row_showmenu=mysql_fetch_assoc($exec_showmenu)) {
		$toAppend.= '<li class="' . $liclass . ' &nbsp; '. $row_showmenu['menu_class'] . '">';
		$toAppend.= '<a href="' . stripslashes($row_showmenu['menu_slug']) . '">' . stripslashes($row_showmenu['menu_name']);
		$toAppend.= '</a>';
 
		$submenu_query="select * from $table where menu_father='" . $row_showmenu['menu_id'] . "'";
		$exec_submenu = mysql_query($submenu_query);
		if( mysql_num_rows($exec_submenu)>0 ) {
			$toAppend.= $showMenu($ulclass, $liclass, $row_showmenu['menu_id']);
		}
 
		$toAppend.= '</li>';
	}
 
	$toAppend.= '</ul>';
	return $toAppend;
}

Continue reading “N-level menu using recursion”

Redirecting site or pages to a new domain or url aka 301 redirections

I learnt about 301 redirection when I moved my wordpress site from earlier domain to this new domain.

Htaccess redirect is better than the meta refresh or redirect tag because there is no delay as the browser reads the .htaccess file first. Here is how it works.

Go to your site’s root folder, download the .htaccess file to your local computer and edit it with a plain-text editor (ie. Notepad). If you are using FTP Client software and you don’t see any .htaccess file on your server, double check your setting and make sure you have turn on invisible / system files. Continue reading “Redirecting site or pages to a new domain or url aka 301 redirections”

Logging errors to file in PHP

PHP offers simple but effective solution to log all errors to a log fiie. On all production web server its a must that you turn off displaying error to end users via a web browser. Remember PHP gives out lots of information about path, database schema and all other sort of sensitive information. You are strongly advised to use error logging in place of error displaying on production web sites. The idea is quite simple -only developer should see php error log.

The way I choose to do it is by having a common config.php file containing this code and included in the first line of every page.

1
2
3
4
5
6
7
8
// Display no errors in runtime pages
ini_set("display_errors" , "0"); 
 
// Log errors to file
ini_set("log_errors" , "1");
 
// Error log file path and name
ini_set("error_log" , "logs/Errors.log.txt");

Good luck!!!