1

I have this chunk of code. It works, but I'm sure there is a simpler and more logical way of doing it. And just to save questions, $seriespart instead of $row["SeriesPart"] is specific because of previous code.

// normal titling convention is Number - Series - Series Part - Sermon
if ($row["Number"] && $row["Series"] && $row["SeriesPart"] && $row["Sermon"]) $title = $row["Number"]." - ".$row["Series"]." - Pt. ".$seriespart." - ".$row["Sermon"];
    // but sometimes a series doesn't have sermon titles
    else if ($row["Number"] && $row["Series"] && $row["SeriesPart"]) $title = $row["Number"]." - ".$row["Series"]." - Pt. ".$seriespart;
        // series that are in process don't have product numbers
        else if ($row["Series"] && $row["SeriesPart"] && $row["Sermon"]) $title = $row["Series"]." - Pt. ".$seriespart." - ".$row["Sermon"];
            // but sometimes a series doesn't have sermon titles
            else if ($row["Series"] && $row["SeriesPart"]) $title = $row["Series"]." - Pt. ".$seriespart;
                // and we don't want product singles just showing a sermon title (it should say S74 - Title as a distinction from other oddball sermons)
                // note the substr() as we want it to say S71 and not S0171
                else if ($row["Number"] >= "0100" && $row["Number"] <= "0199") $title = "S".substr($row["Number"], 2)." - ".$row["Sermon"];
                    // just show the bloody sermon title already
                    else $title = $row["Sermon"];

We have a bunch of sermons. Those sermons are, usually, part of a series. If a series is finished, it will have a series number, series title, series part, and optionally sermon title, sermon part, subtitle, and subtitle part. If a series isn't finished, there won't be a series number. Sometimes, there is no sermon title. If it's not part of a series, then it can be an actual product unto itself. Other times, it's just an oddball sermon that isn't a part of anything.

Examples...

Made Healed (sermon only, not part of a series, oddball)

S45 - Sucker Or Sower? (single that is a product)

The Helper - Pt. 1 - Do You Need Help? (series in progress, no series number)

0513 - Harden Not Your Heart - Pt. 2 (completed series, no sermon title)

1101 - The Honor Of God - Pt. 11 - How God Honors Us - Pt. 5 - By Using Us - Pt. 3 (comleted series, sermon title - sermon part - subtitle - subtitle part)

Any ideas on how to clean up the code so I don't have to keep repeating the same information with if, else if, else?

6
  • How is the data stored? For something this (seemingly) complicated you might have better luck with XSL. Commented May 1, 2012 at 15:35
  • 4
    Why indent each condition as if it's within the scope of the previous (and not as siblings, which they are)? Commented May 1, 2012 at 15:37
  • @ethrbunny The data is coming from MSSQL. Commented May 1, 2012 at 16:17
  • @Mr.Disappointment Hmmm...I guess that's how I've always done it. It just let me see that they were together (like doing } else { instead of putting them on individual lines). Is that considered a bad practice? Commented May 1, 2012 at 16:21
  • @jeremyharris I will do click the up arrow for usefullness. I didn't realize the check was to be clicked, as well. Commented May 1, 2012 at 16:24

2 Answers 2

1

I think this will do what you want:

$parts = array();
if ($row["Number"] && ($row["Number"] < "Music01" || $row["Number"] > "Music99"))
{
    if ($row["Number"] >= "0100" && $row["Number"] <= "0199") $parts[] = "S". substr($row["Number"], 2);
    else $parts[] = $row["Number"];
}

if ($row["Series"]) $parts[] = $row["Series"];
if ($row["SeriesPart"]) $parts[] = "Pt. $seriespart";
if ($row["Sermon"]) $parts[] = $row["Sermon"];

$title = implode(" - ", $parts);
Sign up to request clarification or add additional context in comments.

5 Comments

I think that did it. I wouldn't have thought about creating an array for the parts.
"Pt. $seriespart"; I figured that would need to be "Pt. ".$seriespart; but it works just fine. Can variables be included in "" while functions can't?
@doubleJ: That's right. You can put a variable directly inside of a double-quoted string. I usually go a step further, and enclose the variable in curly braces, like this: "Pt. {$seriespart}", and this helps for clarity on where the variable name ends. Functions cannot be done this way.
Included in that database are music albums that are set up in the same Number - Series (album title) - SeriesPart (track) - Sermon (song name) format. I don't want to display the product number for music albums so I did if ($row["Number"] >= "Music01" && $row["Number"] <= "Music99") $parts[] = NULL;. It wasn't specified earlier as I wasn't showing the product number the last time I tested the music albums.
@doubleJ: See updated answer (first if-condition). Instead of appending a NULL onto the array, just skip that part altogether.
0
$title = '';
if ($row["Series"] && $row["SeriesPart"]) {
    if ($row["Number"]) {
        $title .= $row["Number"]." - ";
    // series that are in process don't have product numbers
    }
    $title .= $row["Series"]." - Pt. ".$seriespart;
    if ($row["Sermon"]) {
        $title .= " - ".$row["Sermon"];
    }
// and we don't want product singles just showing a sermon title (it should say S74 - Title as a distinction from other oddball sermons)
// note the substr() as we want it to say S71 and not S0171
} elseif ($row["Number"] >= "0100" && $row["Number"] <= "0199") {
    $title = "S".substr($row["Number"], 2)." - ".$row["Sermon"];
}
// just show the bloody sermon title already
else {
    $title = $row["Sermon"];
}

And if you not want partialy creation

if ($row["Series"] && $row["SeriesPart"]) {
    if ($row["Number"]) {
        if ($row["Sermon"]) {
            $title = $row["Number"]." - ".$row["Series"]." - Pt. ".$seriespart." - ".$row["Sermon"];
            // but sometimes a series doesn't have sermon titles
        } else {
            $title = $row["Number"]." - ".$row["Series"]." - Pt. ".$seriespart;
        }
    // series that are in process don't have product numbers
    } else {
        if ($row["Sermon"]) {
            $title = $row["Series"]." - Pt. ".$seriespart." - ".$row["Sermon"];
        // but sometimes a series doesn't have sermon titles
        } else {
            $title = $row["Series"]." - Pt. ".$seriespart;
        }
    }
// and we don't want product singles just showing a sermon title (it should say S74 - Title as a distinction from other oddball sermons)
// note the substr() as we want it to say S71 and not S0171
} elseif ($row["Number"] >= "0100" && $row["Number"] <= "0199") {
    $title = "S".substr($row["Number"], 2)." - ".$row["Sermon"];
}
// just show the bloody sermon title already
else {
    $title = $row["Sermon"];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.