Page 1 of 1

First attempt at Function - Help!

Posted: Sun Nov 17, 2002 5:29 am
by grifter
Hi,

I've tried to do simple function for changing a css class dependant on the variable passed to the function.

function get_style($who) // $name from predictions table
{
$style = "user"; // default style.

if( $who == $login ) // is this the logged in user?
{
$style = "thisuser";
}
return $style;
}

then, for each row in the resultset of a db query, I call the function to determine $style like so:

while ($row = mysql_fetch_array($sql_result)) {
$name = $row["name"];
(etc etc for all other columns);

$style = get_style($name);
echo "<TR>
<TD class=$style>$name</TD>
</TR>";
}


$login is the existing user's session id login name.
there is one row in the resultset where $name matches $login.

each time I run it, $style is being returned as "user", even when $name = $login.

any help appreciated. TIA.

Grifter

Posted: Sun Nov 17, 2002 8:41 am
by volka
where do you set $login ?
functions have no access to global scope by default.
http://www.php.net/manual/en/language.variables.scope.php

Posted: Tue Nov 19, 2002 3:21 am
by grifter
Ahh. that would explain it! It's set at the sign in page and given global scope.

Thanks for your help.

G