1

我有这段代码。它有效,但我确信有一种更简单、更合乎逻辑的方法。而且只是为了保存问题,$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 - 吸盘还是播种?(单是产品)

助手 - 铂。1 - 您需要帮助吗?(系列进行中,无系列号)

0513 - 不要让你的心变硬 - Pt。2(已完成系列,无讲道标题)

1101 - 上帝的荣耀 - 铂。11 - 上帝如何荣耀我们 - Pt。5 - 通过使用我们 - 铂。3(完成系列,讲道标题-讲道部分-副标题-副标题部分)

关于如何清理代码的任何想法,这样我就不必用 if、else if、else 重复相同的信息?

4

2 回答 2

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);
于 2012-05-01T15:44:24.640 回答
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"];
}
于 2012-05-01T15:41:11.630 回答