mark110384 Posted June 16, 2008 Share Posted June 16, 2008 Hey guys, I'm trying to email a table created from an array, any suggestions on how I can do that, at present the email is sent the number of times that a record is found, for exaple three records will generate three emails. Any suggestions would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/ Share on other sites More sharing options...
bobinindia Posted June 16, 2008 Share Posted June 16, 2008 sounds like the mail() function is inside your while loop. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566365 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 Yeah, but when I put the mail() function outside the loop it only post an email with the details regarding the last record Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566368 Share on other sites More sharing options...
bobinindia Posted June 16, 2008 Share Posted June 16, 2008 You will need to store the info for the table in a variable which gets a new name each while loop. ie ++1 onto each variable name. Then join them before mailing. each variable hold a row. $variable1.$variable2 etc Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566374 Share on other sites More sharing options...
bobinindia Posted June 16, 2008 Share Posted June 16, 2008 post the code. it will be quicker Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566375 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 Here you go guys, $result = mysql_query($sql); while($myrow = mysql_fetch_array($result)) { $itemId = $myrow['itemId']; $price = $myrow['price']; $quantity = $myrow['qty']; $date = $myrow['date']; $mailcontent = "Item Id: ".$itemId."\n" ."Quantity: ".$quantity."\n" ."Date: " .$date."\n" ."Customer email: ".$email."\n" ."Shipping Address: \n" .$shipadd."\n"; } mail($to, $subject, $mailcontent,$additional_headers); //at present it post the last result Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566379 Share on other sites More sharing options...
bkiatenxi Posted June 16, 2008 Share Posted June 16, 2008 why did you put the mail() outside the while loop? Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566401 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 When the mail() function is in the loop, it will send out an email with regard to how many records are found, for example if three records are found then the email will be sent three times. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566405 Share on other sites More sharing options...
bobinindia Posted June 16, 2008 Share Posted June 16, 2008 $i = 1; while($myrow = mysql_fetch_array($result)) { $itemId = $myrow['itemId']; $price = $myrow['price']; $quantity = $myrow['qty']; $date = $myrow['date']; $mailcontent.$i = "Item Id: ".$itemId."\n" ."Quantity: ".$quantity."\n" ."Date: " .$date."\n" ."Customer email: ".$email."\n" ."Shipping Address: \n" .$shipadd."\n"; $i++; } $mailcontent = $mailcontent1.$mailcontent2.$mailcontent3; //etc mail($to, $subject, $mailcontent,$additional_headers); Something along those lines. No expert myself!! Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566411 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 Thanks, tried that but doesn't to mail anycontent now Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566414 Share on other sites More sharing options...
bobinindia Posted June 16, 2008 Share Posted June 16, 2008 There is no email variable being made. You need a query that selects all items with the same email address and then make at table with the results in it. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566427 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 $result = mysql_query($sql); while($myrow = mysql_fetch_array($result)) { $itemId = $myrow['itemId']; $price = $myrow['price']; $quantity = $myrow['qty']; $date = $myrow['date']; $mailcontent.= "Item Id: ".$itemId."\n" ."Quantity: ".$quantity."\n" ."Date: " .$date."\n" ."Customer email: ".$email."\n" ."Shipping Address: \n" .$shipadd."\n"; } mail($to, $subject, $mailcontent,$additional_headers); Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566475 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 Hey thanks for the input guys but Ive found a soloution to the problem $i = 1; $mailcontent = ""; while($myrow = mysql_fetch_assoc($result)) { $itemId = $myrow['itemId']; $price = $myrow['price']; $quantity = $myrow['quantity']; $date = $myrow['createdate']; if ($i ==1){ $mailcontent = "Date:" . $date."\n" ."Customer Email: " . $email . "\n" ."Shipping Address: " . $shipadd."\n\n"; } $mailcontent .= "Item Id: ".$itemId."\n" ."Quantity: ".$quantity."\n\n"; $i++; } mail($to, $subject, $mailcontent,$additional_headers); } Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566561 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 ummm...i don't really see how what you've done will work. your previous $mailcontent is still being written over every single loop iteration. Did you try my code? You said that everything works fine except all you're getting is the info from the last loop. That's because your overwriting the data every single time. All I did to that code was add a dot before the = in your $mailcontent assignment, so it concats the new info onto the previous info...which is what you 1/2 did right there. You broke it up and used the dot but on the next loop iteration, your first = will just overwrite it. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566567 Share on other sites More sharing options...
mark110384 Posted June 16, 2008 Author Share Posted June 16, 2008 Yeah it works, I get the required number of records back, to be honest I didn't even spot the dot that you put in the coding, perhaps if i'd seen it I would have gotten to the soloution faster, thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/110397-email/#findComment-566591 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.