Jump to content

php Function breaks out of layout? :|


Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/112347-php-function-breaks-out-of-layout/
Share on other sites

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>

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

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

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?

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";

	    }
}

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.

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;
}

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.

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;
}

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 :)

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.