Jump to content

How can I simplify this code?


tech0925

Recommended Posts

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. Can anyone help me to make this simplier?

$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. Any help would be greatly apprecited :happy-04:

 

Link to comment
Share on other sites

You could employ the $bstatus array by making it an associative array.

<?php
//ARRAY OF STATUS CODES
$bstatus['N']  = 'N/A';
$bstatus['I']  = 'Installation Comp';
$bstatus['SI'] = 'Site Inspection';
$bstatus['S']  = 'Sold';
$bstatus['C']  = 'Cancelled';
$bstatus['P']  = 'Press/Follow-Up';
$bstatus['W']  = 'Being Installed';
?>

Then you would only need to run a simple test to see of the status code exists before using the corresponding value

<?php
//GET VARIABLE FOR TESTING 'editstatus'
$editstatus = $_GET['editstatus'];
 
//IF THE 'editstatus' KEY EXISITS IN THE ARRAY OF STATUS CODES, STORE THE CORREPSPONDING VALUE
if(array_key_exists($editstatus, $bstatus)) {
     $estatus = $bstatus[$editstatus];
}
 
//DISPLAY THE VALUE FROM 'editstatus'
print '<pre>' . print_r($estatus, true) . '</pre>';
?>

The array could also be flipped to help with the next test.

<?php
$bstatus_flip = array_flip($bstatus);
 
//GET VARIABLE FOR TESTING 'cstatus'
$cstatus = $_GET['cstatus'];
 
//IF THE 'cstatus' KEY EXISITS IN FLIPPED STATUS CODES, STORE THE CORREPSPONDING VALUE
if(array_key_exists($cstatus, $bstatus_flip)) {
     $dstatus = $bstatus_flip[$cstatus];
}
 
//DISPLAY THE VALUE FROM 'cstatus'
print '<pre>' . print_r($dstatus, true) . '</pre>';
?>
Link to comment
Share on other sites

If the query only returns one result, there is no need for the loop. Also, as suggested, make the status values an associative array for easy looping and comparing.

 

$query = mysqli_query($mysqli, "SELECT status From referrals WHERE id = '".$edit."';");
list($editstatus) = mysqli_fetch_array($query, MYSQLI_NUM);

$statusList=array(
    'N' => 'N/A'
    , 'I' => 'Installation Comp'
    //, add the rest here
);
?>
    <div class="status"><label for="edit_status">Edit Status</label>
    <select id="edit_status" name="edit_status">
<?php
      foreach($statusList as $statusCode=>$statusLabel){ 
?>
         <option <?php echo $statusCode == $editstatus?'selected="selected"':''; ?> value="<?php echo $statusCode; ?>"><?php echo $statusLabel; ?></option>

<?php
      }
?>
  </select>       
    </div>
Link to comment
Share on other sites

I agree with the lookup array if you don't have the ability to add another table to the database.  Normally you would have a status table with a primary key and the status text.  Then you would join this table in your query so that you would get the text.  It's also easier to maintain the text in the database than in the code.

Link to comment
Share on other sites

For Select lists, unless you will only have one select list on your site, you should consider creating a function to create the select list options. I typically create a function that takes an array of values for the options and a second (optional) parameter for the option to be pre-selected. You can then reuse that function for all your select lists.

<?php

function createOptions($optionList, $selectedValue = false)
{
    $optionsHTML = '';
    foreach($optionList as $value => $label)
    {
        $selected = ($value === $selectedValue) ? ' selected="selected"' : '';
        $optionsHTML .= "<option value=\"{$value}\"{$selected}>{$label}</option>\n";
    }
    return $optionsHTML;
}

//Usage
$statusListAry = array (
    'N'  = 'N/A',
    'I'  = 'Installation Comp',
    'SI' = 'Site Inspection',
    'S'  = 'Sold',
    'C'  = 'Cancelled',
    'P'  = 'Press/Follow-Up',
    'W'  = 'Being Installed'
);

$query = mysqli_query($mysqli, "SELECT status From referrals WHERE id = '".$edit."';");
list($editstatus) = mysqli_fetch_array($query, MYSQLI_NUM);

$statusOptions = createOptions($statusListAry, $status);

?>

    <div class="status"><label for="edit_status">Edit Status</label>
    <select id="edit_status" name="edit_status">
    <?php echo $statusOptions; ?>
    </select>
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.