5 replies [Last post]
roscoe
roscoe's picture
Offline
Enthusiast
Last seen: 11 years 37 weeks ago
Joined: 2006-03-09
Posts: 138
Points: 0

New to PHP and I'm trying to condense the IF statement below. The only thing changing is the class="current" element depending on what the value of the variable $page is. I figure there must be a simpler way that I just don't see:

if ($page == "past") {
echo 'mission statement | past | present | future';
} else if ($page == "present") {
echo 'mission statement | past | present | future';
} else if ($page == "future") {
echo 'mission statement | past | present | future';
} else {
echo 'mission statement | past | present | future';
}

Tags:
Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 24 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

You could do it this

You could do it this way:


  • >mission statement

  • >past

  • >present

  • >future


How to get help
Post a link. If you can't post a link, jsFiddle it.
My blog | My older articles | CSS Reference

roscoe
roscoe's picture
Offline
Enthusiast
Last seen: 11 years 37 weeks ago
Joined: 2006-03-09
Posts: 138
Points: 0

Now that looks a whole lot

Now that looks a whole lot better, but this happens to be inside of a bigger switch statement that controls the sub navigation:

<?php

switch ($folder) {
case "personal":
if ($page == "past") {
echo 'mission statement | past | present | future';
} else if ($page == "present") {
echo 'mission statement | past | present | future';
} else if ($page == "future") {
echo 'mission statement | past | present | future';
} else {
echo 'mission statement | past | present | future';
}
break;
case "resume":
echo 'html | pdf | doc | txt';
break;
case "web_sites":
echo 'web sites | web layouts';
break;
case "art_gallery":
echo 'photography | photomanipulation | other work';
break;
default:
break;
}

?>

So what would be the best way to apply your code above?

gary.turner
gary.turner's picture
Offline
Moderator
Dallas
Last seen: 10 hours 25 min ago
Dallas
Timezone: GMT-5
Joined: 2004-06-25
Posts: 9754
Points: 3836

First things first. You're

First things first. You're building a list of links, and that cannot be contained in a span element.

You might consider creating some string variables, and have your choices simply echo the variable. The variables could be in a separate included file to leave the logic uncluttered.

cheers,

gary

If your web page is as clever as you can make it, it's probably too clever for you to debug or maintain.

Tyssen
Tyssen's picture
Offline
Moderator
Brisbane
Last seen: 5 years 24 weeks ago
Brisbane
Timezone: GMT+10
Joined: 2004-05-01
Posts: 8201
Points: 1386

Taking what you've already

Taking what you've already got, it could be:

switch ($folder) {
case "personal": ?>

  • >mission statement

  • >past

  • >present

  • >future

  • <?php
    break;
    case "resume": ?>
  • html

  • pdf

  • doc

  • text

  • <?php
    break;
    case "web_sites": ?>
  • web sites

  • web layouts

  • <?php
    break;
    case "art_gallery": ?>
  • photography

  • photomanipulation

  • <?php
    break;
    default:
    break;
    }
    ?>

    Although you'd probably be better creating a series of arrays and then using foreach statements to print out the links rather than having to duplicate the HTML for each different case.

    How to get help
    Post a link. If you can't post a link, jsFiddle it.
    My blog | My older articles | CSS Reference

    roscoe
    roscoe's picture
    Offline
    Enthusiast
    Last seen: 11 years 37 weeks ago
    Joined: 2006-03-09
    Posts: 138
    Points: 0

    Problem Solved

    I figured everything out. Below is the code I used. Both $folder and $page are defined in code earlier on from $_GET if you wondering how I got those. I just did it for the first switch case but once I input the data into the other it will work as well. Thanks for all of your help.

    <?php

    switch ($folder) {
    case "personal":
    $array = array("mission statement" => "personal/", "past" => "personal/past", "present" => "personal/present", "future" => "personal/future");
    break;
    case "resume":
    $array = array("html" => "#", "pdf" => "#", "doc" => "#", "txt" => "#");
    break;
    case "web_sites":
    $array = array("web sites" => "#", "web layouts" => "#");
    break;
    case "art_gallery":
    $array = array("photography" => "#", "photomanipulation" => "#", "other work" => "#");
    break;
    default:
    $array = "";
    break;
    }

    if ($array != "") {
    $result = count($array);
    $count = 1;
    $current = str_replace("_", " ", $page);

    foreach ($array as $key => $value) {
    echo '' . $key . '';

    if ($count < $result) {
    echo ' | ';
    }
    $count += 1;
    }
    }
    ?>