dizel247 Posted August 17, 2006 Share Posted August 17, 2006 Hello,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 CarriersTable2 ContactsMy 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 <?php $companies = $_GET['cbox']; foreach($companies as $id => $carrier){ echo "Carrier ID: $carrier <br/>"; } ?>CarrierID 3CarrierID 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 emailrepeat loop.Can some one please point me to the right direction. I am so uncofortable with loops.Thanks in advance,Roman Quote Link to comment Share on other sites More sharing options...
-Mike- Posted August 17, 2006 Share Posted August 17, 2006 An assumption about your checkboxes on your form is as follows:[code]<input type="checkbox" name="id[]" value="CourierPrimaryKey"/>[/code]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:[code]$array = $_POST['id'];[/code]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);[code]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");}[/code]That's pretty basic, no error checks etc at all. Important parts:Your form:[code]<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" /></form>[/code]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)[code]$variable = $_POST['id'];[/code]Now your $variable variable is holding an array.Looping through this array:[code]foreach($variable as $item){//code to execute in each loop here}[/code]You can reference each value/object in your array by stating $item.[code]foreach($variable as $item){echo $item . "<br />";}[/code]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.[code]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!}[/code]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 ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.