imperialized Posted October 21, 2014 Share Posted October 21, 2014 Hey everyone!!!It has been years since I have had to opportunity to post on here. I am, however, getting back into development. I am running into an issue and I was looking for some help. Here is the code: $years = array(); //Build the years array, we can only go forward 3 years, backwards 1 year. $sYear = date(Y, strtotime('-1 years')); //Start Year $eYear = date(Y, strtotime('+3 years')); //End Year while($sYear < $eYear){ array_push($years, $sYear); //Add each year that fits to the array $sYear++; } //In order for us to make sure we have clean parameters for our jQuery calls later //We have to make sure that our year variable usage is consistant throughout. //If our case, we are passing the full 4 digit year as am INT = $curYear $curYear = clean($_GET['curYear']); print "Before anything: curYear = $curYear <br><br><br>"; if($curYear == "" || $curYear == null){ //No Year set, use current print "empty or null<br>"; exit; $curYear = date(Y); }elseif(!is_numeric($curYear)){ //Not even a number lol print "not number<br>"; $curYear = date(Y); }elseif($aSearch = in_array($curYear, $years, true)){ print "valid year<br>" . $years[$aSearch]; //It isn't in our array of valid years //$curYear = date(Y); }else{ //all else failed $curYear = date(Y); print "Failed all tests.<br>"; } //Should be a good year for our calendar by this point. exit; I am passing the variable curYear in the url: index.php?curMonth=July&curYear=2013For some reason, it outputs: Before anything: curYear = 2013 Failed all tests. printing the $years array gets: Array ( [0] => 2013 [1] => 2014 [2] => 2015 [3] => 2016 ) I think you get the just of what I am looking for here. Thanks for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/ Share on other sites More sharing options...
davidannis Posted October 21, 2014 Share Posted October 21, 2014 (edited) $aSearch = in_array($curYear, $years, true) did you mean to assign the value here? Try using in_array without the true to see if it is the type that is causing it to fail. Edited October 21, 2014 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494323 Share on other sites More sharing options...
Psycho Posted October 21, 2014 Share Posted October 21, 2014 The logic is kind of "wonky". You have the valid test as part of one of the conditions. I would suggest having all the tests be for invalid conditions, then if all of those come up false the result should be it is a valid year. But, the problem is the last condition check elseif($aSearch = in_array($curYear, $years, true)){ print "valid year<br>" . $years[$aSearch]; //It isn't in our array of valid years //$curYear = date(Y); } Why did you use true for the last condition? Do you realize what it does? It is for performing a strict comparison. I.e. The string "2013" will not match the number 2013 using a strict comparison. You don't show what the function clean() does, but the value you are passing it (from the $_GET array) is a string. I suspect you are not converting the value to a number. But, the values you added to the array $years are all numeric values. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494326 Share on other sites More sharing options...
davidannis Posted October 21, 2014 Share Posted October 21, 2014 Also, $years[$aSearch] won't work as you expect since in_array just returns true or false, not the position in the array. http://php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494327 Share on other sites More sharing options...
Psycho Posted October 21, 2014 Share Posted October 21, 2014 (edited) OK, after reviewing all the code, I've found some other opportunities for improvement. The code to create the array for years is overly complicated. Instead of using date() and strtotime(), just use date() to get the current year and subtract one. strtotime() is inefficient. Plus, you don't need a loop, just use range() //Build the years array using current year -1 and 4 yerars forward $startYear = date(Y) - 1; //Start Year $years = range($startYear, $startYear+4); I don't know what clean() is doing, but all it really needs to do is trim() the value since you are doing all the validations inline. empty() can replace the empty string and null check. But, you don't need it anyway since the is_numeric() check would return false anyway. And, you don't need the is_numeric() since you are testing against the valid years. Just a lot of unnecessary code. Also, even if you fix the array_search() described above, the condition would still return true because you are assigning the result of in_array() to the variable $aSearch. So, you are really testing if you can assign a value to $aSearch - which will always return true. I would also change the variable $curYear to $selectedYear since it would not necessarily be the current year. This should be all that you need //Build a valid years array using current year -1 and 4 yerars forward from that $currentYear = date(Y); //Start Year $validYears = range($startYear-1, $startYear+4); //In order for us to make sure we have clean parameters for our jQuery calls later //We have to make sure that our year variable usage is consistant throughout. //If our case, we are passing the full 4 digit year as am INT = $curYear $selectedYear = clean($_GET['curYear']); print "Before anything: curYear = $curYear <br><br><br>"; //Check if the selected value is a valid year, if not set to current year if(!in_array($selectedYear, $validYears)) { $selectedYear = $currentYear; } //Should be a good year for our calendar by this point. print "valid year<br>" . $years[$aSearch]; exit; Edited October 21, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494329 Share on other sites More sharing options...
imperialized Posted October 21, 2014 Author Share Posted October 21, 2014 The logic is kind of "wonky". You have the valid test as part of one of the conditions. I would suggest having all the tests be for invalid conditions, then if all of those come up false the result should be it is a valid year. But, the problem is the last condition check elseif($aSearch = in_array($curYear, $years, true)){ print "valid year<br>" . $years[$aSearch]; //It isn't in our array of valid years //$curYear = date(Y); } Why did you use true for the last condition? Do you realize what it does? It is for performing a strict comparison. I.e. The string "2013" will not match the number 2013 using a strict comparison. You don't show what the function clean() does, but the value you are passing it (from the $_GET array) is a string. I suspect you are not converting the value to a number. But, the values you added to the array $years are all numeric values. The reason I used true for that condition is because if that is true, it changes it to that value. all other conditions, false, set to the current dates value. I believe you are are correct about not converting it to a number and I am trying to match a string. I will test when I get home. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494332 Share on other sites More sharing options...
davidannis Posted October 21, 2014 Share Posted October 21, 2014 (edited) The reason I used true for that condition is because if that is true, it changes it to that value. all other conditions, false, set to the current dates value. I believe you are are correct about not converting it to a number and I am trying to match a string. I will test when I get home. The true checks not just that it is in the array but also that it is the same type (string, numeric, boolean) which is what I meant by "Try using in_array without the true to see if it is the type that is causing it to fail." The default is to not require that type match. No matter what you set that third parameter to the return value of the in_array() will be a boolean that is true if the value is in the array (and type is the same if you put ", true" at the end) or false if the value is not in the array. Edited October 21, 2014 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494335 Share on other sites More sharing options...
Psycho Posted October 21, 2014 Share Posted October 21, 2014 The reason I used true for that condition is because if that is true, it changes it to that value. Huh? The "true" I was referring to was the one used as the third parameter in the in_array() function. That is specifically used to make the in_array() comparisons strict. If you use it then "2013" will not match 2013. But, if you leave it out (or make it false) then the string "2013" will match the number 2013. But, what are you wanting $aSearch to be the index of the $years array when you already have the actual year value? Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494339 Share on other sites More sharing options...
ginerjm Posted October 21, 2014 Share Posted October 21, 2014 Ok - I took your code verbatim and cleaned it up to read it. Basically it's the same - BUT with error checking on. <?php session_start(); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); // $years = array(); //Build the years array, we can only go forward 3 years, backwards 1 year. $sYear = date(Y, strtotime('-1 years')); //Start Year LINE 7 $eYear = date(Y, strtotime('+3 years')); //End Year LINE 8 while($sYear < $eYear) { array_push($years, $sYear); //Add each year that fits to the array $sYear++; } echo "<pre>",print_r($years,true),"</pre>"; //In order for us to make sure we have clean parameters for our jQuery calls later //We have to make sure that our year variable usage is consistant throughout. //If our case, we are passing the full 4 digit year as am INT = $curYear //$curYear = clean($_GET['curYear']); $curYear = $_GET['curYear']; print "Before anything: curYear = $curYear <br><br><br>"; if($curYear == "" || $curYear == null) { //No Year set, use current print "empty or null<br>"; exit; //THIS ENDS THE SCRIPT RIGHT HERE!! $curYear = date(Y); } elseif(!is_numeric($curYear)) { //Not even a number lol print "not number<br>"; $curYear = date(Y); } elseif($aSearch = in_array($curYear, $years, true)) { print "valid year<br>" . $years[$aSearch]; } else { //It isn't in our array of valid years //$curYear = date(Y); //all else failed $curYear = date(Y); print "Failed all tests.<br>"; } //Should be a good year for our calendar by this point. exit; The errors I get are: Notice: Use of undefined constant Y - assumed 'Y' in /home/albany/public_html/homejg/test.php on line 7Notice: Use of undefined constant Y - assumed 'Y' in /home/albany/public_html/homejg/test.php on line 8 I have marked the lines above. You really need to remember to quote your strings. Other than that though I do get a valid answer from the script during testing. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494340 Share on other sites More sharing options...
imperialized Posted October 21, 2014 Author Share Posted October 21, 2014 (edited) Ok, I have read the responses above. Thank you all again for replying.The code has been modified as such: //Build the years array, we can only go forward 3 years, backwards 1 year. $startYear = date(Y) - 1; //Start Year $years = range($startYear, $startYear+4); $curYear = trim($_GET['curYear']); print "Before anything: curYear = $curYear <br><br><br>"; //Check if the selected value is a valid year, if not set to current year if(!in_array($curYear, $validYears)){ $curYear = date(Y); } //Should be a good year for our calendar by this point. print "curYear = $curYear"; exit; This, however, still does not work. The $years array contains all of the right numbers (Thanks for that, much more efficient and clean)When the if statement runs: if(!in_array($curYear, $validYears)){ $curYear = date(Y); } using this URL: /index.php?curYear=2013It says that 2013 does not exist in the array. And set it to 2014. I would also change the variable $curYear to $selectedYear since it would not necessarily be the current year. The reason I chose to use $curYear is because that is the current year I am working with, and noot because it is necessarily the actual year. Thanks for your opinion, however. Edited October 21, 2014 by imperialized Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494350 Share on other sites More sharing options...
Barand Posted October 22, 2014 Share Posted October 22, 2014 $years = range($startYear, $startYear+4); if(!in_array($curYear, $validYears)){ Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494358 Share on other sites More sharing options...
davidannis Posted October 22, 2014 Share Posted October 22, 2014 $curYear = date(Y); as pointed out previously by ginerjm you should put the Y in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494409 Share on other sites More sharing options...
Solution imperialized Posted October 27, 2014 Author Solution Share Posted October 27, 2014 Thanks to all of you guys. I must have been exhausted when I changed that code and missed the variable swap.Anyhow, the final working code is as follows: //Build the years array, we can only go forward 3 years, backwards 1 year. $startYear = date(Y) - 1; //Start Year $validYears = range($startYear, $startYear+4); //In order for us to make sure we have clean parameters for our jQuery calls later //We have to make sure that our year variable usage is consistant throughout. //If our case, we are passing the full 4 digit year as am INT = $curYear $curYear = trim($_GET['curYear']); print "Before anything: curYear = $curYear <br><br><br>"; //Check if the selected value is a valid year, if not set to current year if(!in_array($curYear, $validYears)){ $curYear = date("Y"); } //Should be a good year for our calendar by this point. print "curYear = $curYear"; exit; As far as the date() function goes, what does adding the quotes do? Specifically, if it is numeric data being passed. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494884 Share on other sites More sharing options...
mac_gyver Posted October 27, 2014 Share Posted October 27, 2014 the first parameter of the date() function is a string. when you don't quote it, it is first tested as a defined constant, which throws a php error because it doesn't exist, then php assumes you meant a string and tries the value a second time as a string. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1494915 Share on other sites More sharing options...
imperialized Posted October 28, 2014 Author Share Posted October 28, 2014 the first parameter of the date() function is a string. when you don't quote it, it is first tested as a defined constant, which throws a php error because it doesn't exist, then php assumes you meant a string and tries the value a second time as a string. Thank you for explaining that to me. Appreciate it. I guess if I paid a little close attention to the php date() page I probably would have noticed. Not only that, but all of their examples are quoted. Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1495019 Share on other sites More sharing options...
imperialized Posted October 28, 2014 Author Share Posted October 28, 2014 (edited) Thanks again to all of you who helped. I marked the completed working code as the answer. You all are the best! Edited October 28, 2014 by imperialized Quote Link to comment https://forums.phpfreaks.com/topic/291964-simple-year-checking-with-arrray/#findComment-1495021 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.