christa Posted October 26, 2009 Share Posted October 26, 2009 hi all in a foreach as this one: $recipient = explode(",", $_POST['recipient']); foreach($recipient as $numbers) { if( is_int($numbers) ){ echo "OK"; } else { echo "ERROR: NO INTEGER"; } doesn't works: it check only the last value of $recipient. Where is the mistake? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/ Share on other sites More sharing options...
Bricktop Posted October 26, 2009 Share Posted October 26, 2009 Hi christa, That code is correct. Are you able to post more code, or post the output of $_POST['recipient']? You may need to use trim(). Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944673 Share on other sites More sharing options...
christa Posted October 26, 2009 Author Share Posted October 26, 2009 now, i'm using explode("\n") but the error is the same. In a textarea, i digit: 123 456 789 And the output of $recipient is: 123 456 789 And the output of $_POST['recipient'] is: 123 456 789 Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944689 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 <?php $recipient = explode("\n", $_POST['recipient']); foreach($recipient as $number) { if(is_numeric($number)){ echo "OK"; } else { echo "ERROR: NO INTEGER"; } } ?> Check loop & condition braces Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944690 Share on other sites More sharing options...
Bricktop Posted October 26, 2009 Share Posted October 26, 2009 Hi christa, Try PHP's trim() function. For example: $recipient = explode("\n", $_POST['recipient']); foreach($recipient as $numbers) { if(is_int(trim($numbers)) ){ echo "OK"; } else { echo "ERROR: NO INTEGER"; } } Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944692 Share on other sites More sharing options...
christa Posted October 26, 2009 Author Share Posted October 26, 2009 with is_int() doesn't works with is_numeric works but it also accepts - and + My goal is check all values 123 456 789 but i must to accept it only if everyone are integer. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944703 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 Because input from a form is always a string so you cannot use is_int(). Use is_numeric() or ereg. Bricktop: nothing to do with trimming however good idea to trim each array element. if(ereg('[0-9]+', $number, $regs)) { print "Numeric"; } Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944705 Share on other sites More sharing options...
Bricktop Posted October 26, 2009 Share Posted October 26, 2009 Yes, true - thanks Neil - I totally missed the fact that christa was using is_int(). is_int() assumes a string of numbers is a string and not an integer so can give unexpected results. It's best to use is_numeric() (or ereg as Neil says) in this scenario. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944708 Share on other sites More sharing options...
christa Posted October 26, 2009 Author Share Posted October 26, 2009 ok. How can I continue only if all the values that I enter in textarea are matched by erg()? Example: if i enter: 123 456 qwerty 789 the code goes on and print OK. Instead, i must go on only if all values entered are matched by ereg (= only numbers 0-9) Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944712 Share on other sites More sharing options...
Bricktop Posted October 26, 2009 Share Posted October 26, 2009 Hi christa, You could use PHP's break statement to break out of the foreach loop. For example: if(is_int(trim($numbers)) ){ echo "OK"; } else { echo "ERROR: NO INTEGER"; break; } Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944717 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 I would log the invalid entries <?php $recipient = explode("\n", $_POST['recipient']); if(is_array($recipient)) { $results = array(); foreach($recipient as $number) { $number = trim($number); if(ereg('[0-9]+', $number, $regs)) { // number is valid $results['valid'][] = $number; } else { // entry is invalid $results['invalid'][] = $number; } } // check for invalid entries if(count($results['invalid'])) { print "The following entries are invalid:<br />".implode("<br />",$results['invalid'])."<br />"; } // display valid entries if(count($results['valid'])) { print "The following entries are valid:<br />".implode("<br />",$results['valid'])."<br />"; } } else { print "Missing input"; exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944734 Share on other sites More sharing options...
christa Posted October 26, 2009 Author Share Posted October 26, 2009 I would log the invalid entries with your code, if the first value entered is good, the code goes on. Also if i put a break; Example: 123 qwer ewrwe ewrrtt The 123 value is accepted and inserted into mysql; The others not. Where is the mistake? Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944759 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 There is no mistake. That is what it is meant to do. Only accept the numbered entries and ignore the rest. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944785 Share on other sites More sharing options...
christa Posted October 26, 2009 Author Share Posted October 26, 2009 ok. Thank you very much for all code posted!! C. Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-944836 Share on other sites More sharing options...
christa Posted October 29, 2009 Author Share Posted October 29, 2009 sorry if i reopen this topic. I've seen an issue in the code of neil.johnson If i write something as this: 123 546 456 esr 564 opo the code always will print : those entries are invalid: esr opo those entries are valid: 123 546 456 564 There is a way to print like this one? Valid entries: 4 Invalid entries: 2 and viceversa Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947074 Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 <?php print "Valid Entries ".count($results['valid'])."<br />"; print "Invalid Entries: ".count($results['invalid']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947089 Share on other sites More sharing options...
christa Posted October 29, 2009 Author Share Posted October 29, 2009 <?php print "Valid Entries ".count($results['valid'])."<br />"; print "Invalid Entries: ".count($results['invalid']); ?> in this way, it will always print Valid Entries: 6 Invalid Entries: 6 because it check always all entries in the textarea: 1 esr 2 opo 3 123 4 546 5 456 6 564 Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947116 Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 No it wont. Are you sure the correct keys are used? $results['valid'] $results['invalid'] Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947118 Share on other sites More sharing options...
christa Posted October 29, 2009 Author Share Posted October 29, 2009 yes, the code i'm using is the same. Where is my mistake? Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947123 Share on other sites More sharing options...
JonnoTheDev Posted October 29, 2009 Share Posted October 29, 2009 This is 100% correct. You must have ammended it somewhere. <?php $recipient = explode("\n", $_POST['recipient']); if(is_array($recipient)) { $results = array(); foreach($recipient as $number) { $number = trim($number); if(ereg('[0-9]+', $number, $regs)) { // number is valid $results['valid'][] = $number; } else { // entry is invalid $results['invalid'][] = $number; } } // check for invalid entries if(count($results['invalid'])) { print "Invalid Entries: ".count($results['invalid'])."<br />"; //print "The following entries are invalid:<br />".implode("<br />",$results['invalid'])."<br />"; } // display valid entries if(count($results['valid'])) { print "Valid Entries: ".count($results['valid']); //print "The following entries are valid:<br />".implode("<br />",$results['valid'])."<br />"; } } else { print "Missing input"; exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947136 Share on other sites More sharing options...
christa Posted October 29, 2009 Author Share Posted October 29, 2009 ok, i reviewed my code: now is correct. Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/179051-check-variables-in-foreach/#findComment-947169 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.