[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 06-28-2008, 07:23 AM   #1 (permalink)
Account Suspended
 
squid's Avatar
 
Join Date: Jan 2008
Location: Stafford
Posts: 993
15.00 NP$ (Donate)

squid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to behold

Special Olympics Myanmar Relief
Function breaks out of the layout?

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>";
}
}
Why does it break out of the Div?

So if i have;
PHP Code:
<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 Code:
<NAVIGATION STUFF HERE>
<
html>
<
head>
<
title>hi</title>
</
head>
<
body>
<
div id="something"></div>
</
body>
</
html>
squid is offline  
Old 06-28-2008, 07:30 AM   #2 (permalink)
NamePros Member
 
Join Date: May 2008
Posts: 180
100.95 NP$ (Donate)

blacknet is just really niceblacknet is just really niceblacknet is just really niceblacknet is just really niceblacknet is just really nice


PHP Code:
<?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..
blacknet is offline  
Old 06-28-2008, 10:55 AM   #3 (permalink)
NamePros Member
 
Join Date: Sep 2006
Posts: 87
100.00 NP$ (Donate)

Bruce_KD will become famous soon enoughBruce_KD will become famous soon enough


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
Bruce_KD is offline  
Old 06-28-2008, 11:06 AM   #4 (permalink)
Account Suspended
 
squid's Avatar
 
Join Date: Jan 2008
Location: Stafford
Posts: 993
15.00 NP$ (Donate)

squid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to beholdsquid is a splendid one to behold

Special Olympics Myanmar Relief
Quote:
Originally Posted by Bruce_KD
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
squid is offline  
Old 06-28-2008, 11:10 AM   #5 (permalink)
NamePros Member
 
Join Date: Sep 2006
Posts: 87
100.00 NP$ (Donate)

Bruce_KD will become famous soon enoughBruce_KD will become famous soon enough


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
Bruce_KD is offline  
Old 06-29-2008, 02:12 AM   #6 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold


blacknet, you're missing a
PHP Code:
return $out;
a
PHP Code:
$out = '<ol>';
and a
PHP Code:
$out = '</ol>';
in that function, also.
Barrucadu is online now  
Old 06-29-2008, 06:50 AM   #7 (permalink)
NamePros Member
 
Join Date: May 2008
Posts: 180
100.95 NP$ (Donate)

blacknet is just really niceblacknet is just really niceblacknet is just really niceblacknet is just really niceblacknet is just really nice


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

$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*
blacknet is offline  
Old 06-29-2008, 08:25 AM   #8 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services

 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
My contribution

Instead of:
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 (
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);
}
__________________
Eric is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 12:34 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85