试图简化我的if语句

时间:2013-06-17 05:06:16

标签: php arrays if-statement foreach

以下mysqli查询将始终提取一个结果。但是,在查看我的代码后,我觉得必须有一个更好的方法来简化状态下拉html部分。正如您所看到的,我有一堆if语句和一个数组。我这样做是为了让我可以在html中将selected设置为当前的下拉视图。任何人都可以帮助我简化这个吗?

$query = mysqli_query($mysqli, "SELECT * From referrals WHERE id = '".$edit."';");

  while($row = mysqli_fetch_array($query))

     {
        $editstatus = $row['status'];
     }

            if($editstatus == "N")
              {
                $estatus = "N/A";
              }

            if($editstatus == "I")
              {
                $estatus = "Installation Comp";
              }

            if($editstatus == "SI")
              {
                $estatus = "Site Inspection";
              }

            if($editstatus == "S")
              {
                $estatus = "Sold";
              }

            if($editstatus == "C")
              {
                $estatus = "Cancelled";
              }

            if($editstatus == "P")
              {
                $estatus = "Press/Follow-Up";
              }

            if($editstatus == "W")
              {
                $estatus = "Being Installed";
              }

                  $bstatus[] = "N/A";
                  $bstatus[] = "Installation Comp";
                  $bstatus[] = "Site Inspection";
                  $bstatus[] = "Sold";
                  $bstatus[] = "Cancelled";
                  $bstatus[] = "Press/Follow-Up";
                  $bstatus[] = "Being Installed";
?>

    <div class="status"><label for="edit_status">Edit Status</label>
    <select id="edit_status" name="edit_status">

<?php

                               foreach($bstatus as $cstatus) {

                                         if($cstatus == "N/A")
                                           {
                                             $dstatus = "N";
                                           }

                                         if($cstatus == "Installation Comp")
                                           {
                                             $dstatus = "I";
                                           }

                                         if($cstatus == "SI")
                                           {
                                             $dstatus = "Site Inspection";
                                           }

                                         if($cstatus == "Sold")
                                           {
                                             $dstatus = "S";
                                           }

                                         if($cstatus == "Cancelled")
                                           {
                                             $dstatus = "C";
                                           }

                                         if($cstatus == "Press/Follow-Up")
                                           {
                                             $dstatus = "P";
                                           }

                                         if($cstatus == "Being Installed")
                                           {
                                             $dstatus = "W";
                                           }

?>

    <option <?php if($cstatus == $estatus) { echo "selected=\"selected\""; } ?> value="<?php echo $dstatus; ?>"><?php echo $cstatus ?></option>

<?php
                                                             }
?>

    </select>       
    </div>

正如我所提到的,在查看此代码后,我知道必须有更好的方法来做到这一点我只是不知道如何。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

为什么不为estatuses创建一个php关联的editatatuses数组呢?

http://php.net/manual/en/language.types.array.php

$statuses = array(
        "N"  => "N/A",
        "I" => "Installation Comp",
        ...
    )

答案 1 :(得分:0)

switch

的优秀候选人
$editstatus = $row['status'];
switch ($editstatus)
{
    case 'N':
        $estatus = 'N/A';
        break;
    case 'I':
        $estatus = 'Installation Comp';
        break;
    // Fill in the rest here ...
    default:
        // None of my case statements were matched
        break;
}

答案 2 :(得分:0)

使用以下方法之一

方法1

switch ($editstatus) {
    case "N":
        $dstatus = "N";
        break;
    case "I":
        $estatus = "Installation Comp"
        break;
//rest of your code goes here
}

http://php.net/manual/en/control-structures.switch.php

方法2

关联数组也适合

$array = array(
    "N" => "N",
    "I" => "Installation Comp",
    ..
    ..
);

http://php.net/manual/en/language.types.array.php