Results 1 to 10 of 10

Thread: How to use variables in arrays

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

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

    Code:
    $rate.$x  = $row['rate.$x'];
    doesn't work. I tried $rate[1] too but neither worked. Any help would be greatly appreciated. Mahalo, e
    Last edited by kuau; 03-18-2009 at 08:07 PM. Reason: to simplify the question

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Should be:

    PHP Code:
    $rate[$x] = $row[$rate[$x]]; 
    I think... Can you provide all the code if it doesn't work?
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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
    Code:
    $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

    Code:
    $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:

    Code:
    $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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here:

    PHP Code:
    $agency "Avis";
    $dayrate $agency."_dayrate";

    ${
    $dayrate} = $row['rate'.$x]; 
    Is 'dat ok?
    Jeremy | jfein.net

  5. The Following User Says Thank You to Nile For This Useful Post:

    kuau (03-19-2009)

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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.

    Code:
    while ($row = mysql_fetch_array($result)) {
      $ratetable = $row['rate_table'];
      echo $ratetable;  
    }

  7. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:

    PHP Code:
    $rate_arr = array();
    while (
    $row mysql_fetch_array($result)) {
      
    $rate_arr[] = $ratetable $row['rate_table'];
    }
    echo 
    implode("<br />"$rate_arr); 
    Jeremy | jfein.net

  8. The Following User Says Thank You to Nile For This Useful Post:

    kuau (03-19-2009)

  9. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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:

    Code:
    $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.
    Last edited by kuau; 03-19-2009 at 05:47 PM. Reason: correction

  10. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    The reason for this is because when doing:
    Code:
      $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 '[]'
    Jeremy | jfein.net

  11. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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!

  12. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What happens when you do one?
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •