Monthly Archives: January 2013

How to get the top most parent ( parent’s parent ) Recursively in php , when to use Recursive Function

My table Structure:

id parent_id menu name
1 0 Home
2 0 Gallery
3 0 Message
4 3 From Principal
5 3 From Guardian
6 2 Events
7 2 Students
8 6 Picnic
9 6 Sports

Major Requirement:

Input : 9 (Sports->Events->Gallery Bottom Up Parent Wise)

output: 2 (Gallery->Events->Sports Top Down )

Our Requirement is to make the function that will return us the top most parent of any menu item. Lets say i need to find the parent of Sports (id=9) menu which is simple i.e. the menu name with id 6 i.e. Events

Simply we will write the simple query: select menu_name from table_name where id=6

But it is not as simple as we have thought to find the top most parent of any menu . Because sometime we don’t know the depth i.e level at which the top most parent  is.

Here we actually don’t know the depth or level of top most parent . If you don’t know the level then use the recursive function it will save your cost of programming . Recursive function will solve your problem with magic:

Here is the Code :

 function get_most_root_of_any_id($id=NULL){

            return false;

        $result = $this->CI->db->get("table_name");

            return false;

      $record =  $result->first_row();

            return $id;
        return $this->get_most_root_of_any_id($record->parent_id);


This Example is made on the basis of Codeigniter , So please modify as per your need . More on RECURSION Enjoy…

When and How to use Recursion Function in PHP

Recursion function in php with example:
After reading many blogs and forums about recursion function , and along with the expert advise in recursion function . Not everyone but most expert say not to use the recursion function until necessary . What I mean to say is do not use the recursion function , if the same problem can be solved without using the recursion function . But at the same time performance matters  a lot . When you are iterating through the unknown structures better use the recursion but be confident in using the recursion. I am not 100% sure whether it is the exact answer or not. But the reason behind not using the recursion function as far as you can is:

First the recursion function by itself is difficult to use.

Second debugging becomes quite logical or difficult.

The true fact I faced is , I used the recursion function but later on , it was difficult for me to debug it. Any way does not matter whether you use recursion function in your code or not but the thing is you have to know about the recursion function as well.

What is recursion function?

A recursive function is just a function that calls itself.

Things to take care of Using Recursive Function

  1. Every recursive function must have the terminating condition

2. The function must call the function itself and Each recursive call must be different than the one before

Lets go with the Common practical example of the recursive function for calculating the factorial:

We have using this example since we started programming.. .. Flash back …

Factorial Calculation without using Recursion:


//iterative factorial function

function factorial($number) {

$result = 1;

while ($number > 0) {

print "result = $result, number = $number\n";

$result *= $number;



return $result;



Lets do the same example using the recursion function :


//recursive factorial function

function factorial($number) {

if ($number < 2) {

return 1;

} else {

return ($number * factorial($number-1));



print factorial(6);


You did not find that much intresting … ok lets go with the real practical example:

First I will make the table category with three fields id, parent_id and category_name.

First column is the primary key and parent_id holds the parent key and last  is the name  of the category.

Id Parent_id Category_name
1 0 Jacket
2 0 Mufler
3 1 Wind proof
4 3 Bossini
5 2 Silk
6 1 Water proof
7 5 Kashmiri silk

Fig: table category

From the above table it is clear that there is hierarchy in the category name i.e.

  1. Jacket(id=1)
    1. Wind Proof(id=3)
      1. Bossini(id=4)
  2. Water Proof(id=6)

And similar hierarchy in other category . Why am I explaining all these long story is .. ..

My requirement:  Make the function that will accept the id or the category name as the parameter and return all its child or siblings.

i.e. when id 1 is passed as an parameter , it must return the 3 , 4 and 6 as the ouput. The output can be an array or just in the string format but must be 3, 4 and 6 according to the above table.

i.e when 3 is passed as an parameter the output must be 4

The solution seems to be simple …. Quick solution that has arise to your mind is ..

Select id from category where parent_id=1  ???????? is it the right solution

I don’t think so .. it will give only the first child . But solution is simple you can just make another similar function that returns the child and next and next function till the level of hierarchy if the level of hierarchy is not defined , then you will face the simple problem but its possible . Here where I want to focus is RECURSIVE FUNCTION.

Ok here is the recursion function that will help you to solve the above problem:

function get_all_sub_caseTypeId($parent_id){



$query = $this->CI->db->get('CASE_TYPE');

foreach($query->result() as $rows){

$child_ids[$rows->CASE_TYPE_ID] = ($rows->CASE_TYPE_ID);


$child_ids[$rows->CASE_TYPE_ID] = $this->get_all_sub_caseTypeId($rows->CASE_TYPE_ID);



return $child_ids;


function has_child($parent_id){



$query = $this->CI->db->get('category);


return false;


return true;


This example is made on the basis of CODEIGNITER

Conclusion  for appropriate Use of Recursive Function:

Everything that can be done iteratively can be done through recursively and vice versa. However, just because it is possible does not mean it is always appropriate or even easy to implement.

Recursion has a valuable niche in computer science. It provides a more natural path through unknown structures, a more intuitive way of writing many algorithms, and a more elegant approach to writing some kinds of code. Hopefully this article has helped to establish recursion as an important and useful tool in your retinue of PHP tactics. Happy coding!

Magic Constant in PHP

PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.

There are eight magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it’s used on in your script. These special constants are case-insensitive and are as follows:

A few “magical” PHP constants
Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__ The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__ The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).

Remove Paragraph (p) tags – TinyMCE HTMLeditor


In order to remove the paragraph or p tags that automatically gets inserted by TinyMCE, open the tinymce.js file and include the following lines:

force_p_newlines : false,
force_br_newlines : true,
forced_root_block : ”,


Hi guys,

is there a way to disable the paragraph tag (<p>). I know that I can make a blank line if I push SHIFT + ENTER. But for me thats no real solution. I’m using TinyMCE successfully for some of my customers and those can’t handle with that solutions.

Is there a hack or anything else that I can disable that? I don’t want any <p> only <br /> should be made.

Thanks for answers ahead!



I don’t know how many times I told people to:
1) Check the manual
2) Search the forums
3) Once done 1 & 2, THEN post on the forums.

anyway, what you are looking for is force_br_newlines & force_p_newlines:

force_br_newlines : true, force_p_newlines : false

Solution 2:


                force_p_newlines: false