Jump to content

Archived

This topic is now archived and is closed to further replies.

Crusader

get variable from within a function

Recommended Posts

How would I be able to output the invidual variables from a function such as this? I don't want to get them all at once but individually to output within a page.

[code]class Foo {
    function PlayerStats($mid) {
        $get_stats = 'SELECT users.mid, users.username, users.email, users.race, users.turns, users.covert_turns, users.tylium, users.production, users.alert_status, users.commander, users.status FROM users WHERE users.mid="'.$mid.'" limit 1';
        $usr_stats = mysql_query($get_stats) or die(mysql_error());
        $i=0;
        while($stats = mysql_fetch_assoc($usr_stats)){
            $player_name[$i] = $stats['username'];
            $player_email[$i] = $stats['email'];
            $player_race[$i] = $stats['race'];
            $player_turns[$i] = $stats['turns'];
            $player_covert_turns[$i] = $stats['covert_turns'];
            $player_tylium[$i] = $status['tylium'];
            $player_production[$i] = $stats['alert_status'];
            $player_commander[$i] = $status['commander'];
            $player_status[$i] = $status['status'];

        }
    }
}[/code]

To further elaborate, I just want to be able to echo $player_name[$i] anywhere on the page, have another function run and then echo $player_email[$i].

Thanks.

Share this post


Link to post
Share on other sites
First of, remember to increment your $i ($i++)

As far as i know, you can't get the variables defined in a function. You need to either define the variables before your function, or "return" the values.

[code]
class Foo {
    function PlayerStats($mid) {
        $get_stats = 'SELECT * FROM users WHERE mid="$mid" limit 1';
        $usr_stats = mysql_query($get_stats) or die(mysql_error());
        $i=0;
        while($stats = mysql_fetch_assoc($usr_stats)){
            $player_name[$i] = $stats['username'];
            $player_email[$i] = $stats['email'];
            $player_race[$i] = $stats['race'];
            $player_turns[$i] = $stats['turns'];
            $player_covert_turns[$i] = $stats['covert_turns'];
            $player_tylium[$i] = $status['tylium'];
            $player_production[$i] = $stats['alert_status'];
            $player_commander[$i] = $status['commander'];
            $player_status[$i] = $status['status'];
            $i++;
        }
        return array("name" => array($player_name), "email" => array($player_email), "race" => array($player_race), "turns" => array($player_turns), "covertturns" => array($player_covert_turns), "tylium" => array($player_tylium),  "production" => array($player_production), "commander" => array($player_commander), "status" => array($player_status));
    }
}[/code]

with this you could call the values with:
[code]
$foo = new Foo();
$plrsts = $foo->PlayerStats($mid);
echo $plrsts['name'][0];
[/code]

The above code is quite messy, and could most likely easely get cleaned up - but i noticed you limited your search to only return 1 result, therefor the while, and the extra arrays shouldn't be nessesary:

[code]
class Foo {
    function PlayerStats($mid) {
        $get_stats = 'SELECT * FROM users WHERE mid="$mid" limit 1';
        $usr_stats = mysql_query($get_stats) or die(mysql_error());
        $stats = mysql_fetch_assoc($usr_stats));
        return $stats;
    }
}[/code]

call this with

[code]
$foo = new Foo();
$plrsts = $foo->PlayerStats($mid);
echo $plrsts['username'];
echo $plrsts['email'];
[/code]


I havn't tested this so i can't guarenteee it will work first try...

Share this post


Link to post
Share on other sites
Thanks! Just realized that I wasn't going to need a loop at all and that I even forgot to add to the loop counter!

Mysql_fetch_assoc worked like a charm :D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.