TheMarshal Posted January 23, 2013 Share Posted January 23, 2013 (edited) Hi all 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! Thankx all in advance Edited January 23, 2013 by TheMarshal Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/ Share on other sites More sharing options...
trq Posted January 24, 2013 Share Posted January 24, 2013 The explanation of your issue leaves a lot to be desired. It also seems unrelated to this boards topic. I suggest you try again, maybe even with some code. Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1407837 Share on other sites More sharing options...
TheMarshal Posted January 24, 2013 Author Share Posted January 24, 2013 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 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; } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1407873 Share on other sites More sharing options...
trq Posted January 24, 2013 Share Posted January 24, 2013 You need to seriously look at using SQL joins. I don't want to be harsh, but, that is probably the worst code I have seen in a few years. You might also want to look at recursion (in php) to provide an easy solution to your problem. Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1407886 Share on other sites More sharing options...
TheMarshal Posted January 24, 2013 Author Share Posted January 24, 2013 (edited) 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 January 24, 2013 by TheMarshal Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1407891 Share on other sites More sharing options...
Barand Posted February 4, 2013 Share Posted February 4, 2013 (edited) Recursion : see http://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/?do=findComment&comment=1410085 Edited February 4, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1410085 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Hehe, nice one, barand. You actually had me going for a couple of secs there. Thought the browser wasn't registering the clicks. 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. Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1410106 Share on other sites More sharing options...
Barand Posted February 4, 2013 Share Posted February 4, 2013 I thought it an apt definition of recursion Quote Link to comment https://forums.phpfreaks.com/topic/273565-how-to-get-initiate-parent-id/#findComment-1410110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.