Sending items fetched by while loop from mysql database in email

Need some help.

I am trying to create a script that sends an html email to a shopper with a list of all the orderd items. I want the items fetched from the database to be stored in a single variable so that I can embed it in the message variable of mail() function. Or Just the Items fetched to be embeded in the email.

Have tried the code below but it only displays 1 item and not all that are in the loop. How can I do it?

I'll be grateful for your help.

$message3 = "
    </td>
</tr>
<tr bgcolor='#A8CE58'><td><b>ITEMS ORDERED</b></td></tr>
<tr>
    <td>
        <table border='' style='margin-bottom: 10px; margin-top:10px; padding: 1px;border-right:  : 1px solid #000;'>
            <tr align='left'>
                <th>---</th>
                <th>Name</th>
                <th>Qty</th>
                <th>Total</th>
            </tr>";


        $ress= mysqli_query($server, "SELECT * FROM ordered_products WHERE session='$session'");
            mysqli_data_seek($ress, 0);
            while($colms=mysqli_fetch_array($ress, MYSQLI_NUM))
             {
                $message4 =
                "                                   
                    <tr align='left'>
                        <td><img src='e_images/$colms[8]' height='auto' width='50px'></td>
                        <td>$colms[2]</td>
                        <td >$colms[9]</td>
                        <td>ksh.$colms[5]</td>
                    </tr>";


             }
$message = "$message3". $message4;

1 answer

  • answered 2018-04-14 14:21 sietse85

    You need to concatenate the string

    $message4 = '';
    
    while($colms=mysqli_fetch_array($ress, MYSQLI_NUM)) {
        $message4 .= //your html
    }
    

    What you doing now is overwrite the value of $message4 each time in the loop

    With

    .= 
    

    You solve this. Don't forget to declare the variable before the loop, else you will get a warning from PHP.