chiefrokka Posted August 11, 2008 Share Posted August 11, 2008 I've done this before but can't find the file I did this in before. to simplify, I have a variable called $Current_Week which can be 1-17 let's say I have a db with some variables like Week1_Q1_Winner Week1_Q2_Winner Week2_Q1_Winner Week2_Q2_Winner I'm trying to find the syntax to set that variable based on the $Current_Week. First I just want to create a variable and then I want to do a mysql update to update that specific variable. here's what I have but the syntax isn't workin <?php $Week'.$Current_Week.'_Q1_Winner = $row['Name']; // let's say we're on Week 1, I want to create a variable Week1_Q1_Winner = myname // now I want to update db variable for that week mysql_query("UPDATE `Winners` SET `$Week'.$Current_Week.'_Q1_Winner` = '$Week'.$Current_Week.'_Q1_Winner' Where `Year` = '2008' ") or die(mysql_error()); // so if we're on Week 8 for example it would update the db variable "Week8_Q1_Winner" ?> Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/ Share on other sites More sharing options...
ag3nt42 Posted August 11, 2008 Share Posted August 11, 2008 You might be better off using a text file to do this rather then in a database.. point being because.. if your storing it in a database then the only way to retrieve it is by putting the DB value into a variable hense screwing up the whole idea of it in the first place.. if you use a txt file instead you can simply read in the lines from the text file.. well i spose it would work best this way with the .php extension instead of .txt but the theory is still the same.. then include these. When you need to update.. you can either write some functions to read the file line by line and change things..or you can simply use the already setup variables residing inside the file and recreate the whole thing. otherwise whats the sense in creating a variable to hold a variable? TEXTFILE.txt: $Week1_Q1_Winner = $NameVariable; Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613840 Share on other sites More sharing options...
chiefrokka Posted August 11, 2008 Author Share Posted August 11, 2008 while that might work, I don't know enough about storing files and don't have the time to spend researching it. I know I can get this to work because I've done it before, it's just finding the right syntax to do it this way. The reason why I create a variable the same name as the db variable is just so I don't have to keep using $row['var name'] plus I'm creating a dynamic variable based on the Current Week so I can use that variable to do a mysql UPDATE. anybody else know how to do this besides using a file? Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613863 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 Although I don't like the way you're doing it, try: mysql_query("UPDATE `Winners` SET `Week$Current_Week_Q1_Winner` = 'Week$Current_Week_Q1_Winner' Where `Year` = '2008' ") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613870 Share on other sites More sharing options...
chiefrokka Posted August 11, 2008 Author Share Posted August 11, 2008 Although I don't like the way you're doing it, try: mysql_query("UPDATE `Winners` SET `Week$Current_Week_Q1_Winner` = 'Week$Current_Week_Q1_Winner' Where `Year` = '2008' ") or die(mysql_error()); Week1_Q1_Winner has to be the players name I grab from a separate query so that wouldn't work as it would just set that variable to the word "Week1_Q1_Winner" i think. I started trying a new method but not finished yet. $str = "Week"; $str .= $Current_Week; $str .= "_Q1_Winner"; that would give me my dynamic variable name based on the week your on but I have to get that variable name to grab the $row['Name'] of the actual winner. so "Week2_Q1_Winner" would be the variable name which is the name of one of the variables in the database. the database has same variables but one for each week of the season so my script just needs to know which variable to send the Winner to based on which week we're on. Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613891 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 Woops, I thought that was what you needed. Just put the $ in front of Week now. Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613895 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 Try $VariableName = 'Week'.$Current_Week.'_Q1_Winner'; $$VariableName = $row['Name']; mysql_query("UPDATE `Winners` SET `$VariableName` = '".$$VaiableName."' Where `Year` = '2008' ") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613909 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 Try $VariableName = 'Week'.$Current_Week.'_Q1_Winner'; $$VariableName = $row['Name']; mysql_query("UPDATE `Winners` SET `$VariableName` = '".$$VaiableName."' Where `Year` = '2008' ") or die(mysql_error()); You misspelled it. You mean $$VariableName in the query, not $$VaiableName. xD Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613916 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 YES Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613919 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 YES COOL. CAPS LOCK IS FUN. Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613920 Share on other sites More sharing options...
chiefrokka Posted August 11, 2008 Author Share Posted August 11, 2008 Try $VariableName = 'Week'.$Current_Week.'_Q1_Winner'; $$VariableName = $row['Name']; mysql_query("UPDATE `Winners` SET `$VariableName` = '".$$VaiableName."' Where `Year` = '2008' ") or die(mysql_error()); You misspelled it. You mean $$VariableName in the query, not $$VaiableName. xD awesome, that worked Sasa... thank you. now let me go read up on this double $$ to fully understand what I'm doing. lol Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613961 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 awesome, that worked Sasa... thank you. now let me go read up on this double $$ to fully understand what I'm doing. lol $foo = 'Hello World'; $bar = 'foo'; echo $$bar; http://php.net/manual/en/language.variables.variable.php HTH Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613963 Share on other sites More sharing options...
sasa Posted August 11, 2008 Share Posted August 11, 2008 YES COOL. CAPS LOCK IS FUN. sorry Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613967 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 YES COOL. CAPS LOCK IS FUN. sorry Aww, that wasn't an intentional caps lock? D: Quote Link to comment https://forums.phpfreaks.com/topic/119183-creating-a-variable-from-another-variables-value/#findComment-613970 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.