Jump to content
xisto Community
Sign in to follow this  
mandla

Variable Variables changing a variable name from x1 to x2 to x3 dynamically

Recommended Posts

This sounds like a confusing ask, but I know what I am looking to achieve so please be patient if i seem a little confused. If you read slowly you will work out what Im trying to achieve.basically I have a database with data already loaded. We will assume there are two columns one has fruit and the other has the fruits coloreg. FRUIT COLOR Apple Red Banana Yellow Orange orange Grape Purplewhat I want to achieve from this point is have a variable called fruitwhich i declare using the $Fruit= functionright so far this is clear. but then here if the tricky part. What I want is for fruit to dynamically generate a number for for itself. Before you get confused I want fruit to become $fruit1 and then assign itself the value red (red is a dynamic value as picked up from the databade) if the first color was Blue $fruit1 would have become bluethen when it gets to the <?php } while ....?> assume this is correct syntax and loops the script to next value which is banana / yellow this time around $fruit1= should change itself to $fruit2 and assing its self to be the value of second color in this case being yellow. The sweep will continue again $fruit2 becoming $fruit3 and assigng its self to the color of the third record. until the loop ends. Such that if I wrote <?php echo $fruit3;?> i would get the value of the third color in this case example the color is orange. In other words all the generated variables should maintain their value and not have it overritten with a new value.I dont know if this makes much sense. I have tried explainign it as best as I can and hope its clear enuff. please contribute if you know the answer. I am tearing my hair out. Found Variable Variables on google but they not really making sense to me right now. any help would be greatly appreciated.I wanna use a php script using the <?php do { ?> command to loop the vari

Share this post


Link to post
Share on other sites

I think you can use PHP Arrays to achieve this effect, variable variables aren't needed here, at least I think so, if I understood you correctly.

you can set your array:

$array = array();

Also, it depends how you get your data of fruits and colors, is it from a mysql database? if so, it can be even easier to do, but lets say I don't know ho w you get data, so a custom way top do would be to use that array:

$array['banana'] = 'yellow';$array['orange'] = 'orange';

It is one way to do, you set an array key the fruit name, and array value for that key the colour, or you can do it with numbers, if needed, just read how to use arrays..

So to continue, you can do:

foreach ($array as $fruit => $color){   echo $fruit .' has the color '. $color;}

it will go through all your array values, you can set up a counter if you need some numbers in a varible, for example: $counter = 0; and in the loop you can $counter++;

Another way to do it is to use a different methods with array:

$array[] = array('banana', 'yellow');$array[] = array('orange', 'orange');

and to get the values, you can just:

echo $array[0][0]; // Bananaecho $array[0][1]; // Yellowecho $array[1][0]; // Orangeecho $array[1][1]; // Orange

or you can do it like this:

$array[] = array('fruit' => 'banana', 'color' => 'yellow');$array[] = array('fruit' => 'orange', 'color' => 'orange');echo $array[0]['fruit']; // Bananaecho $array[0]['color']; // Yellowecho $array[1]['fruit']; // Orangeecho $array[1]['color']; // Orange

So it will increment +1 if you set the values using [] so you can use a for loop:

for ($i = 0; $i < count($array); $i ++) {   echo $array[$i]['fruit'];   echo ' has the color ';   echo $array[$i]['color'];}

There are different ways to do it, just read on PHP arrays tutorial how to achieve things. :)

I didn't test the code, but I think I didn't make any mistakes.

Furthermore, I've found a way to do what you asked for using variable variables, at least I think so, but an array solution is better in my opinion:

http://stackoverflow.com/questions/543792/php-variable-variables

Using variable variables can make other programmers who will use your code think a little, usually it's better to write code which is easy to read and understand without a lot of comments, even if it's 10 lines and not with a "better" method which is 3 lines, but it depends what you're programming, performance is a preference usually and readability that it would be code only for you to understand :D
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

Oh thank you ever so much buddy. I took your advice or writting a longer code which is easier to understand then the short 2lines which are better and yet confusing. eventually I did use the loop whic basicallywas like thisthe code may need correcting but I will give you a rough idea of what i went with eventually.i declared a variable as = 1eg. $x = 1 (declare value of $x so computer has proper figue assigned to $x)<?php } while ....?> (began the loop funtion)$variable = "fruit".x ; (this tells script that $varible = fruit1as that is value of x at this point)$$variable = $row_fruit['color']; I thank set the value of variable in this case color to become a variable by adding $ on $variable at this point $$variable is same value as $fruit1. which then means $color1 is equal to value of database row color first record.$x = $x + 1; we now increment the value of $X to 2 for the next sweep. which means when it get to "fruit".$x this time around x=2 hence the result will be a variable fruit2this can loop till all the selected records in the datatbase have been exhausted and then becuase they have been asigned to variable they can then be echoed outside to the loop scriipt or manipulated in later stages of the scripthence you can now echo $fruit1 or echo $fruit4 and get different value but that correspond to the database query results.i know it may sound a bit daunting to undestand but I hope you do understand.if you need me to copy the exact script for you I will kindly do so just ask and I will upload or rather copy and paste for your perusal.thank you for your advice though it helped a lot when I was getting all stressy cause i seemed lost and looked like I wasnt aware of what Im trying to do anymore.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.