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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

	    }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.