Petty_Crim Posted July 1, 2007 Share Posted July 1, 2007 I'm having trouble creating dynamic variables using field names. Basically I got a heap of fields named after Players and I want to grab the field name and make a variable called it while assigning it a value from a post variable which is also the player name. This is the code I'm using: $result=mysql_query("SELECT * FROM playerstats"); while($fld = mysql_fetch_field($result)) { if ($fld->name!='match_id' && $fld->name!='opponent' && $fld->name!='map') { $fld->name=$_POST[$fld->name]; } } Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 1, 2007 Author Share Posted July 1, 2007 My problem is exactly the same as this: http://www.phpfreaks.com/forums/index.php/topic,129931.0.html ATM if I want to add another player/field, I have to manually go around updating all the code. With this it would be done automatically. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 2, 2007 Share Posted July 2, 2007 U want to update the player name or just to select it. i am not clear about ur q. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 U want to update the player name or just to select it. i am not clear about ur q. Player names are fields in my table. If I add a new player atm I have to go around updating everything that is why I want to automate it. In my code above I'm trying to get the field name ie John make that into a variable with the same name ie $john and then assign a post variable as its value. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Quote Link to comment Share on other sites More sharing options...
Carterhost Posted July 2, 2007 Share Posted July 2, 2007 What about using an Array? Instead of having player's names as variables: $john $steve $frank $jeff you would have: $player[1] $player[2] $player[3] $player[4] Disclaimer: it's quarter past midnight here, syntax may not be correct. Check the PHP man pages for array usage... Quote Link to comment 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.