Jump to content

Recommended Posts

Hi all :happy-04:

 

May anyone tell me how to get first parent id from the child's id.

 

like 388 have parent id : 95 and 95 have initiate parent id is : 2, I want to get 2 when i get the value 388 from url?

 

i hope i explaned the question! :sweat:

 

Thankx all in advance ::)

Edited by TheMarshal
Link to comment
https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/
Share on other sites

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

Edited by TheMarshal
  • 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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.