citricsquidut78696 Posted June 28, 2008 Share Posted June 28, 2008 Hi, I have a function that echo's the navigation for a website, it runs perfectly, BUT, it breaks out of the layout. If i do; <div id="something"><?php eNavigation(); ?></div> It "Breaks" out of the div and puts itself above everything else. The function is; function eNavigation(){ $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT site FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { if ($NAVrow['title'] == 'Home') { continue; } echo "<li><a href=\""; echo stripslashes($NAVrow['title']); echo "\">"; echo stripslashes($NAVrow['title']); echo "</a></li>"; } } Why does it break out of the Div? :| So if i have; <html> <head> <title>hi</title> </head> <body> <div id="something"><?php eNavigation(); ?></div> </body> </html> When i view the page in my browser, the following happens; <NAVIGATION STUFF HERE> <html> <head> <title>hi</title> </head> <body> <div id="something"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/ Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 Use return instead. function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT site FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); if ($title == 'Home') { continue; } $html .= '<li><a href="' . $title . '">' . $title . "</a></li>\n"; return $html; } } Change <div id="something"><?php eNavigation(); ?></div> to <div id="something"><?php echo eNavigation(); ?></div> Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576810 Share on other sites More sharing options...
LemonInflux Posted June 28, 2008 Share Posted June 28, 2008 Yes, I've had this before with echoing functions. They do seem to act a bit unpredictably. Not sure if it's a bug or what, but it's definitely something to note: try to use return, not echo. ---------------- Now playing: Augustana - Dust via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576812 Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 Because when you use echo in a function, it doesn't return the output of where the function was called from. But from where the function is defined. Example: function test() { echo 'hello '; } echo 'world' . test(); //output: hello world Where as function test() { return 'hello '; } echo 'world' . test(); //output: world hello Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576815 Share on other sites More sharing options...
citricsquidut78696 Posted June 28, 2008 Author Share Posted June 28, 2008 Use return instead. Excellent, it's worked, slightly. I've just got to try and find a way around the fact i have more than 1 thing echo'ed from the database, so $html would need to be a changing variable, but I think $html1, $html2, would work? Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576816 Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 What are you trying to do? What else do you want to output? You should be able to concat everything to the $html variable. Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576817 Share on other sites More sharing options...
citricsquidut78696 Posted June 28, 2008 Author Share Posted June 28, 2008 I want it to echo every title from the database. I've made a sort of "mini site" script, for personal use. I had it working PERFECTLY before, but I wanted to add a template feature, so I've had to move to using functions, which is what stumped me... The database has; ID | TITLE | CONTENTS And I want it to echo each title, so like; <li><a href="/title1">title1</a></li> <li><a href="/title2">title2</a></li> It worked perfectly before with; <? $resulttt = mysql_query("SELECT site, title, content FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($rowww = mysql_fetch_assoc($resulttt)) { if ($rowww['title'] == 'Home') { continue; } echo "<li><a href=\""; echo stripslashes($rowww['title']); echo "\">"; echo stripslashes($rowww['title']); echo "</a></li>"; } ?> But when I use what you provided before it only echo's the first result, not every single one, I tried the following, but it creates the same problem as before, they break out of the layout; function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT * FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); echo '<li><a href="' . $title . '">' . $title . "</a></li>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576821 Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 Umm, you're not using my code. You're still using echo. change echo to $html .= then before the closing brace for your function add return $html; Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576823 Share on other sites More sharing options...
citricsquidut78696 Posted June 28, 2008 Author Share Posted June 28, 2008 Umm, you're not using my code. You're still using echo. change echo to $html .= then before the closing brace for your function add return $html; As i explained, that works, BUT it only echo's the first result, no more after it. So i have around 8 results i want to echo, not 1. Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576824 Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 It wont return just 1 result. It'll return all results .= is the concatenation operator. Post the current code you're using now. EDIT: Argh balls! I just noticed I had a typo in my code example. I added the return statement inside of the while loop. It should be outside of the loop. function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT site FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); if ($title == 'Home') { continue; } $html .= '<li><a href="' . $title . '">' . $title . "</a></li>\n"; } return $html; } Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576826 Share on other sites More sharing options...
citricsquidut78696 Posted June 28, 2008 Author Share Posted June 28, 2008 function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT * FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); $html .= '<li><a href="' . $title . '">' . $title . "</a></li>\n"; return $html; } } and then eNavigation() where i want it echoed, but this only echos the first result. Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576829 Share on other sites More sharing options...
wildteen88 Posted June 28, 2008 Share Posted June 28, 2008 Sorry, I modified my post before you replied. It wont return just 1 result. It'll return all results .= is the concatenation operator. Post the current code you're using now. EDIT: Argh balls! I just noticed I had a typo in my code example. I added the return statement inside of the while loop. It should be outside of the loop. function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT * FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); $html .= '<li><a href="' . $title . '">' . $title . "</a></li>\n"; } return $html; } Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576830 Share on other sites More sharing options...
citricsquidut78696 Posted June 28, 2008 Author Share Posted June 28, 2008 Sorry, I modified my post before you replied. It wont return just 1 result. It'll return all results .= is the concatenation operator. Post the current code you're using now. EDIT: Argh balls! I just noticed I had a typo in my code example. I added the return statement inside of the while loop. It should be outside of the loop. function eNavigation() { $html = null; $domain = $_SERVER['HTTP_HOST']; $NAVresult = mysql_query("SELECT * FROM pages WHERE site='$domain' ORDER BY id ASC"); while ($NAVrow = mysql_fetch_assoc($NAVresult)) { $title = stripslashes($NAVrow['title']); $html .= '<li><a href="' . $title . '">' . $title . "</a></li>\n"; } return $html; } It worked! I love you. Also, have an in-advance congratulations on your 8,000 post Quote Link to comment https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/#findComment-576849 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.