Php problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Php problem

Post by utahfriend »

I am trying to create a drop down menu that a user can choose a month, day and year. Everything works find expect the year drop down menu does not work. I would appreciate any help in trying to find out why it doesn't work.

Here is the code

Code: Select all

<?PHP
                       define("ADAY", (60*60*24));
                        if (!checkdate($_POST['month'],$_POST['mday'],$_POST['year'])) {
	                        $nowArray = getdate();
					$mday = $nowArray['mday'];
	  				$mon = $nowArray['mon'];
					$year = $nowArray['year'];
   				} else {
					$mday = $_POST['mday'];
					$mon = $_POST['month'];
					$year = $_POST['year'];
   				}

				$start = mktime (12,0,0, $month,$day,$yr);
				$firstDayArray = getdate($start);

				echo "
				<form name=disp_events action=$_SERVER[PHP_SELF] method='post'>
     				<p align=\"center\">
			  
 			   <select name=\"day\">"; 
			 
				$days=Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
				for ($x=1; $x <= count($days); $x++) {
   					echo"<option value=\"$x\"";
  					 if ($x == $day) {
						echo " SELECTED";       
   					}
   					echo ">".$days[$x-1]."";	
				}
		       
		          echo "</select>

			    <select name=\"month\">";
			
				   $months=Array("January", "February", "March", "April", "May",
				   "June", "July", "August", "September", "October", "November", "December");
				   for ($x=1; $x <= count($months); $x++) {
   					echo"<option value=\"$x\"";
   					if ($x == $month) {
						echo " SELECTED";
   					}
   					echo ">".$months[$x-1]."";
				   }
				
				echo "</select>

				<select name=\"year\">";
				
					for ($x=2006; $x <= 2015; $x++) {
   						echo"<option";
  						if ($x == $year) {
							echo " SELECTED";
   						}
   						echo ">$x";
					}
			
				echo "</select>
				<input type=\"submit\" value=\"Go!\"></p>";
			
$month=sprintf("%02d",$month);
$day=sprintf("%02d",$day);

if ($month=="00") {
	$nowArray=getdate();
	$month=$nowArray['mon'];
	$yr=$nowArray['year'];
	$day=$nowArray['mday'];
	echo "no date<br>";
}
echo "Day: $day<br>";
echo "Month: $month<br>";
echo "Year: $year<br>";

$newdate="$year-$month-$day";
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What does this have to do with databases?


You have mday in your expected post variables, but no such field in the form. You have both mon and month in expected post variables, but only month.

Year appears to be the only one that should work.


Moved to PHP - Code.
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

Year, however is the only one that does not work. Day and month work just fine.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how does it not work? It certainly appears that it should work.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

for ($x=2006; $x <= 2015; $x++) {
                           echo"<option value=\"".$x."\"";
                          if ($x == $year) {
                            echo " SELECTED";
                           }
                           echo ">$x";
                    }
Try that.
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

That still did not fix the problem. When you select the day and month, they change to the items selected. However, the year drop down menu always reverts back to the 2006 year, no matter what year you choose! I need to find out how to make it so that the year stays to the year selected.
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

I stripped down the code to point out my problem. If you run this code, it brings down a drop down menu for month and a drop down menu for year. When you select the month and click "Go!", the correct month shows below the menu in the $month field. However, when you select a year and click "Go!", the menu goes back to the first option (2006) and 2006 shows below in the $year field (even if 2010 was chosen). I need to find out why the year drop down menu does not retain the selection made and the correct year show in the $year field. Any help is greatly appreciated.

Code: Select all

<?PHP
define("ADAY", (60*60*24));
if (!checkdate($_POST['month'],$_POST['mday'],$_POST['year'])) {
	$nowArray = getdate();
	$mon = $nowArray['mon'];
	$year = $nowArray['year'];
   	} else {
	$mon = $_post['month'];
	$year = $_POST['year'];
}


echo "
<form name=disp_events action=$_SERVER[PHP_SELF] method='post'>
<p align=\"center\">
</select>
<select name=\"month\">";

$months=Array("January", "February", "March", "April", "May",
 	"June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++) {
	echo"<option value=\"$x\"";
	if ($x == $month) {
		echo " SELECTED";
	}
	echo ">".$months[$x-1]."";
}
				
							
echo "</select>
<select name=\"year\">";
for ($x=2006; $x <= 2015; $x++) { 
	echo"<option value=\"".$x."\""; 
	if ($x == $year) { 
		echo " SELECTED"; 
	} 
	echo ">$x"; 
} 

echo "</select>
<input type=\"submit\" value=\"Go!\"></p>";
			

echo "<p align=\"center\">Month: $month</p>";
echo "<p align=\"center\">Year: $year</p>";

?>
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

What does checkdate() look like?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this and see what it does...

Code: Select all

<?php
define("ADAY", (60*60*24));
/* Changing this a little bit
if ( !checkdate($_POST['month'],$_POST['mday'],$_POST['year']) ) {
	$nowArray = getdate();
	$mday = $nowArray['mday'];
	$mon = $nowArray['mon'];
	$year = $nowArray['year'];
} else {
	$mday = $_POST['day']; // Changed mday to day to fit the form field data
	$mon = $_POST['month'];
	$year = $_POST['year'];
}
*/
$nowArray = getdate();
$mday = $nowArray['mday'];
$mon = $nowArray['mon'];
$year = $nowArray['year'];
if ( checkdate($_POST['month'], $_POST['day'], $_POST['year']) ) {
	$mday = $_POST['day'];
	$mon = $_POST['month'];
	$year = $_POST['year'];
}

$start = mktime (12,0,0, $mon,$mday,$year); // Changed $month to $mon, $day to $mday and $yr to $year
$firstDayArray = getdate($start);
?>

<form name="disp_events" action=<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p align="center">
<select name="day">
<?php
// Kill this array and just use a for loop from 1 to 31
//$days=Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
for ($x = 1; $x <= 31; $x++) {
	echo "<option value=\"$x\"";
	//if ($x == $day) {
	if ($mday == $x) {
		echo " selected";
	}
    echo ">$x</option>";
}
echo "</select>";

echo "<select name=\"month\">";
$months=Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++) {
	echo"<option value=\"$x\"";
	if ($mon == $x) {
		echo " selected";
	}
    echo ">" . $months[$x-1] . "</option>";
}
echo "</select>";

echo "<select name=\"year\">";
for ($x=2006; $x <= 2015; $x++) {
	echo"<option";
	if ($year == $x) {
		echo " selected";
	}
	echo ">$x</option>";
}
echo "</select>";
echo "<input type=\"submit\" value=\"Go!\"></p>";
            
$month=sprintf("%02d",$mon);
$day=sprintf("%02d",$mday);

/*
 * This is already done once at the top
 * Move this to top and change only if check_date returns true
if ($month=="00") {
	$nowArray=getdate();
	$month=$nowArray['mon'];
	$yr=$nowArray['year'];
	$day=$nowArray['mday'];
	echo "no date<br>";
}
*/
if ($month == '00') 
{
	echo "No date...<br />";
}
echo "Day: $day<br>";
echo "Month: $month<br>";
echo "Year: $year<br>";

$newdate="$year-$month-$day";
?>
utahfriend
Forum Commoner
Posts: 34
Joined: Thu Nov 10, 2005 12:25 pm
Location: Bountiful, Utah

Post by utahfriend »

FANTASTIC!!! It works... Thank you so much!!!!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're welcome. You might want to clean up the code I posted a little bit by removing most of the comments.
Post Reply