tech0925 Posted June 17, 2013 Share Posted June 17, 2013 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 Quote Link to comment Share on other sites More sharing options...
tech0925 Posted June 17, 2013 Author Share Posted June 17, 2013 I figured it out. I will use the switch statement instead Quote Link to comment Share on other sites More sharing options...
renfley Posted June 17, 2013 Share Posted June 17, 2013 if you declare your value a = a you can then use this! if (isset($row['status'])) { $estatus = $row['status']; } else{ $estatus = ''; } quick short and work, just need to make sure your variable is equal to value Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted June 17, 2013 Share Posted June 17, 2013 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>'; ?> Quote Link to comment Share on other sites More sharing options...
kicken Posted June 17, 2013 Share Posted June 17, 2013 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> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted June 17, 2013 Share Posted June 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 17, 2013 Share Posted June 17, 2013 (edited) 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 June 17, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
tech0925 Posted June 17, 2013 Author Share Posted June 17, 2013 Thanks everyone!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.