oeroesmoeloevoeloe Posted January 30, 2009 Share Posted January 30, 2009 Hi all, I'm trying to make a method that makes it easier for me to generate errors when using forms. The main idea is that I call a static method to which I give an array which contains 1. The text entered by the user 2. The error message that will be given when the entered text is empty. The method will then return a list of errors, or it will return an empty string. If the string is empty I know that the user filled in everything needed. My form posts to itself and looks like this : <form method="POST" action="admin.php"> <table class="tableForDiv" cellpadding="3"> <tr class="tblRowHeader"> <td colspan="2">Nieuws</td> </tr> <tr> <td colspan="2"> <label class="alignTopLeft" for="newsMessage"><strong>Nieuwsbericht</strong></label> <textarea cols="70" rows="3" name="newsMessage"></textarea> </td> </tr> <tr> <td class="fillSpaceInAdminNewsForm"></td> <td> <input type="submit" name="submitNews" value="Post bericht"> <input type="reset" name="reset" value="reset"> </td> </tr> </table> </form> Above the form I have this php code where I try to check if the textarea 'newsMessage' has any text. I'm using an array (correct way?) to store the values. <?php if (isset($_POST['submitNews'])) { $message = $_POST['newsMessage']; $message = mysql_real_escape_string($message); $fieldarray[0] = $message; $fieldsarray[1] = "Geef een berichttekst op"; $errorstring = error_builder::give_error_string($fieldarray); if(!empty($errorstring)) { echo $errorstring; } } ?> and finally my php class : <?php class error_builder { public static function give_error_string($array) { $noErrors=true; $errorString = "Gelieve het volgende te corrigeren : <div class='divLoginErrors'><ul>"; $fieldarray = array($array); for($i = 0; $i<=count($fieldarray); $i+2) { if(empty($fieldarray[$i])) { $message = $fieldarray[$i+1]; $errorString.="<li>$message</li>"; $noErrors = False; } } if(!$noErrors) { $errorString.="</ul></div>"; } else { $errorString=""; } return $errorString; } } ?> I'm doing $i+2 everytime to go to the next field. I know there is probably a better way of doing this (double array I guess?) but I'm new to php programming and am not really sure how to do it When I run all of the above code, I think I generate an andless loop. I see the wait cursor forever when I press the submit button. When i put the for loop in comments all goes well, so I guess I'm creating some kind of endless loop? Or does it have anything to do with the static method I'm trying to call? I hope you guys can help me! If you need anything else, please ask and I'll post it. thank you! Quote Link to comment https://forums.phpfreaks.com/topic/143141-having-an-endless-loop-what-im-i-doing-wrong/ Share on other sites More sharing options...
Zhadus Posted January 30, 2009 Share Posted January 30, 2009 $i+2 should be $i += 2 I believe. Quote Link to comment https://forums.phpfreaks.com/topic/143141-having-an-endless-loop-what-im-i-doing-wrong/#findComment-750713 Share on other sites More sharing options...
lonewolf217 Posted January 30, 2009 Share Posted January 30, 2009 i think ^^ is right. simplest way to troubleshoot loops is always to echo your counter variable inside the loop to make sure it is incrementing properly Quote Link to comment https://forums.phpfreaks.com/topic/143141-having-an-endless-loop-what-im-i-doing-wrong/#findComment-750716 Share on other sites More sharing options...
Maq Posted January 30, 2009 Share Posted January 30, 2009 $i+2 should be $i += 2 I believe. Yes, you're adding 2 to $i but never assigning it. So $i is always the same... You should echo $i with your current code and then with Zhadus's solution. Quote Link to comment https://forums.phpfreaks.com/topic/143141-having-an-endless-loop-what-im-i-doing-wrong/#findComment-750717 Share on other sites More sharing options...
oeroesmoeloevoeloe Posted January 30, 2009 Author Share Posted January 30, 2009 fast responses wow but indeed, adding the $i+=2 seems to have solved the loop problem, but I don't get any output atm. it seems that this code does not output anything. if(empty($fieldarray[$i])) { $message = $fieldarray[$i++]; $errorString.="<li>$message</li>"; echo "$i"; $noErrors = False; } is my reasoning wrong for empty($fieldarray[$i]) ? Quote Link to comment https://forums.phpfreaks.com/topic/143141-having-an-endless-loop-what-im-i-doing-wrong/#findComment-750747 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.