Jump to content

Change option value php


jpopuk

Recommended Posts

Hi, I am creating a php script with date of birth fields. I have it so when I add a DOB it does it like this:

$dob_day	= isset($_POST['dob_day']) && is_numeric($_POST['dob_day']) && $_POST['dob_day'] > 0 && $_POST['dob_day'] <= 31 ? $_POST['dob_day'] : '';
$dob_month	= isset($_POST['dob_month']) ? check_input($_POST['dob_month']) : '';
$dob_year	= isset($_POST['dob_year']) && is_numeric($_POST['dob_year']) ? $_POST['dob_year'] : '';
$dob		= $dob_day . '' . $dob_month . '' . $dob_year;

This works absolutely fine.

 

The issue I am having is when I go to edit the DOB in the edit page. It never seems to display all the content correctly. This is how the fields are setup in the edit template:

<select name="dob_day">
	<option value="">Day</option>
	<?php for ($i = 1; $i <= 31; $i++) { ?>
	<option value="<?php echo $i; ?>"<?php echo $dob[0] == $i ? ' selected="selected"' : ''; ?>><?php echo $i; ?></option>
	<?php } ?>
</select>
<select name="dob_month">
	<option value="">Month</option>
	<option value=" January "<?php echo $dob[1] == 1 ? ' selected="selected"' : ''; ?>>January</option>
	<option value=" February "<?php echo $dob[1] == 2 ? ' selected="selected"' : ''; ?>>February</option>
	<option value=" March "<?php echo $dob[1] == 3 ? ' selected="selected"' : ''; ?>>March</option>
	<option value=" April "<?php echo $dob[1] == 4 ? ' selected="selected"' : ''; ?>>April</option>
	<option value=" May "<?php echo $dob[1] == 5 ? ' selected="selected"' : ''; ?>>May</option>
	<option value=" June "<?php echo $dob[1] == 6 ? ' selected="selected"' : ''; ?>>June</option>
	<option value=" July "<?php echo $dob[1] == 7 ? ' selected="selected"' : ''; ?>>July</option>
	<option value=" August "<?php echo $dob[1] == 8 ? ' selected="selected"' : ''; ?>>August</option>
	<option value=" Septembe r"<?php echo $dob[1] == 9 ? ' selected="selected"' : ''; ?>>September</option>
	<option value=" October "<?php echo $dob[1] == 10 ? ' selected="selected"' : ''; ?>>October</option>
	<option value=" November "<?php echo $dob[1] == 11 ? ' selected="selected"' : ''; ?>>November</option>
	<option value=" December "<?php echo $dob[1] == 12 ? ' selected="selected"' : ''; ?>>December</option>
</select>
<select name="dob_year">
	<option value="">Year</option>
	<?php for ($i = gmdate('Y', time() + ($user_data['time_offset'] * 3600) + ($config['add_time'] * 3600)); $i >= gmdate('Y', time() + ($user_data['time_offset'] * 3600) + ($config['add_time'] * 3600)) - 25; $i--) { ?>
	<option value="<?php echo $i; ?>"<?php echo $dob[2] == $i ? ' selected="selected"' : ''; ?>><?php echo $i; ?></option>
	<?php } ?>
</select>

This only allows the Day to show fine. - Month and Year just show as Month and Year in their respective dropdowns rather than the actual Month or Year.

 

I know in Month I need to change the numerical 1 through to 12 to the name of the months, but I am unsure on how to do this without messing up the code.

 

As for the year I have no clue to that what so ever.

 

If anyone has any advice or help that would be absolutely brilliant.

 

Thanks in advance,

 

Paul

Link to comment
Share on other sites

This is from a register form I created a long time ago I hope it will help you 

<?php // BIRTHDAY

$ms = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$d = 1; ?>

<select name="b_day">
  <option value="Day">Day</option>
    <?php while ( $d <= 31 ) {
    echo '<option value="'.$d.'"'.($_POST['b_day'] == $d ? ' selected="selected"':'').'>'.$d.'</option>';
    $d++;
    } ?>
</select>

<select name="b_month">
  <?php
  foreach($ms as $m){
    echo '<option value="'.$m.'"'.($_POST['b_month'] == $m ? ' selected="selected"':'').'>'.$m.'</option>';
    $m++;
   } ?>
</select>

<select name="b_year">
  <?php
  $end = date('Y'); // Current Year
  $start = ($end - 100); // Current Year - 100
  $y = $start; ?>
  <option value="Year">Year</option>
  <? while( $y <= $end){
    echo '<option value="'.$y.'"'.($_POST['b_year'] == $y ? ' selected="selected"':'').'>'.$y.'</option>';
    $y++;
  }?>
</select>

Link to comment
Share on other sites

Thanks for the reply. - I have had a look at this and unfortunately this will not solve my solution.

 

The data is coming from one from database entry (dob).

 

So essentially the database cell  dob will say: 1 January 2014 - rather than 3 individual cells (dob_day, dob_month, dob_year).

Edited by jpopuk
Link to comment
Share on other sites

But this is a string when pulled from a database.

$dob		= $dob_day . '' . $dob_month . '' . $dob_year;

you would need to break it down, before showing it

 

literally your going to see for example: 2 MAY 1986

 

you would need to explode this

$section = explode(" ", $dob);
$day = $section[0]; // 2
$month = $section[1]; // May
$year = $section[2]; // 1986

example of this in your script would be

<option value=" January "<?php echo $section[1]; == Janurary ? ' selected="selected"' : ''; ?>>January</option> // This would be false
<option value=" May "<?php echo $section[1]; == May ? ' selected="selected"' : ''; ?>>May</option> // This would be True as the month was may

That being said I would still use my way

$ms = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
<select name="b_month">
  <?php
  foreach($ms as $m){
    echo '<option value="'.$m.'"'.($section[1] == $m ? ' selected="selected"':'').'>'.$m.'</option>';
   } ?>
</select>
Edited by xXREDXIIIXx
Link to comment
Share on other sites

Yes that is correct. - When I go to edit the post though which is the form in first post, I am not sure why it is not showing correct content.

 

The dob_day field shows the day, but the month and year is not showing correct content even though it has been posted to the database.

Link to comment
Share on other sites

So you think it would be better to break it up to dob_day, dob_month, dob_year rather than having have it being pulled from one database entry (dob)?

 

That's really down to you. you can do that or the way I said before. I personally split it up in the database. 

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.