Jump to content

how to get initiate parent id


TheMarshal

Recommended Posts

OK, thnax for trying help me

 

I have many of parent's id like 1,2,3,4,5,6..,109 .

 

those parents have many childrens like the parent no 3 have 200,201,202,....etc

 

also those childrens have many other childrens like the subparent no 201 have 500,501,505,509....etc

 

so now for example when i got to page 505 this page have details of this number and have beside some advertisements, those advertisements shows by the parent id and the initiate parent of this number 505 is 3 .

 

and if i in page 201 that give me initiate parent 3.

 

because 505 has parent id 201 and 201 has parent id 3 so i want 3 comes directly when i in page 505 or 201 and so on

 

i hope that help to explane what i mean :sweat:

 

about code i tried with this code but look like not perfect with my issue

 


 $adsnourl = @intval($_REQUEST['ads']);
     $QueryAds = @mysql_query("SELECT * FROM ".CATEG." WHERE cat_parent=0");
     $allAds = array();
     while ($rowparent = @mysql_fetch_assoc($QueryAds)) {
        $parentno = $rowparent['cat_id'];
        $allAds[] = $parentno;
     }
        if (in_array($adsnourl,$allAds)) {
          $adsno = $adsnourl;
        } else {
           $firstParentQuery = @mysql_query("SELECT * FROM ".CONT." WHERE cont_id=".$adsnourl."");
           $firstParentFetch = @mysql_fetch_assoc($firstParentQuery);
           $firstParentrow   = $firstParentFetch['cont_parent'];


               if (in_array($firstParentrow,$allAds)) {
                   $adsno = $firstParentrow;
                } else {
                     $secondParentQuery = @mysql_query("SELECT * FROM ".CATEG." WHERE cat_id=".$firstParentrow."");
                     $secondParentFetch = @mysql_fetch_assoc($secondParentQuery);
                     $secondParentrow   = $secondParentFetch['cat_parent'];



                  if (in_array($secondParentrow,$allAds)) {
                     $adsno = $secondParentrow;
                   } else {
                     $thirdParentQuery = @mysql_query("SELECT * FROM ".CATEG." WHERE cat_id=".$secondParentrow."");
                     $thirdParentFetch = @mysql_fetch_assoc($thirdParentQuery);
                     $thirdParentrow   = $thirdParentFetch['cat_parent'];
                   if (in_array($thirdParentrow,$allAds)) {
                      $adsno = $thirdParentrow;
                   } else {
                       $fourthParentQuery = @mysql_query("SELECT * FROM ".CATEG." WHERE cat_id=".$thirdParentrow."");
                       $fourthParentFetch = @mysql_fetch_assoc($fourthParentQuery);
                       $fourthParentrow   = $fourthParentFetch['cat_parent'];
                     if (in_array($fourthParentrow,$allAds)) {
                        $adsno = $fourthParentrow;
                     } else {
                       $adsno = 0;
                     }
                   }
                   }


             }
        }

Sounds good >> that is probably the worst code I have seen in a few years ... not whole life ^_^ because i'm still novice in programming so i have hope to learn.

 

I will search for recursion in php & joins then i will see if that help or not!

 

Thank you

  • 2 weeks later...

Hehe, nice one, barand. You actually had me going for a couple of secs there. Thought the browser wasn't registering the clicks. :P

 

Anyway, TheMarshal: If you read up in the PHP manual on user defined functions, you can see examples of recursive functions in there. Along with a few warnings about using recursion. Recursion is very powerful, but also easy to mess up in such a way that it will crash your script. So better make sure you've dotted all of your I's and crossed all of your T's.

 

PS: I took the liberty of moving this thread to the "PHP Coding Help" section, as this is more of a general PHP help topic than a maths help topic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.