mstevens Posted October 16, 2014 Share Posted October 16, 2014 (edited) Greetings, I am new to these forums, I am working on this assignment, and these are the current issues I am running into. Notice: Undefined variable: year in G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\zodiac_year_switch.php on line 77 ie. $year = validateInput($year,"Birth Year"); Notice: Undefined variable: year_count in G:\EasyPHP-5.3.2i\www\PHP_Projects\ChineseZodiacs\zodiac_year_switch.php on line 138 ie. echo "<p>You are person " . $year_count . "to enter " . $year . "</p>\n"; Honestly, I believe they are linked, because what should be happening, as the user enters the year, and hits submit, it should create a file called counts/$year.txt - $year should equal the entered data in the textbox, any help would be appreciated. Thank you for your help. <!DOCTYPE html> <head> <title>Write to and From a File</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <?php $dir = "counts"; if ( !file_exists($dir)) { mkdir ($dir, 0777); } function validateInput($year, $fieldname) { global $errorCount; if (empty($year)) { echo "\"$fieldname\" is a required field.<br />\n"; ++$errorCount; $retval = ""; } else { // if the field on the form has been filled in if(is_numeric($year)) { if($year >=1900 && $year <=2014) { $retval = $year; } else { ++$errorCount; echo "<p>You must enter a year between 1900 and 2014.</p>\n"; } } else { ++$errorCount; echo "<p>The year must be a number.</p>\n"; } } //ends the else for empty return($retval); } //ends the function function displayForm() { ?> <form action = "<?php echo $_SERVER['SCRIPT_NAME']; ?>" method = "post"> <p>Year of Birth: <input type="text" name="year" /></p> <p><input type="reset" value="Clear Form" /> <input type="submit" name="submit" value="Show Me My Sign" /></p> </form> <?php } function StatisticsForYear($year) { global $year_count; $counter_file = "counts/$year.txt"; if (file_exists($counter_file)) { $year_count = file_get_contents($counter_file); file_put_contents($counter_file, ++$year_count); } else { $year_count = 1; file_put_contents($counter_file, $year_count); } return ($year_count); }?> </head> <body> <?php $showForm = true; $errorCount = 0; //$year=$_POST['year']; $zodiac=""; $start_year =1900; if (isset($_POST['submit'])) $year = $_POST['year']; $year = validateInput($year,"Birth Year"); if ($errorCount==0) $showForm = false; else $showForm = true; if ($showForm == true) { //call the displayForm() function displayForm(); } else { //begins the else statement //determine the zodiac $zodiacArray = array("rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "goat", "monkey", "rooster", "dog", "pig"); switch (($_POST['year'] - $start_year) % 6) { case 0: $zodiac = $zodiacArray[0]; break; case 1: $zodiac = $zodiacArray[1]; break; case 2: $zodiac = $zodiacArray[2]; break; case 3: $zodiac = $zodiacArray[3]; break; case 4: $zodiac = $zodiacArray[4]; break; case 5: $zodiac = $zodiacArray[5]; break; case 6: $zodiac = $zodiacArray[6]; break; case 7: $zodiac = $zodiacArray[7]; break; case 8: $zodiac = $zodiacArray[8]; break; case 9: $zodiac = $zodiacArray[9]; break; case 10: $zodiac = $zodiacArray[10]; break; case 11: $zodiac = $zodiacArray[11]; break; default: echo "<p>The Zodiac for this year has not been determined.</p>\n"; break; } //ends the switch statement echo "<p>You were born under the sign of the " . $zodiac . ".</p>\n"; echo "<p>You are person " . $year_count . "to enter " . $year . "</p>\n"; } //ends the else statement ?> </body> </html> Edited October 16, 2014 by mstevens Quote Link to comment https://forums.phpfreaks.com/topic/291870-writing-to-and-from-a-file/ Share on other sites More sharing options...
Psycho Posted October 17, 2014 Share Posted October 17, 2014 Those are just Notices, which would usually be suppressed in a production environment. But, it's always a good idea to have them turned on when writing code. Basically, that warning means that you are trying to reference a variable which has not been defined yet. I'm not sure why you are getting that first error since you define $year directly before the line with the error: $year = $_POST['year']; $year = validateInput($year,"Birth Year"); If anything you would get an error on that first line if $_POST['year'] wasn't defined. BUt, there are problems with the logic. Never assume just because one value was POSTed (i.e. 'submit') that all the values were posted. It's always best to explicitly check. Such as $year = isset($_POST['year']) ? $_POST['year'] : false; //Then perform error logic if $year is false There are other logic problems as well. For example, the switch() statement is completely unnecessary. Just verify it is an int between 0 and 11 then use the value as the index of the array. Quote Link to comment https://forums.phpfreaks.com/topic/291870-writing-to-and-from-a-file/#findComment-1493945 Share on other sites More sharing options...
mstevens Posted October 17, 2014 Author Share Posted October 17, 2014 Those are just Notices, which would usually be suppressed in a production environment. But, it's always a good idea to have them turned on when writing code. Basically, that warning means that you are trying to reference a variable which has not been defined yet. I'm not sure why you are getting that first error since you define $year directly before the line with the error: $year = $_POST['year']; $year = validateInput($year,"Birth Year"); If anything you would get an error on that first line if $_POST['year'] wasn't defined. BUt, there are problems with the logic. Never assume just because one value was POSTed (i.e. 'submit') that all the values were posted. It's always best to explicitly check. Such as $year = isset($_POST['year']) ? $_POST['year'] : false; //Then perform error logic if $year is false There are other logic problems as well. For example, the switch() statement is completely unnecessary. Just verify it is an int between 0 and 11 then use the value as the index of the array. Those are just Notices, which would usually be suppressed in a production environment. But, it's always a good idea to have them turned on when writing code. Basically, that warning means that you are trying to reference a variable which has not been defined yet. I'm not sure why you are getting that first error since you define $year directly before the line with the error: $year = $_POST['year']; $year = validateInput($year,"Birth Year"); If anything you would get an error on that first line if $_POST['year'] wasn't defined. BUt, there are problems with the logic. Never assume just because one value was POSTed (i.e. 'submit') that all the values were posted. It's always best to explicitly check. Such as $year = isset($_POST['year']) ? $_POST['year'] : false; //Then perform error logic if $year is false There are other logic problems as well. For example, the switch() statement is completely unnecessary. Just verify it is an int between 0 and 11 then use the value as the index of the array. So, maybe I should explain, the goal is someone goes to the page, to view a zodiac, based on there DOB. The text box requests their DOB: I want the form to take the inputed data, ie. 1977 or whatever, and store it in a file, the (count/$year.txt) $year = 1977 as an example, (count/1977.txt); I then want the $year_count to go into the file, collect the total inputs, so if 3 users entered 1977; and display you are number 3 to enter 1977, for some reason I enter a year, it does not create the file as mentioned above, which then obviously does not display the total users who entered that respective year. Quote Link to comment https://forums.phpfreaks.com/topic/291870-writing-to-and-from-a-file/#findComment-1493946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.