View Full Version : How to use variables in arrays
How do I define the variable for an array so that the variable part of it parses correctly?
Table1 tells me which rate to use, which can be 1,2,or 3 ($x). The fields in table2 are rate1, rate2, rate3. I am able to get the SQL query to reference the right field, but how do I write the variable for the array?
$rate.$x = $row['rate.$x'];
doesn't work. I tried $rate[1] too but neither worked. Any help would be greatly appreciated. Mahalo, e
Should be:
$rate[$x] = $row[$rate[$x]];
I think... Can you provide all the code if it doesn't work?
Thanks Nile. Maybe I am trying to do something the wrong way, but I am trying to concatenate the number to the end of the variable name, ie. if the applicable rate table# is 2, then I want to say
$dayrate = $row['rate2']
In other words, I take the rate from a different field depending on the rate table used. If the rate table is 3, then it would be
$dayrate = $row['rate3'];
I would also like to use a variable within a variable name. Is that possible? For example, if $agency = Avis then I would like the above to be Avis_dayrate =... or if $agency = Alamo, then it would be $Alamo_dayrate = ...
I'm tyring to write modular code rather than copy and paste the same chunk of code 5 times with the agency different each time. There must be a way to do this, but I'm just missing it. I would like to be able to say:
$agency = "Avis"; include ('calculate_price.php');
and end up with variables for all 5 agencies so I can then choose the best price.
My head hurts. :)
Here:
$agency = "Avis";
$dayrate = $agency."_dayrate";
${$dayrate} = $row['rate'.$x];
Is 'dat ok?
Dear Nile: That was what I needed... thanks a million!
The code below produces an array of 5 numbers (one for each car agency). Can you please tell me how to reference these numbers. I thought $ratetable[1] would give me the Avis rate table (second one) but it comes out blank & gives an error message. Thanks.
while ($row = mysql_fetch_array($result)) {
$ratetable = $row['rate_table'];
echo $ratetable;
}
Try:
$rate_arr = array();
while ($row = mysql_fetch_array($result)) {
$rate_arr[] = $ratetable = $row['rate_table'];
}
echo implode("<br />", $rate_arr);
Thanks, Nile. I'm really trying hard to understand arrays so I tried to expand it, but what am I doing wrong? I want it to list the agency name with the appropriate rate table just to see if I understand, but it didn't work, so I'm not quite there yet:
$rate_arr = array();
while ($row = mysql_fetch_array($result)) {
$rate_arr[][] = $agency = $row['agency'];
$rate_arr[][] = $ratetable = $row['rate_table'];
}
echo implode("<br>", $rate_arr);
It just lists the word "Array" 10 times.
The reason for this is because when doing:
$rate_arr[][] = $agency = $row['agency'];
$rate_arr[][] = $ratetable = $row['rate_table'];
Your making a new array value in $rate_arr, and in that your making another value. Try only one '[]'
I tried only one first and it didn't work which is why I tried 2. I thought it became a multidimensional array (?) when I added the agency name so I was trying to say... oh, I just realized what I did wrong. I'll try again and let you know what happens. Thanks! :)
What happens when you do one?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.