Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/255138-foreach-loop-question/
Share on other sites

<?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/>";	
			*/
		}
	}	
}
}
?>

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

 

 

 

 

 

 

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.