PHP doNav Function

Shadow121

Member
Messages
901
Reaction score
0
Points
16
Alright I'm making a forum system of my own and I need help making a breadcrumb trail. This is what I have so far.

Code:
function doNav($act){
		$addonfromHere = "<a href=\"".SITE_LINK."\">Index</a>";
		$linkNum = count($act);
		$linksUsed = 1;
		$linksreturn = "";
		foreach($act as $links){
			$linkz[] = $links;
		}
		for($i = 0; $i <= $linkNum; $i++){
			if($linksUsed == 1){
				$linksreturn = $addonfromHere;
			}else{
				$linksreturn .= " &raquo; <a href=\"".SITE_LINK."$links/\">".$linkz[$i]."</a>";
			}
			$linksUsed++;
		}
		return $linksreturn;
}

It only does the 2nd/3rd/4th things and not the first one. Any help with this?
 
Last edited:

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Because the first time it is run, the value of $linksUsed is 1, which is set on line 4. After the first run, the value will be 2, then 3 and so on. Which means that on the first iteration of your for loop, $linksreturn will be set to $addonfromHere and it will ignore the else statement.

To correct this, change line 4 to :
$linksUsed = 0;

EDIT: Actually, don't ;)
Doing so will cause the second link to vanish, AND the first. You could try changing the ==1 to ==0 on line 10 though...
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Can you post a sample or two with the values of :

Code:
$act = ??
$linksreturn = (Expected output) ??
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
Say I have the following: ?x=ucp&y=password

I expect it to be User Panel &raquo; Password
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Say I have the following: ?x=ucp&y=password

I expect it to be User Panel &raquo; Password
So how exactly do you call the function?
PHP:
<?php

[...]

echo (doNav(array('12345','67890','abcde')));
?>
Using your initial code, I would expect it to show:
<a href="http://yoursite.com/">Index</a> » <a href="http://yoursite.com/abcde/">67890</a> » <a href="http://yoursite.com/abcde/">abcde</a>


Note that on line 13, you use the variable $links, but the only other time you used it was in the foreach loop, so you can only assume that your PHP setup is using the last iteration of that foreach as the value for $links.


I would instead use the following:
PHP:
function doNav($act){
		$output = '<a href="http://forums.x10hosting.com/programming-help/'.SITE_LINK.'">Index</a>';
		foreach($act as $link){
			$output .= ' &raquo; <a href="http://forums.x10hosting.com/programming-help/'.SITE_LINK.$link.'">'.$link.'</a>';
		}
		return $output;
}
Called the same way, the output should now be:
<a href="http://yoursite.com/">Index</a> » <a href="http://yoursite.com/12345">12345</a> » <a href="http://yoursite.com/67890">67890</a> » <a href="http://yoursite.com/abcde">abcde</a>
 
Last edited:

Shadow121

Member
Messages
901
Reaction score
0
Points
16
Alright, I'll try that now. ^_^

Works like a charm ^_^
 
Last edited:
Top