Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting foreach() to display results once

Status
Not open for further replies.

camcim

Programmer
Jan 25, 2003
20
0
0
US
Hi all,
I've been playing with this bit of code but can't seem to stop it from inserting the company_id twice. The company_id value is a unique number corresponding to the email (checkbox_email)

Here is part of the code:
Code:
if($email){
function mkpasswd($nochars=8) 
{ 
mt_srand ((double) microtime() * 1000000); 
$letters=" ABCDEFGHJKLMPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz2345
6789"; 
$len=strlen($letters); 

for ($loop=0; $loop<$nochars; $loop++) 
{ 
$randno=mt_rand(0,$len); 
$password.=substr($letters, $randno, 1); 
} 
return($password); 

}

foreach ($_POST['checkbox_email'] as $value){
$password = mkpasswd($nochars=8);
$sendto = &quot;$value&quot;;   
$subject = &quot;Listing Renewal&quot;;   
$message = &quot;You can update now

Username: $value
Password: $password

Yours sincerely,
Bruce Fleming&quot;;
$from = &quot;From: Xer<webmaster@xer.com>&quot;;
mail($sendto, $subject, $message, $from);

foreach ($_POST['company_id'] as $cool){

$insert_email = &quot;insert into client_admin(client_admin_id,company_id,login_email,password) 
              values (NULL,'$cool','$value','$password')&quot;;
			  $result_email = mysql_query($insert_email);
			  if($result_email){
			  echo &quot;Email Sent&quot;;
			  } else {
			  echo &quot;Email could not be sent&quot;;
			  }

}
}
} else if($default == 'true'){

Can anyone see the problem?

Cheers,
camcim
 
You have a nested foreach statement, indent your code and this'll be more clear...

So for every email you process, you're going to process all the company id's.

Two boxes checked, 4 id's processed... 10 checked, 100 id's processed.

But 1 checked should be 1 processed. If even that's a problem you probably need to look into your HTML.

By doing a
echo &quot;<pre>&quot;;
print_r ($_POST);

You can diagnose the information being sent to your PHP script and that may help you decipher it.

Enjoy,
Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top