Jump to content

[SOLVED] Help auto-selecting field


msaz87

Recommended Posts

Hey all,

 

I'm having difficulties getting a drop down list to auto select the current date from a MySQL DB. The code below shows the the date getting pulled then formatted and my attempt at getting it to select.

 

						while($row = mysql_fetch_array($tournament_details_results)){ 

						$start	= $row['date'];
						$days	= $row['length'];

					}

					$fix_date	= strtotime($start);

					$date		= date('M j, Y',$fix_date);

					?>

					Date: <br/>
					<select name="date">
					<?php for ($e = 0; $e < $days; $e++, $t++) { ?>

							<?php 		

								$s	= date('M j, Y', strtotime($date)+(86400*$t));
								$sv	= date('Y-m-d', strtotime($s));

							?>							

						<option <? if ($date_selection_mod == ($sv)); echo "selected"; ?> value="<?php echo($sv); ?>">

							<?php echo($s); ?>

						</option>

					<?php } ?>
					</select>

 

When setting up the DB, the user inputs how long the event is, in terms of days, then gives a start date and so the $e portion deals with making all the dates for it automatically, though it only stores the start one.

 

Any ideas? Thanks very much!

Link to comment
https://forums.phpfreaks.com/topic/181968-solved-help-auto-selecting-field/
Share on other sites

well I see a bunch of problems with your code. First of all you are looping through all of the results and replacing the old value with the new one. So $start & $days will only have one value. Second of all you could simply loop through the results and display it all at one time. Try this:

 

Date: <br/>
<select name="date">
  <?php while($row = mysql_fetch_array($tournament_details_results)){                    
  	$start   = $row['date'];
    $days   = $row['length'];
    $fix_date   = strtotime($start);
    $date      = date('M j, Y',$fix_date);
    $s   = date('M j, Y', strtotime($date)+(86400*$t));
    sv   = date('Y-m-d', strtotime($s));
  ?>
  <option <?php if ($date_selection_mod == ($sv)); echo "selected"; ?> value="<?php echo($sv); ?>"> <?php echo($s); ?> </option>
  <?php } ?>
</select>

well I see a bunch of problems with your code. First of all you are looping through all of the results and replacing the old value with the new one. So $start & $days will only have one value. Second of all you could simply loop through the results and display it all at one time. Try this:

 

Date: <br/>
<select name="date">
  <?php while($row = mysql_fetch_array($tournament_details_results)){                    
  	$start   = $row['date'];
    $days   = $row['length'];
    $fix_date   = strtotime($start);
    $date      = date('M j, Y',$fix_date);
    $s   = date('M j, Y', strtotime($date)+(86400*$t));
    sv   = date('Y-m-d', strtotime($s));
  ?>
  <option <?php if ($date_selection_mod == ($sv)); echo "selected"; ?> value="<?php echo($sv); ?>"> <?php echo($s); ?> </option>
  <?php } ?>
</select>

 

What happens with the code you suggested is it only displays one date (and it's actually a date earlier than the one retrieved from the DB).

 

Let me try to explain it in context a bit more...

 

This page is used to edit a schedule of an event. The event is set up in a previous page where they enter a start date, say 2009-11-20 and then say how many days it lasts. So lets say it lasts 4 days, the only two things entered into the DB are the 2009-11-20 and the length...

 

On that <select> field, the original code pulled the start date, then used the loop to generate the next three days as options.. IE: so you can set up a schedule for each day of the tournament.

 

Since this page is used to edit these, I'm looking to make it auto-select the date entered into the scheduling DB (so if when setting it up, they picked 2009-11-22, it would default to that).

 

Not sure if that helps clear anything up... if I'm not giving enough code to make it clear, please let me know.

 

Thanks for the help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.