1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Shorter PHP (help needed)

Discussion in 'PHP' started by MrLeN, Jun 29, 2013.

  1. #1
    I am trying to make a navigation rule for a website I am playing with.

    I have started adding a navigation rule for each city of Victoria (a state of Australia), but I only got 3 done, and I stopped and said: "There must be a quicker way of doing this".

    Can anyone help me to make this code shorter, and to include the cities that I have not yet added (which are commended out at the bottom)?

    Code (Text):
    1.  
    2. <?php
    3. if ($state == "Victoria") {
    4.   $nav_city = "Melbourne";
    5.  
    6.   if ($city == "Melbourne") {
    7.     $nav_melbourne = "nav_highlighted";
    8.   }
    9.    else {
    10.      $nav_melbourne = "nav_off";
    11.    }
    12.   echo '<li class="'.$nav_melbourne.'" style="border-top: 1px solid #990000; border-bottom: 1px solid #990000;"><a href="/?state='.$state.'&city=melbourne">Melbourne</a></li>';
    13.  
    14.    if ($city == "Ararat") {
    15.     $nav_ararat = "nav_highlighted";
    16.   }    
    17.    else {
    18.      $nav_ararat = "nav_off";
    19.    }
    20.   echo '<li class="'.$nav_ararat.'"><a href="/?state='.$state.'&city=ararat">Ararat</a></li>';
    21.    
    22.    if ($city == "Bairnsdale") {
    23.     $nav_bairnsdale = "nav_highlighted";
    24.   }    
    25.    else {
    26.      $nav_bairnsdale = "nav_off";
    27.    }
    28.   echo '<li class="'.$nav_bairnsdale.'"><a href="/?state='.$state.'&city=bairnsdale">Bairnsdale</a></li>';
    29.  
    30. /*
    31.  
    32. Benalla
    33. Ballarat
    34. Bendigo    
    35. Dandenong
    36. Frankston
    37. Geelong
    38. Hamilton
    39. Horsham
    40. Melton
    41. Moe
    42. Morwell
    43. Mildura
    44. Sale
    45. Shepparton
    46. Swan Hill    
    47. Traralgon
    48. Wangaratta
    49. Warrnambool
    50. Wodonga
    51.    */
    52. }
    53.  
    54. ?>    
    55.  

    MrLeN, Jun 29, 2013 IP
  2. MrLeN

    MrLeN Active Member

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Also, I could make a loop without asking for help - but I can't make a loop because each if else has a different city name added in the actual variable name. ie:

    Code (Text):
    1.  
    2. if ($city == "Melbourne") {
    3.     $nav_melbourne = "nav_highlighted";
    4.   }
    5.   else {
    6.     $nav_melbourne = "nav_off";
    7.   }
    8.  
    Note: in the above, it could me nav_melbourne, or nav_ararat, or nav_bairnsdale or any one of the cities that are commented out. And I don't know how to make a variable - variable (if that makes sense).
    MrLeN, Jun 29, 2013 IP
  3. Pod2G

    Pod2G Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    You can do it by creating an array or loop. Loop will be needing database connection and you need to create table for it.
    Pod2G, Jun 30, 2013 IP
  4. MrLeN

    MrLeN Active Member

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    MrLeN, Jun 30, 2013 IP
  5. Pod2G

    Pod2G Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    I told you that it can be done by array. anyways best of luck :)
    Pod2G, Jun 30, 2013 IP