S.I.
S.I.

Reputation: 3375

Populating user birthday in dropdowns from database with php

I have user system on the site. When user is registering he select his date of birth also. I'm storing this date in table in this format 1900-01-01.

Now I want to make page from where users can edit their account and also to change date of birth..

Problem: How to populate 3 dropdowns Year, Month, Day when in database table is stored as one?

So this is the query when user open edit page:

SELECT * FROM users WHERE id = ? LIMIT 1

and the dropdowns..

<select name="year" id="year" class="form-control">
    <option value="--">Year</option>
        <?php for($i=date('Y'); $i>1899; $i--) {
            $birthdayYear = '';
            $selected = '';
            if ($birthdayYear == $i) $selected = ' selected="selected"';
            print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
        } ?>
</select>
<select name="month" id="month" onchange="" class="form-control" size="1">
    <option value="--">Month</option>
    <option value="01">January</option>
    <option value="12">...</option>                     
    <option value="12">December</option>
</select>   
<select name="day" id="day" onchange="" class="form-control" size="1">
    <option value="--">Day</option>
    <option value="01">01</option>
    <option value="...">...</option>
    <option value="31">31</option>
</select>

Is it possible this or I must store each value in different column in database?

Update:

result = $pdo->prepare("SELECT * FROM users WHERE id = ? LIMIT 1");
$result -> bindParam(1, $id, PDO::PARAM_INT);
$result -> execute();
foreach ( $result as $row )             
{
      $birthDay = $row['user_birthday'];                 
      $explodeBirthDay = explode('-',$birthDay);
      $birthdayYear = $explodeBirthDay['0'];
      $birthdayMonth = $explodeBirthDay['1'];
      $birthdayDay = $explodeBirthDay['2'];             
      echo '    
          <div class="form-group">                     
             <label class="col-md-4 control-label" for="birthday"></label>
                <div class="col-md-2">
                  <div class="form-group">
                    <select name="year" id="year" class="form-control">
                        <option value="--">Year</option>';                                                      
                        for($i=date('Y'); $i>1899; $i--) {
                            $selected = '';
                            if ($birthdayYear == $i) $selected = ' selected="selected"';
                            print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
                        }                           
                    echo'   
                        </select>       
                    </div>
                 </div>     
                 <div class="col-md-2">
                   <div class="form-group">
                     <select name="month" id="month" onchange="" class="form-control" size="1">
                       <option value="--">Month</option>
    <option  if($birthdayMonth == "01") { ?> selected="selected" <?php } ?> value="01">January</option>

    <option  if($birthdayMonth == "12") { ?> selected="selected" <?php } ?>value="12">December</option>
                         </select>            
                        </div>     
                      </div>
                 <div class="col-md-2">         
                    <div class="form-group">
                        <select name="day" id="day" onchange="" class="form-control" size="1">
                        <option value="--">Day</option>
    <option <?php if($birthdayDay == "01") { ?> selected="selected" <?php } ?> value="01">01</option>
    <!-- remaining days -->
      <option <?php if($birthdayDay == "31") { ?>selected="selected" <?php } ?> value="31">31</option>
                        </select>
                    </div>                      
                </div>                      
        </div>                                      

Upvotes: 0

Views: 580

Answers (2)

Vallabh Bothre
Vallabh Bothre

Reputation: 661

Use this code,

$result = $pdo->prepare("SELECT * FROM users WHERE id = ? LIMIT 1");
$result -> bindParam(1, $id, PDO::PARAM_INT);
$result -> execute();    

$fff = '1';    
foreach ( $result as $row )             
{

      $birthDay = $row['user_birthday'];
      $explodeBirthDay = explode('-',$birthDay);

      $birthdayYear = $explodeBirthDay['0'];
      $birthdayMonth = $explodeBirthDay['1'];
      $birthdayDay = $explodeBirthDay['2'];
      ?>    
          <div class="form-group">                     
             <label class="col-md-4 control-label" for="birthday"></label>
                <div class="col-md-2">
                  <div class="form-group">
                    <select name="year<?php echo $fff; ?>" id="year<?php echo $fff; ?>" class="form-control">
                        <option value="--">Year</option>
                        <?php
                        for($i=date('Y'); $i>1899; $i--) {
                            $selected = '';
                            if ($birthdayYear == $i) $selected = ' selected="selected"';
                            print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
                        }
                        ?>                           

                        </select>       
                    </div>
                 </div>     
                 <div class="col-md-2">
                   <div class="form-group">
                     <select name="month<?php echo $fff; ?>" id="month<?php echo $fff; ?>" onchange="" class="form-control" size="1">
                       <option value="--">Month</option>
    <option <?php if($birthdayMonth == "01") { ?> selected="selected" <?php } ?> value="01">January</option>

    <option <?php if($birthdayMonth == "12") { ?> selected="selected" <?php } ?>value="12">December</option>
                         </select>            
                        </div>     
                      </div>
                 <div class="col-md-2">         
                    <div class="form-group">
                        <select name="day<?php echo $fff; ?>" id="day<?php echo $fff; ?>" onchange="" class="form-control" size="1">
                        <option value="--">Day</option>
    <option <?php if($birthdayDay == "01") { ?> selected="selected" <?php } ?> value="01">01</option>
    <!-- remaining days -->
      <option <?php if($birthdayDay == "31") { ?>selected="selected" <?php } ?> value="31">31</option>
                        </select>
                    </div>                      
                </div>                      
        </div>  
<?php 
     $fff++;
     } 

?>

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

Try changing this,(Considering $date has value from database.)

if (date("Y",strtotime($date)) == $i) $selected = ' selected="selected"';
        print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");

Upvotes: 1

Related Questions