Dynadot — .com Registration $8.99

Function breaks out of the layout?

Spaceship Spaceship
Watch

squid

Account Closed
Impact
19
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;

PHP:
<div id="something"><?php eNavigation(); ?></div>

It "Breaks" out of the div and puts itself above everything else.

The function is;
PHP:
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;
PHP:
<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;
PHP:
<NAVIGATION STUFF HERE>
<html>
<head>
<title>hi</title>
</head>
<body>
<div id="something"></div>
</body>
</html>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
<?php
function eNavigation(){
$domain = $_SERVER['HTTP_HOST'];
$NAVresult = mysql_query("SELECT site FROM pages WHERE site='$domain' ORDER BY id ASC");  
$out = '';
while ($NAVrow = mysql_fetch_assoc($NAVresult)) { 	
if ($NAVrow['title'] != 'Home') {
$out .= "<li><a href=\"";
$out .= stripslashes($NAVrow['title']);
$out .= "\">";
$out .= stripslashes($NAVrow['title']);
$out .= "</a></li>";
}
}
}
$injectthis = eNavigation();
?>
<html>
<head>
<title>hi</title>
</head>
<body>
<div id="something"><?php echo $injectthis; ?></div>
</body>
</html>

not the best code, but will work and make page load faster..
 
0
•••
You're selecting the field site from your mysql database, but trying to print out the field title. I'm not sure if this is one of your issues or not, but it may be affecting the result.
Also, whats with the whole "if" "continue" thing...?


Bruce
 
0
•••
Bruce_KD said:
You're selecting the field site from your mysql database, but trying to print out the field title. I'm not sure if this is one of your issues or not, but it may be affecting the result.
Also, whats with the whole "if" "continue" thing...?


Bruce

The "title" is a field, so page ID 1 title = "something" page id 2 title = "something else" etc.

Also, i don't want it to echo the "home" one :)
 
0
•••
It doesn't matter what "title" is, if you aren't selecting it in your mySQL query, you can't call $row['title'].

Try
Code:
$NAVresult = mysql_query("SELECT title FROM pages WHERE site='$domain' ORDER BY id");

(ASC is the default order by, so you don't really need to include it).


Bruce
 
0
•••
blacknet, you're missing a
PHP:
return $out;
a
PHP:
$out = '<ol>';
and a
PHP:
$out = '</ol>';
in that function, also.
 
0
•••
Alas, it was only a quick 2 seconds code whilst on the phone and after being awake for way too long.

As you'll know there are tonnes of mistakes in the whole thing. Anyways, this may be of more help.. you'll still need to connect up to your db etc and everything else you need to do lol.

PHP:
<?php

function eNavigation(){
	$htmlNav = '';
	if( $navResult = select("SELECT site, title FROM pages WHERE site='" . cleanse( getHostDomain() ) . "' ORDER BY id ASC") ) {
		foreach( $navResult as $index => $values ) {
			if( trim(strtolower( $values['title'] )) !== 'home' ) {
				$htmlNav .= '<li><a href="'. $values['title'] .'">'. $values['title'] .'</a></li>';
			}
		}
	}
	return (bool)strlen($htmlNav) ? '<ol>' . $htmlNav . '</ol>' : '';
}

function getHostDomain() {
	// it's preferable to user SERVER_NAME over HTTP_HOST
	// as HTTP_HOST is only present in valid HTTP 1.1 calls
	// even more preferable is to define the current host in a config file
	$domain = strtolower( trim ( $_SERVER['SERVER_NAME'] ) );
	return (substr($domain, 0, 4) == 'www.') ? substr( $domain , 4) : $domain;
}

function select( $query ) {
	// a quick select function
	// always best to get resources freed up as quickly as possible.
	$result = mysql_query( $query ) or die( 'Query failed: ' . mysql_error() . "\n" . $query );
	$result_rows = mysql_num_rows( $result );
	if ($result_rows == 0) {
		return FALSE;
	}
	$output = array();
	for ($r=0;$r<$result_rows;$r++) {
	   $output[$r] = mysql_fetch_assoc($result);
	}
	mysql_free_result($result);
	return $output;
}

function cleanse( $var ) {
	// quick function to cleanse our variables for use in query strings
	// prevent sql injection attacks etc
	if( function_exists('mysql_real_escape_string') ) {
		if( function_exists('get_magic_quotes_gpc') ) {
		   $var = get_magic_quotes_gpc() ? stripslashes($var) : $var;
		}
		// function will only work if a mysql connection is active..
		return @mysql_ping() ? mysql_real_escape_string( $var ) : $var;
	}
	return $var;
}

$navigation = eNavigation();
?>
<html>
<head>
<title>hi</title>
</head>
<body>
<div id="something"><?php echo $navigation; ?></div>
</body>
</html>

is probably a more useful response *shrugs*
 
0
•••
My contribution :p

Instead of:
PHP:
function getHostDomain() {
    // it's preferable to user SERVER_NAME over HTTP_HOST
    // as HTTP_HOST is only present in valid HTTP 1.1 calls
    // even more preferable is to define the current host in a config file
    $domain = strtolower( trim ( $_SERVER['SERVER_NAME'] ) );
    return (substr($domain, 0, 4) == 'www.') ? substr( $domain , 4) : $domain;
}

Use:
PHP:
function getHostDomain() {
    // it's preferable to user SERVER_NAME over HTTP_HOST
    // as HTTP_HOST is only present in valid HTTP 1.1 calls
    // even more preferable is to define the current host in a config file
    $domain = strtolower( trim ( $_SERVER['SERVER_NAME'] ) );
    return preg_replace('#^www\.#', '', $domain);
}
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back