Adjacency List Model and Nested Set Model for hierarchical data in MySQL

I am working on classifieds website these days. These kind of websites are getting famous these days and they also have average kind of traffic. while working on database designing for websites like classifieds / auction / web directories, we have to take care of few things like

1. This is a tree structure which may have n levels.
2. And there has to a straight way to traverse from top to bottom and from bottom to top.
3. Strong tree traversal algorithm.
4. Retrieving a Single Path between two nodes.
5. Depth of nodes and depth of sub-tree and lots of other things.

For this we need a strong table design and relationship structure. In MySQL we can manage this hierarchical data with two different models.

1. The Adjacency List Model
2. The Nested Set Model

The Adjacency List Model is a very basic table structure which maintain the column lets say "parent" to maintain the parent child relationship. (It actually hold the PK of parent node). But this model again has some disadvantages, described in brief on the link at the bottom.

so,I came across a another hierarchical model called as nested set model. In this type of hierarchical data model parent nodes envelope there children. This is based on algorithm called as preorder tree traversal algorithm. This is a very good concept while working on tree structure tables.

Source of information : http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

new php.ini directives in php5

PHP 5 came up with some new directives in php.ini file. I am writing about all new directives introduced in PHP 5. Heres the list...

1) mail.force_extra_parameters : Force the addition of the specified parameters to be passed as extra parameters to the sendmail binary.

2)register_long_arrays :

configures the php to allow or disallow the registration of long arrays $HTTP_*_VARS

possible values : On / Off

Default:off

Recommended : If you are not using them then its recommended to keep them off for some performance reasons.

3) session.hash_function :
This directive allows us to configure the way of php's session id generation algorithm.
0: MD5 (128 bits)
1: SHA-1 (160 bits)

possible values : 0 / 1

Default : 0

4)session.hash_bits_per_character :
This directive allows us to define how many bits are stored in each character when converting the binary hash data to something readable.

possible values :
'4' (0-9, a-f)
'5' (0-9, a-v)
'6' (0-9, a-z, A-Z, "-", ",")

default : 5

5) zend.ze1_compatibility_mode :
This configuration directive allows us to enable or disable the compatibility with zend engine 1 ( which is a php 4 engine). Its set to Off as default and turning it on may affect cloning,casting. Objects are also passed by value instead of pass by reference.

possible values : On / Off

Standard PHP Library extension download introduction

Standard PHP Library

php5 is focused on object oriented strict syntax and object oriented capabilities. Standard php library (SPL) made this job very east. SPL provides all standard set of interfaces for PHP5 to simplify the data access and traverse the aggregate structures. These structures can be anything like directory traversal,database,arrays kind of structures.

simple words : SPL is nothing but a bunch of classes n interfaces which are developed to solve the common problems.


Lets take some examples,
Suppose that you are working on one of the following tasks
1. Database results.
2. Directory traversing n display.
3. Reading file contents.

In all of these tasks we have to iterate the results. Like If we fetch data from database then we have to iterate the result set in order to fetch complete data from result set and same thing with file read and directory traversal task also. In all these cases the programming and coding is same ( fetch the main resource and iterate through the result set), but we have to use different set of php functions to handle different resources( for database we use mysql_fetch_array for directory traversal we use readdir() and so on..)

The classes and interfaces in SPL makes this kind of traversal easy. Lets start with simple DirectoryIterator class.

SPL can work with following data structures.
Arrays
objects
heap
doubly link list
stack
Queues

It provides set of iterators to traverse the objects. List of few iterators
ArrayIterator
IteratorIterator
DirectoryIterator
CachingIterator
RecursiveIteratorIterator
RecusrsiveCachingIterator
RecursiveDirectoryIterator
SimpleXMLIterator
FilesystemIterator
FilterIterator
GlobIterator
ParentIterator
LimitIterator

SPL also provides some Interfaces
SeekableIterator interface
Countable interface

SPL also provides some exceptions and functions. click to read complete details of SPL

--------------------------------------------------------------------------------
Here is one simple example of DirectoryIterator

DirectoryIterator
This is used to iterate through any directory as simple as that.
// sample php code to iterate through any server directory
foreach(new DirectoryIterator('path/to/dir') as $element):
echo $element;
endforeach;
?>
sample Directory iteration with SPL

language construct in php echo print isset unset empty include

What is language construct?
Language constructs are built in to php and they can be used like a function. But the basic difference between them is the language constructs can't return the anything.

language constructs can be used with or without parentheses.

List of some language constructs in php
echo(), print(), isset(), unset(), empty(), include(), require(),array(),list()

how to avoid direct access to file protect files in php

If you don't want users to access some files directly from browser then you can simply use following php script on top of such files.

// this is first code block
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME']))
{
// tell people trying to access this file directly goodbye...
exit('This file can not be accessed directly...');
}
/ protected code below this.
?>

clearstatcache filesystem function clears cached file status

PHP maintains the status of files in internal cache. If we are using any of the file functions like is_file , is_readable , is_writable, filectime() etc then php stores the information returned by the applied function and if we try to use that same function on the same file in same script then php uses the cached value even if the file status changes in between two calls. But as any php script time is small only, but we have to keep this in mind while working on long running scripts. So if you are really working on such kind of script then you can use the php built in function to clear the php's internal cached information about the files.

Syntax
void clearstatcache(boolean $clear_real_path_cache(default FALSE),string $file_path)


first parameter : To clear real path cache or not.
second parameter : Use only when first parameter is TRUE.


some functions affected by clearstatcache()



programming tips tricks performance improvement

Here are some of the Tips and tricks which can improve the speed and performance of your php script and may save your resources.(NOTE:I haven't tested all these tips but these are wildly used to improve the performance of the php script.)

Tips while working with strings

string concatenation

echo 'php', 'developer'; // saves overhead time for string concatenation.
echo 'foo' . 'bar'; // slower..


Interpolation

$variable = 'this is '.$another_var.' with me'; // faster
$variable = "this is $another_var with me"; // slower

Tip:
Simply use the single quote instead of double quote.
Avoid the string concatenation with (.) if possible.

 
 
 

typo3 cms templates tutorial

PHP news

php freelancer mumbai India