Page 1 of 1

Drop down menus and mysql insertion

Posted: Thu Mar 01, 2007 2:47 pm
by aspekt9
I'm looking to create drop down boxes for a user to select the date and then have it insert what they select for month, day, year, hour, minutes and insert it into mysql. It needs to be organized in a timestamp like so: 2007-02-28 21:06:40

I've created the drop down boxes and it works pretty well:

Code: Select all

function selectCurrentDate($currentMonth, $currentDay, $currentYear, $currentHour, $currentMins, $currentSecs) 
{
	$display = '';
    // Month Array 
    $monthNames = array('January', 'Feburary', 'March', 'April', 'May', 'June', 
              'July', 'August', 'September', 'October', 'November', 'December'); 
     
    // MaxNumber of days in a month (will need to write more robust code) 
    $maxDays = 31; 
	// Start year
	$startYear = 2000;
	// Display years up to max years
    $maxYears = 2050;
	// Max hours to display
	$maxHour = 12;
	// Max minutes to display
	$maxMinutes = 59;
	// Max seconds to display
	$maxSeconds = 59;
    // Begin Month select menu 
    $display .= 'Select event date: <select name="theMonth">'; 

    // Loop through each month 
    foreach ($monthNames as $myMonth) 
    { 
        // If month is equal to $currentMonth passed in, select 
        if($currentMonth == $myMonth) 
            $display .= '<option value="'. $myMonth .'" selected>' . $myMonth . '</option>'; 
        else 
            $display .= '<option value="'. $myMonth .'">' . $myMonth . '</option>'; 
    } 

    // end select     
    $display .= '</select> '; 
     
    // Begin Day select Menu 
    $display .= ' <select name="theDay">'; 

    // Loop through the max number of days 
    for($i = 1; $i < $maxDays; $i++) 
    { 
        if($i == $currentDay) 
            $display .= '<option value="'. $i .'" selected>'. $i.'</option>'; 
        else 
            $display .= '<option value="'. $i .'">'. $i.'</option>'; 
    } 
     // end day select
    $display .= '</select>';
	
    // Display year select
	$display .= ' <select name="theYear">';
	
	// Loop through max number of years
	for($i = $startYear; $i <= $maxYears; $i++) 
    { 
        if($i == $currentYear) 
            $display .= '<option value="'. $i .'" selected>'. $i.'</option>'; 
        else 
            $display .= '<option value="'. $i .'">'. $i.'</option>'; 
    }
	$display .= '</select>';
	
	// Begin hour select
	$display .= ' <select name="theHour">';
	
	for($h = 1; $h <= $maxHour; $h++)
	{
		if($h == $currentHour)
			$display .= '<option value="'. $h .'" selected>'. $h.'</option>';
		else
			$display .= '<option value="'. $h .'">'. $h.'</option>';
	}
	$display .= '</select>';
			
	// Begin minute display
	$display .= '<select name="theMinutes">';
	
	for($m = 1; $m <= $maxMinutes; $m++)
	{
		if($m == $currentMins)
			$display .= '<option value="'. $m .'" selected>'. $m.'</option>';
		else
			$display .= '<option value="'. $m .'">'. $m.'</option>';
	}
	$display .= '</select>';

	// Begin second display
	$display .= '<select name="theSeconds">';
	
	for($s = 1; $s <= $maxSeconds; $s++)
	{
		if($s == $currentSecs)
			$display .= '<option value="'. $s .'" selected>'. $s.'</option>';
		else
			$display .= '<option value="'. $s .'">'. $s.'</option>';
	}
	$display .= '</select>';	
	return $display;
} 
$curmonth = date('F');
$curday = date('j');
$curyear = date('Y');
$curhour = date('g');
$curmin = date('i');
$cursecs = date('s');

echo selectCurrentDate($curmonth,$curday,$curyear,$curhour,$curmin,$cursecs);
Now how would I organize all the data into a timestamp. Once it's in the form of the timestamp it'll be easy to insert it into mysql. Could I do something like:

Code: Select all

$events = mysql_query("INSERT INTO timestamp, DATE_FORMAT(date, '%M %e, %Y %h:%m:%s') as something?
            date,  FROM calevents") or die(mysql_error());
I don't really get how the options work
Thanks for the suggestions

Posted: Thu Mar 01, 2007 3:45 pm
by RobertGonzalez
Why not try it on your local development server and see if it works? Or as another option, run a test of it in phpMyAdmin.