Forum Home
Press F1
 
Thread ID: 69767 2006-06-11 11:21:00 PHP Function Problem Erayd (23) Press F1
Post ID Timestamp Content User
462343 2006-06-11 11:21:00 Hi,

The following function does not return a value. If 'return' is changed to 'echo', it echos the value it should be returning without a problem. Can anyone see what's wrong with it? It's a part of a website - this bit generates the navbar links.


function buildLinks($sb) {
foreach($sb as $ln) {
if(is_array($ln) && is_array($ln[0])) {
$this->buildLinks($ln);
}
else {
$text = $ln['text'];
$href = $ln['href'];
$s .= "<a href='$href'>$text</a>\n";
}
}
return $s;
}
Erayd (23)
462344 2006-06-11 12:06:00 seemed okay when I ran a little test..
What is the array you are initially passing into the function?
gibler (49)
462345 2006-06-11 12:28:00 It's the return array of buildSidebar() from MediaWiki (in includes/Skin.php). The array is several layers deep (hence the recursion). It echos fine (if 'return' is replaced with 'echo', it just won't return. Since the first post, I have also discovered that it refuses to run if I rename it. Erayd (23)
462346 2006-06-11 12:38:00 ; at the very end ?? Rob99 (151)
462347 2006-06-11 13:01:00 ; at the very end ??
What are you asking me? I don&apos ; t understand the question.
Erayd (23)
462348 2006-06-12 12:43:00 What are you asking me? I don't understand the question.

function buildLinks($sb) {
foreach($sb as $ln) {
if(is_array($ln) && is_array($ln[0])) {
$this->buildLinks($ln);
}
else {
$text = $ln['text'];
$href = $ln['href'];
$s .= "<a href='$href'>$text</a>\n";
}
}
return $s;
}; <----- are you missing this
Rob99 (151)
462349 2006-06-12 13:05:00 Hmm, do you need that? I thought that was the purpose of the { }, to open and close it. I didn't know you needed an extra ;

Anyway. Irc is great.

/server irc.freenode.net
('/msg nickserv identify yournickname' if you need to log on)
/join ##php
mejobloggs (264)
462350 2006-06-12 23:08:00 You don't need the ';' at the end of the function in PHP.

What's the code when you're calling the function and trying to output the return value?
Mickey (9928)
462351 2006-06-13 07:48:00 You don't need the ';' at the end of the function in PHP.

What's the code when you're calling the function and trying to output the return value?
The code is:
$s = $this->buildLinks($this->buildSidebar());However since I posted this thread I managed to find another way of achieving the same end, so I no longer need help. Thanks anyway guys.

The outcome I was going for was to use MediaWiki:Sidebar (via a class derived from MediaWiki class Skin) to dynamically generate a set of nav links. I have now succeeded in doing this, although I used a somewhat different approach.

Thanks again,
Bletch
Erayd (23)
1