Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

110 users online



Function help

Function help

Currently viewing this thread: 1 (0 members and 1 guests)


colincameron

colincameron

Neverside Newbie
Status: Offline!

Function help

im pulling my hair out with this problem. i know im either doing something that cant be done or im missing something simple. i will try and explain this as easy as i can

in a include called functions i have this

PHP:


<?
function homeTab()
{
    if ( 
$section=="home" 
    echo 
" id=\"current\"";
}
?>

on the page with the content i have this

PHP:


<?
require('functions.php');
$section home;
include(
'inc/head.php');
?>

and in the head include i have this to call the function

Code:


<li<? homeTab(); ?>>

everything works fine but the function doesn't work. nothing gets outputted.

am i forgetting something when using the if statement in a function? cause if i put that exact same code in the site without a function it works

Atealtha

Atealtha

Status: Offline!
PHP:

<?php
if ()
{
 
//put in those curly brackets!
}
?>

colincameron

colincameron

Neverside Newbie
Status: Offline!

i tried that too, but it doesnt help

im trying to make a modified version of this from alistapart. is there a reason why they left out the curly brackets?

http://www.alistapart.com/articles/keepingcurrent/

Code:


<div id="navigation">
<ul>
<li<?php if ($thisPage=="Page One")
echo " id=\"currentpage\""; ?>>
<a href="#">Page One</a></li>
<li<?php if ($thisPage=="Page Two")
echo " id=\"currentpage\""; ?>>
<a href="#">Page Two</a></li>
<li<?php if ($thisPage=="Page Three")
echo " id=\"currentpage\""; ?>>
<a href="#">Page Three</a></li>
<li<?php if ($thisPage=="Page Four")
echo " id=\"currentpage\""; ?>>
<a href="#">Page Four</a></li>
</ul>
</div>

DarkFire

DarkFire

Neverside Newbie
Status: Offline!

but what is currentpage? are you trying to make it output the current page?

___________________

<sig here>

dreamcatcher

dreamcatcher

Status: Offline!

Your $section variable should be declared global inside the function:

PHP:

<?php

function homeTab()
{
    global 
$section;

    if ( 
$section=="home" 
    echo 
" id=\"current\"";
}

?>

Or you can pass the data directly into the function, in which case it doesn`t need to be global.

PHP:

<?php

function homeTab($section)
{
    if ( 
$section=="home" 
    echo 
" id=\"current\"";
}

?>

Also, a switch statement will run faster than multiple if`s.

Hope that helps. Smile

___________________

::M-Dream::

:: Asian Cinema :: Asian Music :: Chinese Astrology :: Free PHP Scripts
:: Asian Cover Scans :: Asian Photo Galleries

colincameron

colincameron

Neverside Newbie
Status: Offline!

ok, i had a feeling i needed to put something in the brackets. i will that

btw, if i put that globals stuff in there doesn't that override the variable and use the one in the function or only if it was like this:

PHP:

<?php

$GLOBALS
['section'] = "";

?>

spike

spike

website designer
Status: Offline!

btw, if youre getting an additional error

$section = home;

should be changed to

$section = "home";

currently, its checking to see if home is a constant, then assigning it as a string.

___________________

http://www.anatonic.com/shared/sig.gif
http://www.anatonic.com/

colincameron

colincameron

Neverside Newbie
Status: Offline!

thanks for the tip

colincameron

colincameron

Neverside Newbie
Status: Offline!

so adding the variable to the brackets did the trick the code looks like this:

PHP:

<?php

//    HOME TAB
function homeTab($section)
{
    if ( 
$section=="home" 
    echo 
" id=\"current\"";
}

//    SHOPPING TAB
function shoppingTab($section)
{
    if ( 
$section=="shopping" 
    echo 
" id=\"current\"";
}

//    GIVING TAB
function givingTab($section)
{
    if ( 
$section=="giving" 
    echo 
" id=\"current\"";
}

?>

is there a more efficient way of doing this?

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0104 seconds.