0

I'm a noob here, but I all I want to do here is to generate a PHP Mail using all the rows and their values from a Database Table using MYSQLi method.

My Table Format ndr_confirm_id | order_id (INT) | tracking_no (BIGINT) | status (0,1) | update_time Estimated Rows in the Table : 50-100

I want to generate a table within the PHP Mail which lists values from ALL rows available in the table.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$con=mysqli_connect("localhost","admin","ABC1234!","my_krty");//database connection


$date = date('Y-m-d');
$e_date = date('d-m-Y');

//Email Receiver//
   $to = "[email protected]";

   //From Header//
   $header = "From: MyKrty<[email protected]>"."\r\n";
   $header .= "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html; charset=ISO-8859-1" . "\r\n";

   // Subject //
   $subject = "Request";


   // Message //
   $message = "<html>
    <head>
        <title></title>
        <link href='https://fonts.googleapis.com/css?family=Pacifico|Varela+Round' rel='stylesheet' />
    </head>
    <body style='font-family: ' varela=''>

        <table align='center' border='0' cellpadding='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; padding:7px; width:85%;'>
            <tbody>
                <tr>
                    <td style='text-align: center;'>
                        <strong>Tracking AWB</strong></td>
                    <td style='text-align: center;'><strong>
                        Order ID #</strong></td>

                </tr>";
$get_ndr = mysqli_query($con,"SELECT * FROM `oc_ndr_confirm` WHERE `status`='0'");
while ($row = mysqli_fetch_array($get_ndr))
{
$order_id = $row['order_id'];
$tracking_no = $row['tracking_no'];
foreach($row as $value){

$message. = "<tr><td style='text-align:center;'><strong>".$value['tracking_no']."</td><td style='text-align:center;'>".$value['order_id']."</td></tr>";
        }}

$message.= "            </tbody>
        </table>
        <p>
            &nbsp;</p>
        <p>
            &nbsp;</p>

        <p>
            We request you to kindly re-schedule the following above at your earliest.</p>

    </body>
</html> \r\n";    

   //Send Mail//
   $mail_send = mail($to,$subject,$message,$header);

I've tried a lot of Threads, but unable to figure out, as I don't understand arrays much. The mail function is working fine, but I'm just receiving one row only in the table, with values as 2 in both columns.

The last thread I referred to was How to Send Email with all Products in the Cart .

Though, I couldn't make it referring the above mentioned thread as it was too confusing.

1 Answer 1

1

Here are 2 small edits to your code. First a small error in your html. Secondly you have a while loop already where you set your db values in your variables. No extra foreach loop is needed.

 <body style='font-family:  varela'>


while ($row = mysqli_fetch_array($get_ndr)) {
  $order_id = $row['order_id'];
  $tracking_no = $row['tracking_no'];
  $message. = "<tr><td style='text-align:center;'><strong>".$tracking_no."</td><td style='text-align:center;'>".$order_id."</td></tr>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.