Current location: Hot Scripts Forums » Programming Languages » PHP » foreach problems


foreach problems

Reply
  #1 (permalink)  
Old 07-13-09, 12:33 AM
gilbertsavier gilbertsavier is offline
New Member
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
foreach problems

Hi,
I have a list of names, emails, a checkbox and a message box, the names and emails are from a database. What I am trying to do to select say 5 of the names from the list via the checkbox and send an email to those who I have picked.

I can't seem to get my head around the foreach function, is it that there has to be a foreach function to collect the email addresses and to see which user has been selected or do they come under the same one.

I have tried to do some of the coding myself, but fear I am getting more and more confused, some help would be great. Here is what I have done so far.

---------------
Thanks & regards
Lokananth
Live chat By miOOt
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-17-09, 02:24 AM
pyr0t3chnician pyr0t3chnician is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 30
Thanks: 0
Thanked 1 Time in 1 Post
Typically the ids of the checkboxes will be able to pull up the data to send the email. You would only need 1 foreach loop if that is the case.

Code:
foreach($checkbox as $id){
//pull up the info from mysql or other info using $id.
//send the email.
}
You might want to play around with checkboxes for a while and see how it is passed as a post variable and how you can manipulate it. I am working with something similar where the user can click a check box, and with the selected ones, they can perform a set of given actions (delete, update, move, etc).

I'm sorry I can't give you exact code because I am not sure exactly what/how you are doing this. Good luck.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-17-09, 04:57 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Here is a simple program that will send a message to the name/email pairs you have in your database.
One thing to consider though, is the amount of time it will take to send the emails.
If it takes too long then the program may timeout.
So you may want to send only 10 or 20 at a time.
You could set a limit in your query to 10 or 20, then just page through the records if you have more then the limit is set for.

You will need to have a table in your database with columns 'name' and 'email' for the query to work.
Otherwise change the column names in the query to match the column names in the table.
And change the values of
$host
$user
$password
$db
$table
to match your configuration.
PHP Code:

<?php
if(!empty($_POST["submit"]))
{
 
$subject "Hello friend";
 
$headers  'MIME-Version: 1.0' "\r\n";
 
$headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
 
$headers .= 'From: job0107@comcast.net' "\r\n";
 
$msg stripslashes($_POST["msg"]);
 foreach(
$_POST as $name => $email)
 {
  if(
$name == "msg" || $name == "submit"){}
  else
  {
   
$parts explode(",",$email);
   
$body "Hello ".$parts[0].",<br />".$msg;
   
mail($parts[1],$subject,$body,$headers);
   }
  }
 }
?>
<html>
<head?
<title></title>
</head>
<body>
<form action="#" method="post">
<?php
$host 
"localhost"// mysql host address
$user "root";      // mysql username
$password "";      // mysql password
$db "test";        // mysql database name
$table "client";   // mysql table name

mysql_connect($host,$user,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$sql "SELECT name,email FROM $table";
$results mysql_query($sql) or die(mysql_error());
$ct=0;
while(
$row mysql_fetch_assoc($results))
{
 echo 
"<input type='checkbox' name='client".$ct."' value='".$row["name"].",".$row["email"]."'> ".$row["name"]."<br />";
 
$ct++;
 }
?>
<br />Message: <textarea name="msg"></textarea>
<p>
<input type="submit" value="Submit" name="submit">
</form>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach function acting up soloWebDev PHP 5 08-07-07 11:20 AM
Classified Ads skipper23 Perl 3 11-22-05 03:22 AM
Foreach Variable/array Scope DAL PHP 5 10-22-05 02:38 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Classified Ads skipper23 Perl 2 12-30-03 04:43 AM


All times are GMT -5. The time now is 11:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.