Jump to content

creating a variable from another variables value


chiefrokka

Recommended Posts

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"
?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.