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]; } } Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/ 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. Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-287400 Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-287739 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. Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-287744 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. Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-287763 Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-287818 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... Link to comment https://forums.phpfreaks.com/topic/57978-setting-variable-names-using-field-names/#findComment-288402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.