Jump to content

Check variables in Foreach


christa

Recommended Posts

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!

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.