Jump to content


Mr-Chidi

Member Since 20 May 2008
Offline Last Active Mar 25 2017 10:43 AM

Posts I've Made

In Topic: Send multiple rows to one email

10 February 2017 - 07:42 PM

Before you start the loop, you set the email content to the message and table headings.

 

However, within the loop, after sending an email, you set the content back to an empty string. You need to set it to the message and headings again. You then add the rows for the next customer.

 

Hello and thanks for the response. I have tried different ways from what i understand you said and all to no avail. If you can, use my code to clarify me pls. Thanks


In Topic: Send multiple rows to one email

08 February 2017 - 08:38 PM

 

The process is simply ot loop through the results, check for a a change in username and send email when it changes.

 

Here's a pseudocode description of the process

set currentcust = ''
set email content = ''
while fetch next row
    if username != currentcust                  // have we a new username?
        if currentcust != ''                    // don't sent to empty customer
            send email to currentcust
        endif
        set currentcust = username              // save new username
        set emailcontent = ''
    endif
    append billing detail to email content      // build email content
endwhile
send email to currentcust                       // don't forget to sent to last customer in data

 

And after following your guide, I was able to get the result on the debug mode but the message and the table headings did not display. I even tried to use it to test it live but did not get any mail.

 

$currentcust = '';
$emailcontent="
<div class='messages'>
<h3><img src='cid:my_logo'></h3>
<br>
<h4><div style='font-size:15px;'>Email Notification </h4>
<br>
<p>Dear: Client</p>
<p>
The following bills are due for payment:
</p>
<table width='80%' border='0' cellspacing='0' cellpadding='0'>
<tr style='font-weight:bold;'>
<td>Trans Ref</td>
<td>Due Date</td>
<td>Days Overdue</td>
<td>Service Provider</td>
<td>Service Type</td>
<td>Amount Paid</td>
<td>Bill Status</td>
<td>Recurring</td>
</tr>

";
while($rows = $st->fetch(PDO::FETCH_ASSOC)):
    $username = $rows['username'];
    if($username != $currentcust):
    
        if($currentcust != ''):
        #$mail->addAddress($currentcust);
        echo "Creating email for user '{$currentcust}'<br><br>\n";
        echo "Message:'<br>{$emailcontent}<br><hr><br>\n";

        endif;
        $currentcust = $username;
        $emailcontent = '';
        endif;
        
        $emailcontent .= " <tr>";
        $emailcontent .= " <td>".$rows['trans_ref']."</td>";
        $emailcontent .= " <td>".$rows['due_date']."</td>";
        $emailcontent .= " <td>".$rows['days_diff']."</td>";
        $emailcontent .= " <td>".ucwords($rows['service_provider'])."</td>";
        $emailcontent .= " <td>".ucwords($rows['service_type'])."</td>";
        $emailcontent .= " <td>".number_format($rows['amount_paid'],2)."</td>";
        $emailcontent .= " <td>".$rows['bill_status']."</td>";
        $emailcontent .= " <td>".$rows['recurring']."</td>";
        $emailcontent .= " <td>".$rows['bank_url']."</td>";
        $emailcontent .= " </tr>";
        
    endwhile;
    #$mail->addAddress($currentcust);
    
    echo "Creating email for user '{$currentcust}'<br><br>\n";
    echo "Message:'<br>{$emailcontent}<br><hr><br>\n";

In Topic: Send multiple rows to one email

08 February 2017 - 08:33 PM

Looks like a typo

 

while($row = $st->fetch(PDO::FETCH_ASSOC))

then

$message .= "    <td>".$rows['trans_ref']."</td>";

I noticed that from the beginning and corrected the typo. Don't think its the problem


In Topic: Send multiple rows to one email

08 February 2017 - 07:38 PM

As I was stating before, you need to learn how to debug. You state that you were not getting any output when adding the echo statements. Were you getting NO output or were getting the following with no data:

 

 

 

If you are getting NO output at all, then either there are no records returned from the query or there is some other error causing the script to exit prematurely. If you are getting the above message with no content, then either the field returned from the query are empty or the wrong field names are referenced in the logic.

 

Since you did not provide explicit details on what exactly was returned, I can't tell you what you should do next. But, for example, if you had NO output at all, then you could do the following:

while($row = $st->fetch(PDO::FETCH_ASSOC))
{
    $records[$rows['username']][] = $row;
}
//Add the following
echo "Debug line: Content of records variable<pre> " . print_r($records, 1) . "</pre>";

If that line is not displayed at all, you likely have some type of critical error occurring earlier in the script. If it is displayed with an empty array, then the issue is probably with the query or there are no records matching the condition. If that line is displayed with records in the array, then the issue is likely later in the script in using that data.

 

