Jump to content

how can i keep values of this type drop down list has it has 3 selects for date


Lisa23

Recommended Posts

Hi i have this drop down list for date which contain 3 selects DAY MONTH YEAR hwo can i make so that when update form select keeps the same value has before help please

 

<?php
$months = array('','January','February','March','April','May','June','July','August','September','October','November','December');
echo '<select name="month_of_birth">';
for ($i=1;$i<13;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $months[$i] . '</option>';
}
echo '</select>';
echo '<select name="day_of_birth">';
for ($i=1;$i<32;++$i) {
   echo '<option value="' . sprintf("%02d",$i) . '">' . $i . '</option>';
}
echo '</select>';
echo '<select name="year_of_birth">';
$year = date("Y");
for ($i = $year;$i > $year-50;$i--) {
   $s = ($i == $year)?' selected':'';
   echo '<option value="' . $i . '" ' . $s . '>' . $i . '</option>';
}
echo '</select>';
?>

 

 

Link to comment
Share on other sites

  • Replies 53
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

I will want to programatically input selected for a given option.  Here's some quick example code:

 

<?
$monthArr = array(1,2,3,4,5,6,7,8,9,10,11,12);
echo '<select name="month">'
foreach($monthArr as $i){
  echo '<option value="'.$i.'" '.(($_POST['month']==$i)?'selected':'').'>'.$i.'</option>';
}
echo '</select>';
?>

 

Link to comment
Share on other sites

Sorry I forgot to ask

 

When you have submitted the form and you've processed form data, are you re-drected to the form again?

 

If so you will want to look into $_SESSION (URL: http://www.tizag.com/phpT/phpsessions.php)

 

Have a look over that site and then have a little go yourself, if you still need help post back :)

 

Regards, Paul.

Link to comment
Share on other sites

well when i submit the from it takes u to the action page which has the script to post the data and gives an echo form submited what i have is an edit form that allow admin to modify the data only problem that option changes the value evrytime to first option of the select which i want to make keep the one that comes from the database

Link to comment
Share on other sites

yes its a edit form wher the admin uses the modify the data so all the data are already on the database the admin can just edit bk in case he wants to modify i did it with the inputs but now with this options script i dnt know how?? whenevr edit it sets the date back to first option which user has to change again i dnt want that i want to bring the values already from the databse

Link to comment
Share on other sites

Ohh right I see, it's simple when explained better huh? :)

 

Try this...

  $thisMonth = 'mysql month';
  $thisDay   = 'mysql day';
  $thisYear  = 'mysql year';
  
  $months = array('','January','February','March','April','May','June','July','August','September','October','November','December');

  echo '<select name="month_of_birth">',"\n";
    for ($i=1;$i<13;++$i) { 
      if($i == $thisMonth) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$months[$i], '</option>',"\n";
    }
  echo '</select>',"\n";

  echo '<select name="day_of_birth">',"\n";
    for ($i=1;$i<32;++$i) {
      if($i == $thisDay) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n";
    }
  echo '</select>',"\n";

  echo '<select name="year_of_birth">',"\n";
    $year = date("Y");
    for ($i = $year;$i > $year-50;$i--) {
      if($i == $thisYear) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n";
    }
  echo '</select>',"\n";

 

Just change the variables at the top to the data that comes from your database and it should be good to go.

 

Tell me how it goes for you :)

 

Regards, Paul.

Link to comment
Share on other sites

The data that comes from you database E.G. month, day and year

  $thisMonth = 'mysql month';
  $thisDay   = 'mysql day';
  $thisYear  = 'mysql year';

 

Change the above values to your database values something like..

  $thisMonth = $row['month'];
  $thisDay   = $row['day'];
  $thisYear  = $row['year'];

OR whatever your mysql_query result is.

 

Post back if you still need help :)

 

Regards, Paul.

Link to comment
Share on other sites

i tried like this  because all the date is coming from the same colum name ( date_of_birth) it only retrived the year from the database the rest set bk to first option

 

