Search code examples
phpfor-loopphpmailer

PHP Mailer for loop


I am new in PHP. Currently, I am writing a PHP mailer to send the email for order confirmation. There's a list of transactions. I would like to make it in a table and append to the php mailer body. The list is from my backend service in json format. I got Parse error: syntax error, unexpected token "for".

Json:

{
    "email":"[email protected]",
    "address":"Barcelona",
    "address_type":"Office",
    "amount":12,
    "mobile":"7152820391",
    "name":"Boris Caria",
    "order_number":"20bs10d0ea15",
    "transaction_list":[
        {
            "transaction_id":"0012331213213",
            "transaction_amount":12,
            "transaction_time":"12:36:02"
        },
        {
            "transaction_id":"0012312132235",
            "transaction_amount":24.5,
            "transaction_time":"12:38:23"
        }
    ]
}

php:

$data = json_decode(file_get_contents('php://input'), true);
$mail->Body    = '<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet" type="text/css">
    <div class="email">
        <div style="font-size:16px;">
            <p>Dear Customer,</p>
            <br>
            <p>Thanks for shopping at <b style="color:#64b3f4;">E shop</b>!</p>
            <p>Order Number: <b>'. $data["order_number"].'</b></p>
            <p>Date: '. date('jS\, F Y h:i:s A'). '</p>
            <br>
            <p>Your order is below:</p>
            <table border="0">
              
              <tr>
                <td scope="col">Women\'s Light Blue Tunic</td>
                <td scope="col"> * 1</td>
                <td scope="col"> &nbsp; </td>
                <td scope="col">$31.00</td>
              </tr>
              <tr>
                <td scope="col"><b>Total</b></td>
                <td scope="col">&nbsp;</td>
                <td scope="col"><b> &nbsp; </b></td>
                <td scope="col"><b>$'. $data["amount"] .'</b></td>
              </tr>
            </table>        
            <table border="0">
              <tr>
                <td scope="col">Transaction ID</td>
                <td scope="col">Transaction amount)</td>
                <td scope="col">Transaction time</td>
              </tr>'.
               for($x = 0; $x < sizeof($data["transaction_list"]); $x++){
                <tr>
                <td scope="col">$data["transaction_list"][x][transaction_id]</td>
                <td scope="col">$data["transaction_list"][x][transaction_amount]</td>
                <td scope="col"> $data["transaction_list"][x][transaction_time].</td>
                </tr>
                }.'
            </table>
            <p>Shipping information is below:</p>
            <table border="0">
              <tr>
                <td scope="col">Full Name: </td>
                <td scope="col">'. $data["name"] .'</td>
              </tr>
              <tr>
                <td scope="col">Mobile number: </td>
                <td scope="col">'. $data["mobile"] .'</td>
              </tr><tr>
                <td scope="col">Address: </td>
                <td scope="col">'. $data["address"] .'</td>
              </tr><tr>
                <td scope="col">Address Type: </td>
                <td scope="col">'. $data["address_type"] .'</td>
              </tr><tr>
                <td scope="col">Email: </td>
                <td scope="col">'. $data["email"] .'</td>
              </tr>
            </table>
            <p>Thank you for your order.</p>
        </div>
    </div>';

Solution

  •      $data = json_decode(file_get_contents('php://input'), true);            
         $top    = '<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet" type="text/css">
        <div class="email">
            <div style="font-size:16px;">
                <p>Dear Customer,</p>
                <br>
                <p>Thanks for shopping at <b style="color:#64b3f4;">E shop</b>!</p>
                <p>Order Number: <b>'. $data["order_number"].'</b></p>
                <p>Date: '. date('jS\, F Y h:i:s A'). '</p>
                <br>
                <p>Your order is below:</p>
                <table border="0">
                  
                  <tr>
                    <td scope="col">Women\'s Light Blue Tunic</td>
                    <td scope="col"> * 1</td>
                    <td scope="col"> &nbsp; </td>
                    <td scope="col">$31.00</td>
                  </tr>
                  <tr>
                    <td scope="col"><b>Total</b></td>
                    <td scope="col">&nbsp;</td>
                    <td scope="col"><b> &nbsp; </b></td>
                    <td scope="col"><b>$'. $data["amount"] .'</b></td>
                  </tr>
                </table>        
                <table border="0">
                  <tr>
                    <td scope="col">Transaction ID</td>
                    <td scope="col">Transaction amount)</td>
                    <td scope="col">Transaction time</td>
                  </tr>';
                  foreach ($data["transaction_list"] as $key => $value) {
                    $middle[$key] = 
                    '<tr><td scope="col">'.$value["transaction_id"].'</td><td scope="col">'.$value['transaction_amount'].'</td><td scope="col">'.$value['transaction_time'].'</td></tr>';
                }
                $bottom = '</table>
                <p>Shipping information is below:</p>
                <table border="0">
                  <tr>
                    <td scope="col">Full Name: </td>
                    <td scope="col">'. $data["name"] .'</td>
                  </tr>
                  <tr>
                    <td scope="col">Mobile number: </td>
                    <td scope="col">'. $data["mobile"] .'</td>
                  </tr><tr>
                    <td scope="col">Address: </td>
                    <td scope="col">'. $data["address"] .'</td>
                  </tr><tr>
                    <td scope="col">Address Type: </td>
                    <td scope="col">'. $data["address_type"] .'</td>
                  </tr><tr>
                    <td scope="col">Email: </td>
                    <td scope="col">'. $data["email"] .'</td>
                  </tr>
                </table>
                <p>Thank you for your order.</p>
            </div>
        </div>';
        $mail->Body = $top.implode('', $middle).$bottom;
    

    Try This maybe that helps you