ricky spires Posted January 16, 2012 Share Posted January 16, 2012 hello. i have a foreach loop thats pulling out information that i dont want. how do i stop it. this is the loop //GET THE TEXT $PCa = PageContent::find_by_pageContID($PCidA); foreach ($PCa as $PCas){ $title = $PCas->title; $link = $PCas->link; echo $title; echo "<br/>"; } the loop sits inside this function which has more loops function listNavText($PHGlidA, $PHLobA, $PHLodA){ //GET THE LANGUAGE $langBS = basicSettings::find_by_id(1); $langID = $langBS->language_id; //GET THE LIST ORDER $PHLoa = PhLists::find_by_order($PHGlidA, $PHLobA, $PHLodA); foreach ($PHLoa as $PHLoas){ $PHLidA = $PHLoas->id; //GET THE LIST TEXT IDS $PCba = PCbridge::find_by_list($PHLidA, $langID); foreach ($PCba as $PCbas){ echo $PCidA = $PCbas->pageContent_id; //GET THE TEXT $PCa = PageContent::find_by_pageContID($PCidA); foreach ($PCa as $PCas){ $title = $PCas->title; $link = $PCas->link; echo $title; echo "<br/>"; } } } } this is what it echoes out 105L1-Title1 118 116 114 112 110 108L2-Title4 111 109L2-Title5 113 115 117 106L1-Title2 119 107L1-Title3 the number at the start is the text id L1-Title1 to L1-Title5 are tiles ($title) the spaces are links ($links) the problem is that i don't want the link spaces showing. just the 5 titles thanks Quote Link to comment https://forums.phpfreaks.com/topic/255138-foreach-loop-question/ Share on other sites More sharing options...
RussellReal Posted January 16, 2012 Share Posted January 16, 2012 <?php function listNavText($PHGlidA, $PHLobA, $PHLodA){ //GET THE LANGUAGE $langBS = basicSettings::find_by_id(1); $langID = $langBS->language_id; //GET THE LIST ORDER $PHLoa = PhLists::find_by_order($PHGlidA, $PHLobA, $PHLodA); foreach ($PHLoa as $PHLoas){ $PHLidA = $PHLoas->id; //GET THE LIST TEXT IDS $PCba = PCbridge::find_by_list($PHLidA, $langID); foreach ($PCba as $PCbas) { $PCidA = $PCbas->pageContent_id; //GET THE TEXT $PCa = PageContent::find_by_pageContID($PCidA); foreach ($PCa as $PCas) { $title = $PCas->title; $link = $PCas->link; if (!empty($PCidA) && !empty($title)) { echo "{$PCidA} {$title}<br />"; } /* echo $title; echo "<br/>"; */ } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255138-foreach-loop-question/#findComment-1308145 Share on other sites More sharing options...
ricky spires Posted January 16, 2012 Author Share Posted January 16, 2012 wow... perfect thanks that gave me: 105 L1-Title1 108 L2-Title4 109 L2-Title5 106 L1-Title2 107 L1-Title3 thanks again Quote Link to comment https://forums.phpfreaks.com/topic/255138-foreach-loop-question/#findComment-1308148 Share on other sites More sharing options...
ricky spires Posted January 20, 2012 Author Share Posted January 20, 2012 hello. im having a problem with this. a quick recap... basically im using the accordion drop down navigation from dynamic drive http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu.htm what im trying to do is pull the list data from the database say i have 6 items in my list and i want items with a parent id of 0 to be at the top and the other parent id's to relate to the items id. for example: id=1 parent=0 title=title1 id=2 parent=0 title=title2 id=3 parent=1 title=link1 id=4 parent=1 title=link2 id=5 parent=2 title=link3 id=6 parent=2 title=link4 so when its in a list it should look like this: id=1 parent=0 title=title1 id=3 parent=1 title=link1 id=4 parent=1 title=link2 id=2 parent=0 title=title2 id=5 parent=2 title=link3 id=6 parent=2 title=link4 the first problem is that the code below echos all the results in one long list and does not only show items with a parent id of 0 at the top. the code below echos out: title1 -link title2 -link title3 -link title4 -link title5 -link title6 -link this is the code: function name(){ $list = PhLists::find(); foreach ($list as $lists){ $PHLidA = $lists->id; $parent = $lists->parent_id; $title = $lists->title; echo' <div class="arrowlistmenu"> <h3 class="menuheader expandable">'.$title.'</h3> <ul class="categoryitems"> <li><a href="#">-link</a></li> </ul> </div>'; } } so the i tried adding an if to only echo out the ones with an id of 0 if($parent_id =="0"){ echo' <h3 class="menuheader expandable">'.$title.'</h3> } this worked and i got the following title1 -link title2 -link so this is my code now. function name(){ $list = PhLists::find(); foreach ($list as $lists){ $PHLidA = $lists->id; $parent = $lists->parent_id; $title = $lists->title; echo' <div class="arrowlistmenu">'; if($parent_id =="0"){ echo'<h3 class="menuheader expandable">'.$title.'</h3> } echo' <ul class="categoryitems"> <li><a href="#">link</a></li> </ul> </div>'; } } the next step would be to add the find link by parent id code but im having another problem. because i added that if statement when i click title1 the link drops down under title1 and when click title2 the link drops down under title1 it should drop down under title2. if i remove the if statement the link appears under each title but i get 6 titles and not 2. i hope this all makes sense please help. im going in circles. thanks Quote Link to comment https://forums.phpfreaks.com/topic/255138-foreach-loop-question/#findComment-1309514 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.