$thisMonth = $row['date_of_birth'];
  $thisDay   = $row['date_of_birth'];
  $thisYear  = $row['date_of_birth'];

Link to comment
Share on other sites

its in this format 0000-00-00 

and i think i need to change somthing on the mysql update current is like this

 

$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$query = "UPDATE driversnew SET name = '$name', location = '$location', date_of_birth='$date_of_birth', car_number='$car_number', favourite_track='$favourite_track', least_favourite_track='$least_favourite_track', achievements='$achievements', sponsors='$sponsors', email='$email', display='$display'";

 

on the insert which works fine the mysl insert is like this just to show u this id fine

$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$q = "INSERT INTO driversnew (id, name, location, date_of_birth, car_number,
   favourite_track, least_favourite_track, achievements, sponsors, email, image, display)
VALUES ('$_POST[id]', '$_POST[name]', '$_POST[location]','$date_of_birth','$_POST[car_number]','$_POST[favourite_track]', '$_POST[least_favourite_track]','$_POST[achievements]', '$_POST[sponsors]','$_POST[email]', '$image_name','0')";

 

 

 

Link to comment
Share on other sites

Try the following Lisa.

 

<?php
  $getDate = $row['date_of_birth'];
  $splitDate = explode('-',$getDate);

  $thisMonth = $splitDate[1];
  $thisDay   = $splitDate[2];
  $thisYear  = $splitDate[0];
  
  $months = array('','January','February','March','April','May','June','July','August','September','October','November','December');

  echo '<select name="month_of_birth">',"\n";
    for ($i=1;$i<13;++$i) { 
      if($i == $thisMonth) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$months[$i], '</option>',"\n";
    }
  echo '</select>',"\n";

  echo '<select name="day_of_birth">',"\n";
    for ($i=1;$i<32;++$i) {
      if($i == $thisDay) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n";
    }
  echo '</select>',"\n";

  echo '<select name="year_of_birth">',"\n";
    $year = date("Y");
    for ($i = $year;$i > $year-50;$i--) {
      if($i == $thisYear) { 
        $s = ' selected'; 
      } else { 
        $s='';
      }
      echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n";
    }
  echo '</select>',"\n";
?>

 

Regards, Paul.

Link to comment
Share on other sites

Paul your a genious its working one last thing how do i do the same thing on this one i tried like like belllow bt no sucess once again i want the data to cum from the datbase which is value 1 or 2?? sorry for the ben a bug

 

<select name="display">
   <option selected="selected" value ="1">1</option>
   <option selected="selected" value ="0">1</option>
   </select>

Link to comment
Share on other sites

What is the fieldname called that holds this data in the database?

 

++You want the option selected that the variable equals?

For example $row['display'] will display 1 or 2 you want the correct one selected...

 

Try something like...

<select name="display">
  <option <?php if($row['display'] == '1') { echo 'selected'; } ?> value="1">1</option>
  <option <?php if($row['display'] == '2') { echo 'selected'; } ?> value="2">2</option>
</select>

 

Regards, Paul.

Link to comment
Share on other sites

here it is is the same as the u fixed earlier on

 

    
$date_of_birth = $_POST['year_of_birth'] . '-' . $_POST['month_of_birth'] . '-' . $_POST['day_of_birth'];
$query = "UPDATE driversnew SET name = '$name', location = '$location', date_of_birth='$date_of_birth', car_number='$car_number', favourite_track='$favourite_track', least_favourite_track='$least_favourite_track', achievements='$achievements', sponsors='$sponsors', email='$email', display='$display'";

Link to comment
Share on other sites

So $row['display'] returns either 1 or 2...

 

Using the 1 or 2 you want to select the correct option...

which one does 1 select and which one does 2 select?

 

For example if $row['display'] equals 1 you want

<select name="display">
  <optionvalue ="1" selected>1</option>
  <option value ="0">0</option>
</select>

 

or if $row['display'] equals 2 you want

<select name="display">
  <optionvalue ="1">1</option>
  <option value ="0" selected>0</option>
</select>

 

Regards, a slightly confused Paul.

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.