Crusader Posted June 27, 2006 Share Posted June 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13006-get-variable-from-within-a-function/ Share on other sites More sharing options...
uraniumdeer Posted June 27, 2006 Share Posted June 27, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/13006-get-variable-from-within-a-function/#findComment-50028 Share on other sites More sharing options...
Crusader Posted June 27, 2006 Author Share Posted June 27, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13006-get-variable-from-within-a-function/#findComment-50032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.