Sunday, February 25, 2007

Ease your recursion

Ever wanted to have an easy way of building a recursive tree ?
Or something like that ?
Or even without understanding much of recursion or always thinking how was this done and so one ?

Well I eased your life a bit ;P

* Function that builds a category tree with recursion
* (it selects only id, [your specified field] and parent_id
* from the selected table)
* @param string $spacer
* @param string $table_name
* @param string $field_name
* @param integer $start_id
* @param string $link
* @param string $request_param
* @param string $link_style
function buildRecursionTree($spacer = '>>' , $table_name ,
$field_name , $start_id = '0' ,
$link , $request_param ,
$link_style) {
global $db;

$sql = "SELECT `id`,`$field_name`,`parent_id`
FROM `$table_name`
WHERE `parent_id`='$start_id'";
$result = $db->query($sql);
while (list($id,$name,$parent_id) = $db->fetch_array($result)) {
$spacer_next = " ".$spacer;
<a href="'<?="$link?">?<?=$request_param?>=<?=$id?>'
style='<?=$link_style?>' >
buildRecursionTree($spacer_next , $table_name ,
$field_name , $id ,
$link , $request_param ,

And now some sample usage ;)

$spacer = "<img src='images/arrow.png' border='0' />";
$table_name = "categories";
$field_name = "name";
$start_id = "0";
$link = "index.php";
$request_param = "category";
$link_style = "color: #000000; font-family: arial;
font-size: 11px;
text-decoration: none;";
buildRecursionTree($spacer , $table_name , $field_name ,
$start_id ,
$link , $request_param , $link_style);

Well easy enough huh !
See the sample result yourself ;)


Anonymous said...

Microsoft Office
Office 2010
Microsoft Office 2010
Office 2010 key
Office 2010 download
Office 2010 Professional
Microsoft outlook
Outlook 2010
Windows 7
Microsoft outlook 2010

Nike shox shoes said...

As to which more appropriate oneself, water like the person, changes in temperature from cicada.

Tiffany Jewelry said...

This is a great post,which has a lot of readers.
Such kind of post giving us various knowaledge,it worthy to read.It is so wonderful.

duckless said...

The original chi flat iron was released as a professional salon straightener. After gaining popularity on the market the cheap chi flat iron was later released for personal consumer use. If you are a professional stylist or someone who loves straightening their chi hair straightener before leaving for work the Chi original ceramic flat iron is one styling tool you cannot live without. Unlike other wholesale chi flat iron before its day the Chi model was developed with moist ceramic heat technology that does not burn or damage the wholesale chi hair straighteners.