Log in

View Full Version : How to use variables in arrays



kuau
03-18-2009, 05:47 PM
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

Nile
03-18-2009, 11:20 PM
Should be:



$rate[$x] = $row[$rate[$x]];


I think... Can you provide all the code if it doesn't work?

kuau
03-18-2009, 11:35 PM
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. :)

Nile
03-18-2009, 11:50 PM
Here:



$agency = "Avis";
$dayrate = $agency."_dayrate";

${$dayrate} = $row['rate'.$x];


Is 'dat ok?

kuau
03-19-2009, 03:57 AM
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;
}

Nile
03-19-2009, 12:01 PM
Try:



$rate_arr = array();
while ($row = mysql_fetch_array($result)) {
$rate_arr[] = $ratetable = $row['rate_table'];
}
echo implode("<br />", $rate_arr);

kuau
03-19-2009, 05:37 PM
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.

Nile
03-19-2009, 09:30 PM
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 '[]'

kuau
03-19-2009, 09:38 PM
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! :)

Nile
03-19-2009, 10:34 PM
What happens when you do one?