thrash-boy Posted November 30, 2008 Share Posted November 30, 2008 ill try to keep this short and simple. I have an array (row) from a mySQL database that has the variables ModHQ, ModHJ, ModHX, ModHZ and ModWB in it. each one of them variables is one of 3 values: "t", "f", "u" (true/false/unknown). I want to create 5 new variables (colorHQ, colorHJ, colorHX, colorHZ, colorWB). there values will reflect what the above variables are. eg: if ModHQ is "t", i want colorHQ to be "green". if ModHX is "f", i want colorHX to be "red". if ModWB is "u", i want colorWB to be "black". here how im doing it: im looping through an array (models) in php. for each entry in that array (models), im dynamically creating a new variable which is "color" + model im then looking at another array (row) which came from a mySQL database to determine if the variable "Mod" + model is "t", "f" or "u" (it will be one of them 3). if its "t", im setting the value of the dynamic variable ( "color" + model ) to "green", and if its "f" or "u" another string. heres the relevent code: [hide=click me] while($row = mysql_fetch_array($result)){ $models = array("HQ", "HJ", "HX", "HZ", "WB"); for($i = 4; $i >= 0; $i--){ $sqlID = 'Mod'.$models[i]; $varName = "color".$models[i]; if($row[$sqlID] == "t"){ //this model is true, so i want to set this models color to "green" $$varName = "green"; }else if($row[$sqlID] == "f"){ //this model is false, so i want to set this models color to "red" $$varName = "red"; }else if($row[$sqlID] == "u"){ //this model is unknown, so i want to set this models color to "black" $$varName = "black"; } } echo $colorHQ; //echo's nothing. } [/hide] THE PROBLEM: I need to be able to reference the variables created which state the models color outside of the loop, where i have "echo $colorHQ;", so they can be used in the rest of the script. i could always do it with if and else statements, but that would take about 50 lines of code, compared to the 10 or 15 i have there doing the same (and easier to change) job. what do i do to change there scope? ive looked everywhere but cant find anything that works. Link to comment Share on other sites More sharing options...
ClareJonsson Posted November 30, 2008 Share Posted November 30, 2008 You could use global variables, they will be accessible to the whole script: Any variables you wish to be global just declare them at the beginning of your PHP script like this: global $Fred; This declares the variable $Fred as global, and can then be accessed by any function etc. [Assist-X] Link to comment Share on other sites More sharing options...
dsavi Posted November 30, 2008 Share Posted November 30, 2008 Or if you don't want to use the global keyword, you can use the $GLOBALS array. Some think of it as better because it's a bit more straightforward. <?php $variable = Supercow; function foo() { $variable = Megaduck; echo $variable . "/n"; echo $GLOBALS['variable']; } foo(); ?> The result would echo: Megaduck Supercow Hope that helped. Link to comment Share on other sites More sharing options...
sloter Posted November 30, 2008 Share Posted November 30, 2008 How is $colorHQ going to output anything when it is null? I came up with something that will return a multidimensional array. Could you please post your database structure? I could better help you with that. To b e honest your code was such ha slop and nonfunctional it was hard to see what you were doing. [hide=The Code] <?php $x = 0; $colorHQ = array(); while($row = mysql_fetch_array($result)) { $x++; $models = array("HQ", "HJ", "HX", "HZ", "WB"); foreach($models as $model) { switch($row['Mod' . $model]) { case 't': $colorHQ[$x][$i] = "green"; break; case 'f': $colorHQ[$x][$i] = 'red'; break; case 'u': $colorHQ[$x][$i] = 'black'; break; } } print_r($colorHQ); } ?> [/hide] Link to comment Share on other sites More sharing options...
thrash-boy Posted November 30, 2008 Author Share Posted November 30, 2008 How is $colorHQ going to output anything when it is null? I came up with something that will return a multidimensional array. Could you please post your database structure? I could better help you with that. To b e honest your code was such ha slop and nonfunctional it was hard to see what you were doing. $colorHQ is not null. thats what these 2 lines are doing: $varName = "color".$models; $$varName = "green"; lets assume the loop is at the HQ entry in the array. it makes a variable which would equal 'colorHQ' (because currently $models is 'HQ') called $varName. It then makes another variable which is $$varName. It may be hard to understand at first, php first calculates what $varName equals, so it then has this: $('colorHQ') = "green"; which is equal to: $colorHQ = "green"; which is declaring colorHQ as equaling 'green'. if $varName has a different value (eg colorHZ) then the variable name will be different. Link to comment Share on other sites More sharing options...
sloter Posted November 30, 2008 Share Posted November 30, 2008 How is $colorHQ going to output anything when it is null? I came up with something that will return a multidimensional array. Could you please post your database structure? I could better help you with that. To b e honest your code was such ha slop and nonfunctional it was hard to see what you were doing. $colorHQ is not null. thats what these 2 lines are doing: $varName = "color".$models; $$varName = "green"; lets assume the loop is at the HQ entry in the array. it makes a variable which would equal 'colorHQ' (because currently $models is 'HQ') called $varName. It then makes another variable which is $$varName. It may be hard to understand at first, php first calculates what $varName equals, so it then has this: $('colorHQ') = "green"; which is equal to: $colorHQ = "green"; which is declaring colorHQ as equaling 'green'. if $varName has a different value (eg colorHZ) then the variable name will be different. Opps didn't see your two $$ in there, sorry about that. Even though a array uses more memory that would probally be better for this in my opinion. Do you mind posting your DB structure? Link to comment Share on other sites More sharing options...
thrash-boy Posted December 1, 2008 Author Share Posted December 1, 2008 Opps didn't see your two $$ in there, sorry about that. Even though a array uses more memory that would probally be better for this in my opinion. Do you mind posting your DB structure? thats alright everyone misses something every now and then. im hoping this is what you mean by structure, im new to this database/php thing. all of the tables are the same as the one pictured, except the 'UNVALIDATED' one, which has a few more values. < CLICK! Link to comment Share on other sites More sharing options...
sloter Posted December 1, 2008 Share Posted December 1, 2008 Opps didn't see your two $$ in there, sorry about that. Even though a array uses more memory that would probally be better for this in my opinion. Do you mind posting your DB structure? thats alright everyone misses something every now and then. im hoping this is what you mean by structure, im new to this database/php thing. all of the tables are the same as the one pictured, except the 'UNVALIDATED' one, which has a few more values. < CLICK! That is exactly what i needed. :) I see what your doing now. The code i posted above should work. You could try putting that in a function and return the $colorHQ and then running the function at the top of the script? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now