I have a classifieds website and I am making a mod which will mail my users when an ad is posted in the category and in the city where they are subscribed. Now, I have a problem. I have set this file to run once a day with cronjob. In a day, in my website are 50 ads posted per day so the user gets 50 emails a day. I want to include all the 50 emails in a single one, so the user will get 1 email, but I do not know how to do that. Ex: Each email has one link and 50 emails have 50 links. I want that the users receive only a single email which will have all the 50 links in it. I have tried so many ways, but I could not do it. Every suggestion is welcomed.
Thanks
simply gather your ad-information in an array, where you set the email as array-key.
$recipients = array();
while ($row = mysql_fetch_assoc($result)) {
$email = $row["email"];
if (!array_key_exists($email, $recipients)) {
$recipients[$email] = array();
}
$recipients[$email][] = $row;
}
After this, you have an array and each email has a variable number of $row-entries. You just need to add these entries to your mail template using a foreach-loop.
This code is too bad to change it, it's much simplier to write new one. I can suggest you to create array and put information for emails to it instead of sending immediately. If the array already contains item with the same email, don't add new email but add information to existing email. After fetching all emails iterate through new array and send emails.
Since you're already running a cronjob for the script, just set an appropriate time (early morning or night) and query the database you store the postings from the classifieds in, select the last 50 posted, add them into the email and send them out to the users registered to receive the latest postings email. Seems pretty straight forward, unless I misunderstood something.
Also, I'm very curious why (in your script above) you jump in and out of PHP after every little block of code. It's very distracting, and frankly caused me to not even want to read the code. There's no reason from what's displayed above to ever leave PHP - unless those were all little snippets you included together to display here. If that's the case, ignore my complaints.
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
/scratcheshead