Jump to content


This topic is now archived and is closed to further replies.


Need help stock...

Recommended Posts


I am writing an email app that will email files to the companies that we are working with.

I have a 2 tables in mysql database.
Table Carriers
Table2 Contacts

My problem is this.
First I would like my users to select the what carriers to send email to.
I do that by using check boxes.

Then when I am on the next screen I need to send an email to each companies using their conctast from the database.

For each email Subject and Body uses that company name.

I belive I need to use Arays.

I use check boxes for carrier_id as my array.

So when I loop I get

  $companies = $_GET['cbox'];
  foreach($companies as $id => $carrier){
  echo "Carrier ID: $carrier <br/>";


CarrierID 3
CarrierID 4


Now problem 1.
How do I display carrier_id and carrier_name that belongs to that id.

Problem 2.
How do I pull emails from the database using loop refrencing carrier_id.

What I currently think i need to use Loop.

1. Loop carrier Name using carrier_id
    get user emails
      send email
repeat loop.

Can some one please point me to the right direction. I am so uncofortable with loops.

Thanks in advance,


Share this post

Link to post
Share on other sites
An assumption about your checkboxes on your form is as follows:
<input type="checkbox" name="id[]" value="CourierPrimaryKey"/>
Of course, the courier primary key will also identify the correct contact...

So when submitting the form, you will be passing an array.

The first bit is to get your values from the form and shove them into the array:
$array = $_POST['id'];

Then we need to loop through that array, query the database for the contacts email address/name and then send them the generic email (with added bits);
foreach($_POST['id'] as $id){ //loop through the array
$sql = mysql_query("SELECT * from CONTACTS WHERE CourierId = '$id'"); //run query to get the contact
$row = mysql_fetch_array($sql, MYSQL_ASSOC); //get the row.
$subject = "Dear " . $row['contactName'] . ", rest of title";
$body = "Dear " . $row['contactName'] . ", We wish to let you know and the rest of body";
mail($row['emailaddress'], $subject, $body, "FROM: myemail@email.com");

That's pretty basic, no error checks etc at all.
Important parts:
Your form:
<form name="myname" method="post" action="process.php">
<input type="checkbox" name="id[]" value="1"/>One<br />
<input type="checkbox" name="id[]" value="2"/>Two<br />
<input type="checkbox" name="id[]" value="3"/>Three<br />
<input type="checkbox" name="id[]" value="4"/>Four<br />
<input type="checkbox" name="id[]" value="5"/>Five<br />
<input type="checkbox" name="id[]" value="6"/>Six<br />
<input type="submit" value="submit" name="submit" />
Those, when submitted, would give an array containing the values of the selected items.
To get the array, in teh process.php (or where-ever it's being processed to)
$variable = $_POST['id'];
Now your $variable variable is holding an array.
Looping through this array:
foreach($variable as $item){
//code to execute in each loop here

You can reference each value/object in your array by stating $item.
foreach($variable as $item){
echo $item . "<br />";
That would print out the value of each item in the array with a line break between them.
This is how we email them, so we do a query to get EACH couriers contact details from the database during a loop. Each loop will stick the value of the next item in the array into the variable of $id, which we then use during a query to the database.
foreach($_POST['id'] as $id){
$sql = mysql_query("SELECT * FROM TABLENAME WHERE courierId = '$id'"); //the query
$row = mysql_fetch_array($sql, MYSQL_ASSOC); //fetch row, which we can get each field via it's name!

Using the $row, we can reference each query result's fields to get their values, so if you have a field called email storing the email address in the contacts database, you can say $row['email'] to get it's value.
Therefore, you can use the mail function of php (php.net, type mail into the seach box) with their email address returned from the query, and send the message to them. Each loop will do this until it has gone through all the values from your form.

If this isn't explained very well, or is confusing, please let me know ;)

Share this post

Link to post
Share on other sites


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.