mysql_fetch_object field name with variable embedded

Hi all,

I’m running into a bit of an issue here and was wondering if anybody could help. I would like to return a table field value based on the current year.

My table has fields for: games2010, games2011, etc. Based on the year, I want to return the value of the field.

My code does not work. I’ve tried it two ways, I’m hoping you all can give me the third option that will be the correct option.

Option 1 - Gives a parse error, obviously wrong but putting here for demonstration purposes.
[php]if($player->games$year > 0)
{
$numplayers += 1;
}
if($player->games$year > $teamgp)
{
$teamgp = $player->games$year;
}[/php]

Option 2 - No parse error, but does not display anything
[php]if($player->games{$year} > 0)
{
$numplayers += 1;
}
if($player->games{$year} > $teamgp)
{
$teamgp = $player->games{$year};
}[/php]

Any ideas?

Thanks in advance.

I might like to try the third option:

[php]
$fieldname = ‘games’ . $year;
if($player->fields[$fieldname] > 0)
{
$numplayers += 1;
}
if($player->fields[$fieldname] > $teamgp)
{
$teamgp = $player->fields[$fieldname];
}

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service