Jump to content

Recommended Posts

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:

K66gtHS.png

 

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>
Link to comment
https://forums.phpfreaks.com/topic/280500-add-current-date-to-dropdown/
Share on other sites

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.

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";
}

?>
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.