First attempt at Function - Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grifter
Forum Newbie
Posts: 2
Joined: Sun Nov 17, 2002 5:29 am

First attempt at Function - Help!

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
grifter
Forum Newbie
Posts: 2
Joined: Sun Nov 17, 2002 5:29 am

Post by grifter »

Ahh. that would explain it! It's set at the sign in page and given global scope.

Thanks for your help.

G
Post Reply