PHP代码清理(多个如果,否则如果,否则)

时间:2012-05-01 15:31:35

标签: php

我有这大块代码。它有效,但我确信有一种更简单,更合乎逻辑的方法。只是为了保存问题,$seriespart而不是$row["SeriesPart"]是特定的,因为以前的代码。

// 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"];

我们有一堆布道。这些布道通常是一系列的一部分。如果一个系列完成,它将有一个系列号,系列标题,系列部分,以及可选的布道标题,布道部分,副标题和字幕部分。如果系列没有完成,则不会有序列号。有时,没有布道标题。如果它不是系列的一部分,那么它本身就可以是一个实际的产品。其他时候,这只是一个奇怪的布道,不是任何事情的一部分。

...实例

  

Made Healed(只有布道,不属于连续剧,奇怪的一部分)

     

S45 - Sucker或Sower? (单品即产品)

     

助手 - Pt。 1 - 你需要帮助吗? (系列正在进行中,没有系列号)

     

0513 - 硬化不是你的心 - Pt。 2(完成系列,没有布道标题)

     1101 - 上帝的荣耀 - Pt。 11 - 上帝如何尊重我们 - Pt。 5 - 使用我们 - Pt。 3(comleted系列,布道标题 - 布道部分 - 副标题 - 副标题部分)

关于如何清理代码的任何想法,所以我不必用if,else if,else继续重复相同的信息?

2 个答案:

答案 0 :(得分:1)

我认为这会做你想做的事情:

$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);

答案 1 :(得分: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"];
}

如果你不想创作部分

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