Jump to content

Mail Array Loop


Hilly_2004

Recommended Posts

Hi guys,

Wondering if you could help me. Im wanting to create a mailer which emails a user a list of details from the database in one email.

The user selects which people they wish to recieve more details of and enters their email address, and finally posts their ID's as part of an array e.g.

$studentIDs = $_REQUEST['studentIDs'];
$emailAddress = $_REQUEST['emailAddress'];


($studentIDs = "1-14-19-13...etc")

I then explode the array to retrieve the certain ID's e.g.

$idArray = explode("-", $studentIDs);

The next part is where I get stuck, how can I then select these users from the database and the email the results to the email address the user provided ($emailAddress).

I tried using a loop however this caused only the final entry that was exploded to be mailed to the user instead of a list of all of them. Hope this makes sense.
Link to comment
Share on other sites

This is going to be a coders nightmare so I apologise in advance ;)

[code]
<?php

include("../~lib/lib.php");
//check this is flash calling the script
//ignore it for now
//$error = authorise($_REQUEST['authorised']);
if (isset($_REQUEST['studentIDs'])) {
$error = 0;

//get student id's passed by flash
$studentIDs = $_REQUEST['studentIDs'];

//get email address to send to
$emailAddress = $_REQUEST['emailAddress'];

//create array of student id's
$idArray = explode("-",  $studentIDs);

foreach ($idArray AS $key => $value){
    
  //  echo "$value <br>";
    

//print student id's on screen for stephen to see
//print_r ($idArray);
//echo("<br />");

//print email address on screen for stephen to see
//echo ($emailAddress."<br />");



$message = '';

//-----GET ANY REPLIES TO THE TOPIC-----
$Query3 = "SELECT * FROM students_test WHERE studentID = '$value'";
$Result3 = mysql_query($Query3);
$num = mysql_num_rows($Result3);
if ($Result3) {
      while($row = mysql_fetch_array($Result3)) {
      
      $firstname = array($row['firstName']);
      foreach ($firstname as $cheese)
      {
      
      }
      
      $lastname = array($row['lastName']);
      foreach ($lastname as $ham)
      {
      
      }
      
      $email = array($row['email']);
      foreach ($email as $pickel)
      {
      
      }
     // echo "".$row['firstName']."";
      //echo "".$row['lastName']."";
        
//$message = $pickel;
      }
      $subject = "Hello Gareth...";

mail("$emailAddress", "$subject",
    "<html>
<body>
<table width=\"100%\"  border=\"0\">
  <tr>
    <td bgcolor=\"#0000FF\"><div align=\"center\">$pickel</div></td>
  </tr>
</table>
</body>
</html>",
    "To: The Receiver <$emailAddress>\n" .
    "From: The Sender <noreply@lineupthedots.com>\n" .
    "MIME-Version: 1.0\n" .
    "Content-type: text/html; charset=iso-8859-1");

      
      
      }
      }
      
      }
//-----SHOW ALL REPLIES
//$replies = 0;
//while ($replies < $num)
//{

//$lname = mysql_result($Result3,$replies,"lastName");
//$fname = mysql_result($Result3,$replies,"firstName");






//$replies++;
//} //-----END SHOW ALL REPLIES




//get those student's info from db and make a nice html email to send to user with all of selected student's info in :)
//don't print anything else to the screen
//if anything goes wrong, tell flash by setting $error = 1
//$error = 1;

//tell flash it's done
//echo("&error=".$error."&loaded=1");

?>

[/code]
Link to comment
Share on other sites

[!--quoteo(post=372654:date=May 9 2006, 12:52 PM:name=Hilly_2004)--][div class=\'quotetop\']QUOTE(Hilly_2004 @ May 9 2006, 12:52 PM) [snapback]372654[/snapback][/div][div class=\'quotemain\'][!--quotec--]
*Bump*
[/quote]
Hilly
Try the tutorial on this site all about php_mailer - take my advise and use phpmailer for all
emailing stuff.
[a href=\"http://www.phpfreaks.com/tutorials/130/0.php\" target=\"_blank\"]http://www.phpfreaks.com/tutorials/130/0.php[/a]

good luck
JRS
Link to comment
Share on other sites

Cheers for that JRS, Il try and employ some of the code into the workings of mine...

Ive managed to kind of do this loop thing I was on about however I know for a fact that it could and SHOULD be done much easier, instead Ive had use 10 sql queries to get 10 bits of information out of the database (10 being the maximum number of people a user can select), I know it should be done using an array but I dont know how....

[code]
if (isset($_REQUEST['studentIDs'])) {
$error = 0;

//get student id's passed by flash
$studentIDs = $_REQUEST['studentIDs'];

//get email address to send to
$emailAddress = $_REQUEST['emailAddress'];

//create array of student id's
$idArray = explode("-",  $studentIDs);

$test = array($idArray);
foreach($test AS $key => $value)

//---------------------------------------------------------------------------
$Query3 = "SELECT * FROM students_test WHERE studentID = '$value[0]'";
$Result3 = mysql_query($Query3);
$num = mysql_num_rows($Result3);
if ($Result3) {
while($row = mysql_fetch_array($Result3)) {
$firstname = $row['firstName'];
$blurb = $row['blurb'];
$lastname = $row['lastName'];

$course = $row['course'];
if ($course == 'i'){$course = "Interactive Multimedia";}
else if ($course == "w"){$course = "Web Design";}
else if ($course == "g"){$course = "Games Design";}

$web = $row['web'];
        }
    }
if($firstname == ''){}
else {
$user = "<strong>$firstname $lastname</strong><br>
    $course<br>
    <a href=\"http://$web\">$web</a><p>
    $blurb</p>
    <hr>";
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
$Query3 = "SELECT * FROM students_test WHERE studentID = '$value[1]'";
$Result3 = mysql_query($Query3);
$num = mysql_num_rows($Result3);
if ($Result3) {
while($row = mysql_fetch_array($Result3)) {
$firstname1 = $row['firstName'];
$blurb1 = $row['blurb'];
$lastname1 = $row['lastName'];

$course1 = $row['course'];
if ($course1 == 'i'){$course1 = "Interactive Multimedia";}
else if ($course1 == "w"){$course1 = "Web Design";}
else if ($course1 == "g"){$course1 = "Games Design";}

$web1 = $row['web'];
        }
    }
if($firstname1 == ''){}
else {
$user1 = "<strong>$firstname1 $lastname1</strong><br>
    $course1<br>
    <a href=\"http://$web1\">$web1</a><p>
    $blurb1</p>
    <hr>";
}

//---------------------------------------------------------------------------
$Query3 = "SELECT * FROM students_test WHERE studentID = '$value[2]'";
$Result3 = mysql_query($Query3);
$num = mysql_num_rows($Result3);
if ($Result3) {
while($row = mysql_fetch_array($Result3)) {
$firstname2 = $row['firstName'];
$blurb2 = $row['blurb'];
$lastname2 = $row['lastName'];

$course2 = $row['course'];
if ($course2 == 'i'){$course2 = "Interactive Multimedia";}
else if ($course2 == "w"){$course2 = "Web Design";}
else if ($course2 == "g"){$course2 = "Games Design";}

$web2 = $row['web'];
        }
    }
if($firstname2 == ''){}
else {
$user2 = "<strong>$firstname2 $lastname2</strong><br>
    $course2<br>
    <a href=\"http://$web2\">$web2</a><p>
    $blurb2</p>
    <hr>";
}

[/code]
etc...etc... but 10 times!!!!
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.