When you have an issue and have no idea where the actual problem is, pick a point in the logic and generate some output with relevant variables so you can inspect what the values are. You can then make a determination if the problem is created before or after that point. Then, based on the results, pick a different point in the logic to inspect. Rinse and repeat till you find the problem. As you get more experienced, you can add debugging logic into your code from the very beginning that you can turn on/off or which is logged. But, you should never create such debugging code that will be presented to the user.

 

Hello and thanks for the response.
when i added the following:

 

echo "Debug line: Content of records variable<pre> " . print_r($records, 1) . "</pre>";

It echoed the result in array.
Maybe I am not making my self clear enough but what I am saying is that it echoed the message with the table headings but no details. I have tried so many debugging that i could muster but all to no avail.

It echoes:

$message = "
        <div class='messages'>
        <h3><img src='cid:my_logo'></h3><br>
        <div style='font-size:16px; font-weight:bold; text-align=center; margin-bottom:10px;'>Email Notification on Bill Due for Payment</div>
        <p>Dear: Customer</p>
        <p>The following bills are due for payment:</p>
        <table width='80%' border='0' cellspacing='0' cellpadding='0'>
          <tr style='font-weight:bold;'>
            <td>Trans Ref</td>
            <td>Due Date</td>
            <td>Days Overdue</td>
            <td>Service Provider</td>
            <td>Service Type</td>
            <td>Amount Paid</td>
            <td>Bill Status</td>
            <td>Recurring</td>
          </tr>";

I think the problem should be withing this area cos thats whats not echoeing:

foreach($userRecords as $record)
    {
        $message .= "<tr>";
        $message .= "    <td>".$rows['trans_ref']."</td>";
        $message .= "    <td>".$rows['due_date']."</td>";
        $message .= "    <td>".$rows['days_diff']."</td>";
        $message .= "    <td>".$rows['service_provider']."</td>";
        $message .= "    <td>".$rows['service_type']."</td>";
        $message .= "    <td>".$rows['amount_paid']."</td>";
        $message .= "    <td>".$rows['bill_status']."</td>";
        $message .= "    <td>".$rows['recurring']."</td>";
        $message .= "</tr>";    
    }

I have learnt debugging skill from your debug method which i have applied to some other areas in my app.

Hope i am clearer now.

Thanks

In Topic: Send multiple rows to one email

02 February 2017 - 09:31 PM

Didn't work does not provide any details as to what the problem may be. You didn't even provide the modified code that you are currently using.

 

Not trying to be rude, but you need to learn how to debug code. My signature includes the statement "I do not always test the code I provide, so there may be some syntax errors.". I expect the person receiving the code to do the testing. I am providing the "logic".

 

So, going back to my code, the first debugging step I would add would be to generate some output on the web page to SEE what values are being generated instead of waiting for emails.

    //Send the email to the current user
//    $mail->addAddress($username);
//    $mail->Body = $message;
//    $mail->send();
//    $mail->ClearAddresses();
 
//Debug lines
echo "Creating email for user '{$username}'<br><br>\n";
echo "Message:'<br>{$message}<br><hr><br>\n";

Change the last part of the code I gave you with that above, run it, and inspect the output to see if it is correct.

Thanks. And sorry for not being elaborate in my reply. I meant that a mail was sent but it was empty. The details of the bill was not sent along. Even after adding your debugging code, it brought out same empty message. I did a little alteration though cos the code gave an error and i noticed it was mere omission here:

 

##----------------------##
## Create and run query ##
##----------------------##
$bill_status = "OVERDUE";
$query = "SELECT username, trans_ref, due_date, service_provider,
                 service_type, amount_paid, bill_status, recurring,
                 DATEDIFF(NOW(), due_date) AS days_diff
          FROM bills
          JOIN login_details ON bills.user_id = login_details.user_id
          WHERE bills.bill_status = ?"
$st = $pdo->prepare($query);
$st->execute([$bill_status]);

 

I replaced it with:

 

##----------------------##
## Create and run query ##
##----------------------##
$bill_status = "OVERDUE";
$query = "SELECT username, trans_ref, due_date, service_provider,
                 service_type, amount_paid, bill_status, recurring,
                 DATEDIFF(NOW(), due_date) AS days_diff
          FROM bills
          JOIN login_details ON bills.user_id = login_details.user_id
          WHERE bills.bill_status = :bill_status";
$st = $pdo->prepare($query);
$st->bindValue(':bill_status', $bill_status, PDO::PARAM_STR);
$st->execute();

 

And

##--------------------------------------------------##
## Loop through records and create structured array ##
##--------------------------------------------------##
while($row = $st->fetch(PDO::FETCH_ASSOC))
{
    $records[$rows['username']][] = $row;
}

I added s o the row.

 

Thanks