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

    
    <?php
    if ($state == "Victoria") {
      $nav_city = "Melbourne";
     
      if ($city == "Melbourne") {
        $nav_melbourne = "nav_highlighted";
      }
       else {
         $nav_melbourne = "nav_off";
       }
      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>';
      
       if ($city == "Ararat") {
        $nav_ararat = "nav_highlighted";
      }    
       else {
         $nav_ararat = "nav_off";
       }
      echo '<li class="'.$nav_ararat.'"><a href="/?state='.$state.'&city=ararat">Ararat</a></li>';
       
       if ($city == "Bairnsdale") {
        $nav_bairnsdale = "nav_highlighted";
      }    
       else {
         $nav_bairnsdale = "nav_off";
       }
      echo '<li class="'.$nav_bairnsdale.'"><a href="/?state='.$state.'&city=bairnsdale">Bairnsdale</a></li>';
     
    /*
     
    Benalla
    Ballarat
    Bendigo    
    Dandenong
    Frankston
    Geelong
    Hamilton
    Horsham
    Melton
    Moe
    Morwell
    Mildura
    Sale
    Shepparton
    Swan Hill    
    Traralgon
    Wangaratta
    Warrnambool
    Wodonga
       */
    }
     
    ?>    
    
    Code (markup):
     
    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:

    
    if ($city == "Melbourne") {
        $nav_melbourne = "nav_highlighted";
      }
      else {
        $nav_melbourne = "nav_off";
      }
    
    Code (markup):
    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