-1

The below mysqli query will always pull one result. However, after looking at my code I feel there must be a better way to simplify the status dropdown html section. As you can see I have a bunch of if statements and an array. I done it like that so that I can set selected in the html as the current dropdown view. Can anyone help me to make this simpler?

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

As I mentioned, after looking at this code I know there has to be a better way to do this I just don't know how. Any help would be greatly appreciated.

4

3 回答 3

2

为什么不创建一个编辑状态到状态的 PHP 关联数组?

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

$statuses = array(
        "N"  => "N/A",
        "I" => "Installation Comp",
        ...
    )
于 2013-06-17T05:08:55.507 回答
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;
}
于 2013-06-17T05:10:34.797 回答
0

使用以下方法之一

方法一

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

方法二

关联数组也适用

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

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

于 2013-06-17T05:11:38.523 回答