Results 1 to 3 of 3

Thread: What is the best way to loop through an array?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    621
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default What is the best way to loop through an array?

    I have created a table (using css) to display 3 different levels of pricing. The price level depends on what time of year it is. The 3 column headings display the data ranges for which the prices apply. There can be more than one date range for a given price level as shown below:

    agency car_from car_until rate_table
    Avis 2009-08-16 2009-12-15 1
    Avis 2009-01-01 2009-06-14 1
    Avis 2009-06-15 2009-08-15 2
    Avis 2009-12-15 2009-12-31 3

    I have the prices displaying properly but am having trouble displaying the date ranges in the column headings. It places the 2nd date range for the 1st column in the 2nd column, which is price level 2. Here is my attempt at code:

    Code:
    $sql = "SELECT * FROM dates WHERE `agency` = '".$agency."' AND rate_table > 0 ORDER BY rate_table";
    $result1 = mysql_query($sql,$connection) or die("Couldn't execute $sql query.";
    
    $i=0;
    while ($row = mysql_fetch_array($result1)) {
      $i++;
      $car_from[$i] = $row['car_from'];
      $car_until[$i] = $row['car_until'];
    
      $daterange[1] = $car_from[1]." - ".$car_until[1]."<br>";
      $daterange[2] = $car_from[2]." - ".$car_until[2]."<br>";
      $daterange[3] = $car_from[3]." - ".$car_until[3]."<br>";
    }
    the html to display the headings
    Code:
    <div class="tablehead">Effective Dates<br><?php echo $daterange[1]; ?></div>
      <div class="tablehead">Effective Dates<br><?php echo $daterange[2]; ?></div>
      <div class="tablehead">Effective Dates<br><?php echo $daterange[3]; ?></div>
    This is what it looks like: http://www.carrentalhawaii.com/html/rates3.php
    How do I tell it to put 2 date ranges in column one instead of moving it to column 2?
    I never did quite grasp the use of [$i]. Thanks.
    Last edited by kuau; 05-13-2009 at 11:00 PM. Reason: fixed a typo

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Would this work for you

    PHP Code:
    $sql "SELECT * FROM dates WHERE `agency` = '".$agency."' AND rate_table > 0 ORDER BY rate_table";
    $result1 mysql_query($sql,$connection) or die("Couldn't execute $sql query.";
    $i=0;
    $daterange[1]="":
    $daterange[2]="":
    $daterange[3]="":

    while (
    $row mysql_fetch_array($result1)) {
      
    $i++;
      
    $car_from[$i] = $row['car_from'];
      
    $car_until[$i] = $row['car_until'];
      
    if (
    $row['rate_table']==1){
      
    $daterange[1] = $daterange[1].$car_from[1]." - ".$car_until[1]."<br>";
        }    
    if (
    $row['rate_table']==2){
      
    $daterange[2] = $daterange[2].$car_from[2]." - ".$car_until[2]."<br>";
        }    
    if (
    $row['rate_table']==3){
      
    $daterange[3] = $daterange[3].$car_from[3]." - ".$car_until[3]."<br>";
        }

    I think it should put all the dates with the same rates into the same heading

  3. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    kuau (05-14-2009)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    621
    Thanks
    274
    Thanked 14 Times in 14 Posts

    Default

    Dear amnesiac: Thanks a million for helping me!! It now works perfectly.

    So that you can learn from this too, I have put the results of the above code here:

    http://www.carrentalhawaii.com/html/rates3.php

    As you can see, it is an improvement, but repeats the first date in column one and still puts the date in column 2 instead. However, your code enabled me to fix it by making a few minor changes, but to be honest, I do not understand why yours doesn't work, or why mine does. I would love to understand it if someone can please explain. Here is what worked (I changed the numbers to $i):

    Code:
    if ($row['rate_table']==1){ 
      $daterange[1] = $daterange[1].$car_from[$i]." - ".$car_until[$i]."<br>"; 
        }     
    if ($row['rate_table']==2){ 
      $daterange[2] = $daterange[2].$car_from[$i]." - ".$car_until[$i]."<br>"; 
        }     
    if ($row['rate_table']==3){ 
      $daterange[3] = $daterange[3].$car_from[$i]." - ".$car_until[$i]."<br>"; 
        }
    and what it looks like working correctly:

    http://www.carrentalhawaii.com/html/rates.html

    I couldn't have done this without you and now I can get on to the next thing.

    Mahalo plenty! e

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
  •