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 Code:
<div id="something"><?php eNavigation(); ?></div>
It "Breaks" out of the div and puts itself above everything else.
The function is;
PHP Code:
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>";
}
}
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...?
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 Code:
<?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;
}
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 Code:
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);
}