Need help to make PHP shorter:

Started by MrLeN, June 30, 2013, 01:27:22 AM

Previous topic - Next topic

MrLeN

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
  */
}

?>
   

MrLeN

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";
  }


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

Sir Osis of Liver


You have to load the city names into an array, and loop through the array.

But I'm too tired to get the code right.

Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

MrLeN

I don't know how to do that. Sounds good though, lol.

The Craw

Something like this perhaps. Obviously would need to be modified to fit your needs, but Krash has the right idea.


<?php
$cities 
= array(
'Benalla''Ballarat''Bendigo',
'Dandenong''Frankston''Geelong''Hamilton',
'Horsham''Melton''Moe''Morwell',
'Mildura''Sale''Shepparton''Swan Hill',
'Traralgon''Wangaratta''Warrnambool''Wodonga'
);
foreach (
$cities as $place) {
$activeLink $place == $city 'nav_highlighted' 'nav_off';
echo '
<li class="'
$activeLink' menu_item" href="#">'$place'</li>';
}

MrLeN

Thanks Heaps The Craw - I had to modify it a little, but that did teh job.

I appreciate your help heaps!

This is how I modified it :)


if ($_GET['state'] == "victoria") {
$cities = array(
'melbourne', 'ararat', 'benalla', 'ballarat', 'bendigo',
'dandenong', 'frankston', 'geelong', 'hamilton',
'horsham', 'melton', 'moe', 'morwell',
'mildura', 'sale', 'shepparton', 'swan-hill',
'traralgon', 'wangaratta', 'warrnambool', 'wodonga'
);
foreach ($cities as $place) {
$activeLink = $place == $city ? 'nav_highlighted' : 'nav_off';
echo '
<li class="', $activeLink, ' menu_item">', '<a href="/?state='.$state_link.'&city='.$place.'">'.ucwords(str_replace("-", " ", $place)).'</a>', '</li>';
}
}

The Craw

Glad to help. If this worked for you, please consider marking to topic as solved (bottom left).

Advertisement: