Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/13006-get-variable-from-within-a-function/
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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.