Jump to content

can i put a foreach loop in a function before an <li> and the } after the </li>


ricky spires

Recommended Posts

ok.

 

say i have a list with a foreach loop in it.

like this:

 

echo'
<ul>';

$link = Link::find_all();
foreach($link as $links){
$name = $links->name;

echo'
<li>'.$name.'</li>';

}
echo'
</ul>';


 

 

now.

say that i want to put this part :

$link = Link::find_all();
foreach($link as $links){
$name = $links->name;

 

into a function so i can reuse the code...

 

like this:

 

FUNCTION

function(find_li){
  $link = Link::find_all();
  foreach($link as $links){
  $name = $links->name;
}

echo'
<ul>';

find_li();

echo'
<li>'.$name.'</li>';
}
echo'
</ul>';

 

 

the problem is the foreach } its not inside the function so the function will not work. but it needs to be after the </li> so that i get all the results in the <li>

 

so is there a way to put the function before the <li> and the } after the </li>

 

 

 

 

 

No, you cannot do it the way you mentioned. Do you use this similar code to generate a list elsewhere on the site? Or are you trying to streamline the loop?

 

If you are not using it elsewhere on the site, the way you have it original is fine and perfectly acceptable, and putting it in a function does not reduce redundancy. If, you do use it in multiple places, well we can probably help you stream line it a bit more. Just need to know which it is.

 

well.

 

that was an example. the actual code is more complex and looks huge so i thought i would try and brake it up a bit

 

because its a nested list.

 

im using a foreach loop for each level. then each li has a parent id which relates to the id of the li above

 

its looks a bit of a mess but its getting there.

 

function listNavText($PHGlidA, $PHLobA, $PHLodA){	

echo'<div id="phLists">
<div class="arrowlistmenu">';

$langBS = basicSettings::find_by_id(1);
$langID = $langBS->language_id; 

$PHLhead = PhLists::navHeaderOrder($PHGlidA, $PHLobA, $PHLodA);
foreach ($PHLhead as $PHLheads){
$PHLheadID = $PHLheads->id;

//find nav header
$PHheader = PhLists::find_NavHeader($PHLheadID);
foreach ($PHheader as $PHheaders){
$PHheaderID = $PHheaders->id;

$PCBhead = PCbridge::find_NavHeader($PHheaderID, $langID);
foreach ($PCBhead as $PCBheads){
$PCBheadID = $PCBheads->pageContent_id;

$PCheader = PageContent::find_NavHeader($PCBheadID);  		
foreach ($PCheader as $PCheaders){

echo'
<h3 class="menuheader expandable">'.$PCheaders->title.'</h3>

<ul class="categoryitems">';

	$PHLlink = PhLists::navLinkOrder($PHGlidA, $PHLobA, $PHLodA);
	foreach ($PHLlink as $PHLlinks){ 
	$PHLlinkID = $PHLlinks->id;
	$PHLlinkDEST = $PHLlinks->destination_id;

	//find nav link
	$PHlink = PhLists::find_NavLink($PHLlinkID, $PHLheadID);
	foreach ($PHlink as $PHlinks){
	$PHlinkID = $PHlinks->id;

	$PCBlink = PCbridge::find_NavLink($PHlinkID, $langID);
	foreach ($PCBlink as $PCBlinks){
	$PCBlinkID = $PCBlinks->pageContent_id;

	$PClink = PageContent::find_NavLink($PCBlinkID);  		
	foreach ($PClink as $PClinks){
	echo'
	<li><a href="index.php?pageID='.$PHLlinkDEST.'">'.$PClinks->link.'</a></li>';
	}
	}
	}
	}


	$PHLtitle = PhLists::navTitleOrder($PHGlidA, $PHLobA, $PHLodA);
	foreach ($PHLtitle as $PHLtitles){ 
	$PHLtitleID = $PHLtitles->id;
	$PHLtitleDEST = $PHLtitles->destination_id;

	//find nav title
	$PHtitle = PhLists::find_NavTitle($PHLtitleID, $PHLlinkID);
	foreach ($PHtitle as $PHtitles){
	$PHtitleID = $PHtitles->id;

	$PCBtitle = PCbridge::find_NavTitle($PHtitleID, $langID);
	foreach ($PCBtitle as $PCBtitles){
	$PCBtitleID = $PCBtitles->pageContent_id;

	$PCtitle = PageContent::find_NavTitle($PCBtitleID);  		
	foreach ($PCtitle as $PCtitles){
	echo'
	<li><a href="index.php?pageID='.$PHLtitleDEST.'" class="subexpandable">'.$PCtitles->title.'</a>';
	}
	}
	}
	}

			echo'
			<ul class="subcategoryitems" style="margin-left: 15px">';

			$PHLsubLink = PhLists::navSubLinkOrder($PHGlidA, $PHLobA, $PHLodA);
			foreach ($PHLsubLink as $PHLsubLinks){ 
			$PHLsubLinkID = $PHLsubLinks->id;
			$PHLsubLinkDEST = $PHLsubLinks->destination_id;

			//find nav sub link
			$PHsubLink = PhLists::find_NavSubLink($PHLsubLinkID, $PHLtitleID);
			foreach ($PHsubLink as $PHsubLinks){
			$PHsubLinkID = $PHsubLinks->id;

			$PCBsubLink = PCbridge::find_NavSubLink($PHsubLinkID, $langID);
			foreach ($PCBsubLink as $PCBsubLinks){
			$PCBsubLinkID = $PCBsubLinks->pageContent_id;

			$PCsubLink = PageContent::find_NavSubLink($PCBsubLinkID);  		
			foreach ($PCsubLink as $PCsubLinks){
			echo'	
				<li><a href="index.php?pageID='.$PHLsubLinkDEST.'">subLink='.$PCsubLinks->link.'</a></li>';
			}
			}
			}
			}
			echo'
			</ul>
	</li>';

echo'
</ul>';
}
}
}
}
echo'</div></div>';
}

 

 

no worries. ill carry on as i am

For something like that, recursion is generally the method of choice. It can be a bit rough to get right and to do but will definitely streamline the process like you want. I am not very handy with doing recursion stuff, if I find some time I will try and do a mock of what you would be looking for.

 

In the interim, you can check out google for php recursion and find some good articles on the matter, if you want to pursue this.

I'm not sure if I understand the problem completely but why not:

 

<?php 
// A function that prints out a list of links
function my_function($links) {

$html = '';

// There might be cases when the array of links is empty
if (count($links) > 0) {

	// Open the list
	$html .= '<ul>'

	// Add the list items
	foreach ($links as $link) {
		$html .= sprintf("<li>%s</li>", htmlspecialchars($link->name));
	}

	// Close the list
	$html .= '</ul>';
}

return $html  # or echo $html;
}
?>

 

Then you can do something like:

 

<?php
$links = Link::find_all();
echo my_function($links);
?>

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.