Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selection Box (date) 1

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Another selection box question if someone could help!

I have the following select boxes to pass the date into a string and then into a DATE field of my database.

Code:
<select name="day"> 
<?php 
for ($d = 1; $d <= 31; $d++) 
echo "<option value=\"" . $d . "\">" . $d . "</option>"; 
?> 
</select> 
<select name="month"> 
<?php 
for ($m = 1; $m <= 12; $m++) 
echo "<option value=\"" . $m . "\">" . $m . "</option>"; 
?> 
</select> 
<select name="year"> 
<?php 
$max_year = 2005;
for ($y = 2004; $y <= $max_year; $y++) 
echo "<option value=\"" . $y . "\">" . $y . "</option>"; 
?> 
</select>

My question is - how would I retrieve the date and set the fields above to correspond with the date?



 
Insufficient data for a meaningful answer.

You would access the submitted fields' value through one or the other of the superglobal arrays $_POST or $_GET, depending on which method of submission you are using in the form which holds these fields.

You could put together a date string by using PHP's "." string concatenation operator. But keep in mind that you'll need to add logic to your PHP code for badly-formed dates. For example, your user could input a date of the 31st of June or the 30th of February.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
On my update page I do the following:

Code:
$acc_order_date = stripslashes(strip_tags(trim("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}")));

and then INSERT the date into my date field in the database.

What I need to do is to take that DATE out of the database and populate the selection boxes - but how would I do that and split up the DATE into the Day, Month, Year for the selections

 
Insufficient data for a meaningful answer.

PHP can interact with numerous database servers and engines. You haven't specified which. There may be database-side solutions which could perform the split, or you may have to do it PHP-side. If you're splitting the date field apart on the PHP side, your database server may or may not have the ability to format the date conveniently, which can make a difference in the solution.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Assuming you've already done a retrieval from your database into an associative array in the variable $row:
Code:
$tmp = explode('-',$row['acc_order_date']);
$db_day = $tmp[2];
$db_month = $tmp[1];
$db_year = $tmp[0];
Then in your loops where you create the option lists (for example months), add the word "selected" if your loop variable equals the database value:
Code:
<select name="month"> 
<?php 
for ($m = 1; $m <= 12; $m++) {
echo '<option value="' . $m . '";
if ($i == $db_month) echo " selected";
echo '>' . $m . "</option>\n"; } 
?>

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top