nvee Posted November 8, 2009 Share Posted November 8, 2009 Well, I will be dead honest with you, this is the first time I have ever used an array (accept for mysql_fetch_array) so I was quite chuffed with my idea to make my "empty validation" work ... and the code appears fine but when I do a test PHP fails to respond and gives me a time-out error. The code: $errors = array($tradename,$regname); echo "<ul>"; while($value = $errors) { if(empty($value)) { echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>"; } } echo "</ul>"; } I think its simple enough, the variables in the array is variables posted from the form which is declared earlier in the script. I however use a function to declare the variables (yes I know, why right a function to purely do a $_POST["value"];, but I am trying to be as professional with my code now as I can be Any ideas why PHP would crash on my code? Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/ Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 Is your function coded to use passing by value, reference or used global? And does it return the values. I think the problem may be that value is not in scope when you reffer to it - please show full code... Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953682 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 You have an infinite loop there. If you want to loop through an array like that you have to set $value to the current element that's pointed to by the internal array pointer using current(), then every loop move that pointer using next() Like this: while($value = current($errors)) { // Do whatever you want with $value here next($errors); } Alternatively here are some other examples on how to loop through arrays. for($i = 0;$i < count($errors);$i++) { // $errors[$i]; } foreach($errors as $key => $val) { } Or even something like: for(;$value = current($errors);next($errors)) { // Do something with $value } Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953688 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 You can also use list and each with a while loop: while ( list($value) = each($errors) ) { if ( empty($value) ) echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>\n\r"; } Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953693 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 Thank you for all your comments... Alexwd: Thank you for explaining the array situation to me. I never thought of the concept that the array does not "auto pass" to the next value, so logically speaking it would go on to the next value. I however tried your option using current and next, but: * When there is nothing entered in the fields (empty fields) then still does not return the list error, but atleast does not crash the script * However, when I enter values, it crashes the script (weird ... ) Andy-H: I want to correct myself by saying that the code was not an function yet (but I plan to make it one) so apologise for the misinformation. Your second post came the closest so far, when there is nothing in the fields, it returns the list error, but now when there is something in the field, it still returns the error. I did a var_dump on the array and the variables has definately been entered in the array, so the array definately is not empty. Any ideas why it would do this? Logically speaking, the simplest way to do this i guess is to not use a while and do all validation manually, but I am so eager to learn new ways (and easier ways) that I really want things like this to work. Thank you for your prompt and quick responses Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953701 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 I also tried this, but this appears to do just the opposite. Irrelevant if I enter values in the fields or not, it does not give me the error message: $errors = array($tradename,$regname); echo "<ul>"; for($i = 0;$i < count($errors);$i++) { if (empty($errors) ) { echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953705 Share on other sites More sharing options...
Andy-H Posted November 8, 2009 Share Posted November 8, 2009 while ( list($value) = each($errors) ) { if ( !isset($value) || strlen($value) === 0 ) { echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li> "; // remove the next line if you wish to display the error once per empty value break; } } Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953706 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 I also tried this, but this appears to do just the opposite. Irrelevant if I enter values in the fields or not, it does not give me the error message: $errors = array($tradename,$regname); echo "<ul>"; for($i = 0;$i < count($errors);$i++) { if (empty($errors) ) { echo "<li>There are some manditory fields empty, please complete all fields with the * (astericks) next to it!</li>"; } } The reason for this is because you'll need to pass $errors[$i], passing just $errors into empty will be checking the $errors array, and not specific elements. Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953738 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 Hey Andy-H Nope, still nothing - The last post you made does not return any errors irrelevant if the fields are empty or not. Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953755 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 Post your entire code. Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953756 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 Alex, sorry for previous post, you posted the code while I sent that message. Your reply worked, I needed to set the $errors[$i] Thank you soo much to both for your assistance. I have also never used a for() loop, as I have always gotten away with while() - It would appear that the problem has been the actual variable with if(empty($value)) where $value never picked up the actual values within the array like $value[""] - How would I go about doing this with a while loop? I know the topic is solved, but I would really like to get a grip on arrays. (as you can see I am still very new to php) while($value = current($errors)) { // Specifically with the following line, how would I complete the $value within a while, or must I just do it like $value["0"]; which will then self populate the rest as the while is running? if(empty($value[""]) { echo "do something ..."; next($errors); } } Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953758 Share on other sites More sharing options...
mikesta707 Posted November 8, 2009 Share Posted November 8, 2009 if you are taking the current() next() method, doing the following should work while($value = current($errors)) { // Specifically with the following line, how would I complete the $value within a while, or must I just do it like $value["0"]; which will then self populate the rest as the while is running? if(empty($value) { echo "do something ..."; } next($errors); } this is because you store the current value of the array into the variable $value. Basically, all that current does is return whichever element the internal array pointer is pointing at (read the php manual for more info on how this works) and next changes the internal pointer to the next element Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953759 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 Hi Mikesta707 Thanks for your reply, and yes, it makes perfect sense now. I think the reason why it did not work earlier was that I actually had the next($errors); within the if statement in the while loop as appose to the outside of it. I may be wrong, but hell, it should work and logically speaking, the for() solution given earlier works, so im done fiddling around with this. Thank you all for your help with such a minor task, now for the function Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953764 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 Actually no, mikesta707, there's a reason it will not work. empty($value) would never even get a chance to run, because if $value was empty, the while statement wouldn't evaluate to true in the first place. Making this an ineffective way to loop through an array that may contain empty values. Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953765 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 mmmm ... okay, so if I understand you correctly, if no value was passed (meaning the array fields are empty) the while statement would not run because there is no values to run. This makes even more sense But out of curiousity, does this not also work with the for the same, as the for says: for($i = 0;$i < count($errors);$i++) if $errors has nothing in it, it should revert back to 0, meaning the for loop should also not run? Or am I mistaken? OR does the for loop work with the actual variables and not if the variables are empty? Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953769 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 I know the topic is solved, but I have another related question to this. The function sounds simple enough, but I just want to find out how I will be going on about getting all of the variables into the function? From my limited understanding, when declaring a function I need to stipulate which function variables will be going in, and the correct order - function name($var1, $var2) In my case, the variables will never be the same and never the same amount of variables as it depends on the amount of fields was in the form. Now I know that I will work with an array on this one, so my question is do I bring the actual array in the function or do I set the array outside of the function and then use it as a variable when I use the function e.g. // outside of function $array = array($var1,$var2,$var3); FUNCTIONNAME($array); or can I send all the variables into the function and then have the actual array inside of the function? Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953777 Share on other sites More sharing options...
nvee Posted November 8, 2009 Author Share Posted November 8, 2009 double post, sorry Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953778 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 Technically the for statement is just looping through incrementing the variable $i from 0 until it gets to number of the highest element in the array. Here's an example that explains it better: $array = Array('something', 'something else', 'something else'); for($i = 0;$i < count($array);$i++) { echo "$i<br />"; } If you run that you'll notice you get 0, 1, 2 which are the indexes of the array. So if you do: $array = Array('something', 'something else', 'something else'); for($i = 0;$i < count($array);$i++) { echo $array[$i] . '<br />'; } You're getting all the elements of the array, it's the same as: echo $array[0] . '<br />'; echo $array[1] . '<br />'; echo $array[2] . '<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/180764-solved-simple-validation-function-makes-php-to-stop-respond/#findComment-953783 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.