Russia Posted July 25, 2013 Share Posted July 25, 2013 Hey guys I have a table with a date selector that I am trying to add a current date option UNLESS there are rows already present with that date. Here is how it looks: Today is the 25th yet it shows the date for the 20th since there are rows present with the 07/20/2013 date in it. How would I do it so it shows the current date? <div class="lookup"> <form action="index.php" method="get"> <select name="exam_date" ONCHANGE="location = this.options[this.selectedIndex].value;" id="type" class="neutral"> <?php $sql_gdate = "SELECT distinct pat_date from patients ORDER BY pat_date DESC"; $result_gdate = mysql_query($sql_gdate); while($row_gdate= mysql_fetch_assoc($result_gdate)){ echo '<option value="?exam_date=' . $row_gdate['pat_date'] . '"'; IF ($exam_date == $row_gdate['pat_date']) { echo 'selected="selected"'; } else { echo ''; } echo '>' . $row_gdate['pat_date'] . '</option>'; } ?> </select> </form> </div> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 25, 2013 Share Posted July 25, 2013 there's not enough information in your post. do you want the current date as the first <option> choice, even if there are dates greater then the current date, or do you want the current date to be listed in the proper date order? for both cases, you need to store the data in an array using the date as the key to the array. for the first case, if the current date isn't in the array, simply output the <option> choice. for the second case, simply insert the current date into the array if it is not already present and order the array by the dates (assuming your date format can be used to order dates, see the next paragraph.) also, the format you have shown for the dates does not permit correct sorting, so either you have left out some code that produces that format or your data doesn't sort properly when the year changes value. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 25, 2013 Share Posted July 25, 2013 Looks like you are storing the date as a string in the DB and not a date value - that's a bad idea and makes this a little more difficult. Plus, based on your specific needs this could be simpler, but this will get you started <?php //Create array with current date as initial value $dateOptions = array(date('m/d/Y')); //Add dates from DB to array while($row_gdate= mysql_fetch_assoc($result_gdate)) { $dateOptions[] = $row_gdate['pat_date']; } //Remove duplicates (current date) $dateOptions = array_unique($dateOptions); //Sort the dates sort($dateOptions); //Create the options list foreach($dateOptions as $date) { $selected = ($exam_date == $date) ? ' selected="selected"' : ''; echo "<option value='?exam_date={$date}'{$selected}>{$date}</option>\n"; } ?> 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.