在电子邮件正文中循环内容

时间:2014-11-12 12:38:37

标签: php html sql

我想在电子邮件正文中循环一些内容,但我不知道该怎么做。 :D请帮忙。 :(

<?php
    $db_username = 'ads';
    $db_password = 'sad';
    $db_name = 'asdasd';
    $db_host = 'asd.com';
    $mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
?>

$results = $mysqli->query("SELECT * FROM products ORDER BY id ASC");
foreach( mysql_fetch_array($results) as $item ) {
    $message = '<html>
                        <head>
                        <title>From our company</title>
                        </head>

                        <body>

                            <div id="newsletter-product" style="border:1px solid gray; overflow:hidden; width:500px; margin:0 auto; text-align:center;">  

                                //I WANT TO LOOP THIS DIV//
                                <div id="newsletter-organic" style="float:left; width:50%;">
                                  <h1> Organik </h1>
                                    <p><img src="http:mywebsite/images/'.$item['product_img_name'].'" width:150px; height:200px;"></p>
                                      <a href="http://mywebsite.com/get-referal.php?referal_identity='.$referal_identity.'&referal_address='.$referal_address.'&productName='.$item['product_name'].'">
                                        <p>Beras Putih</p></a><br/>
                                </div>                    

                            </div>

                        </body>
                        </html>';
}

我要在那里循环产品。

感谢您帮助我。 :)

4 个答案:

答案 0 :(得分:2)

试试这个

<?php
        $db_username = 'ads';
        $db_password = 'sad';
        $db_name = 'asdasd';
        $db_host = 'asd.com';
        $con =  mysqli_connect($db_host, $db_username, $db_password,$db_name);


        $results = mysqli_query($con,"SELECT * FROM products ORDER BY id ASC");

         $message='';
         while( $item=mysqli_fetch_array($con,$results)  ) {
                    $message. = '<html>
                                <head>
                                <title>From our company</title>
                                </head>

                                <body>

                                    <div id="newsletter-product" style="border:1px solid gray; overflow:hidden; width:500px; margin:0 auto; text-align:center;">  

                                        //I WANT TO LOOP THIS DIV//
                                        <div id="newsletter-organic" style="float:left; width:50%;">
                                          <h1> Organik </h1>
                                            <p><img src="http:mywebsite/images/'.$item['product_img_name'].'" width:150px; height:200px;"></p>
                                              <a href="http://mywebsite.com/get-referal.php?referal_identity='.$referal_identity.'&referal_address='.$referal_address.'&productName='.$item['product_name'].'">
                                                <p>Beras Putih</p></a><br/>
                                        </div>                    

                                    </div>

                                </body>
                                </html>';
                  }

答案 1 :(得分:2)

You can try this, in the code there are only "newsletter-organic" d

<?php
        $db_username = 'ads';
        $db_password = 'sad';
        $db_name = 'asdasd';
        $db_host = 'asd.com';
        $mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
        ?>

        $results = $mysqli->query("SELECT * FROM products ORDER BY id ASC");
        $message = '<html>
                        <head>
                        <title>From our company</title>
                        </head>

                        <body>

                            <div id="newsletter-product" style="border:1px solid gray; overflow:hidden; width:500px; margin:0 auto; text-align:center;">';
    foreach( mysql_fetch_array($results) as $item ) {

                                //I WANT TO LOOP THIS DIV//
                                $message .= '<div id="newsletter-organic" style="float:left; width:50%;">
                                  <h1> Organik </h1>
                                    <p><img src="http:mywebsite/images/'.$item['product_img_name'].'" width:150px; height:200px;"></p>
                                      <a href="http://mywebsite.com/get-referal.php?referal_identity='.$referal_identity.'&referal_address='.$referal_address.'&productName='.$item['product_name'].'">
                                        <p>Beras Putih</p></a><br/>
                                </div>';                    

          }

          $message .= '</div>
                    </body>
                 </html>';

答案 2 :(得分:1)

$results = $mysqli->query("SELECT * FROM products ORDER BY id ASC");
            foreach( mysql_fetch_array($results) as $item ) {
            $message = '<html>
                        <head>
                        <title>From our company</title>
                        </head>

                        <body>

                            <div id="newsletter-product" style="border:1px solid gray; overflow:hidden; width:500px; margin:0 auto; text-align:center;">';

                            for ($i = 1; $i <= 10; $i++) {
                                //I WANT TO LOOP THIS DIV// this div will loop 10 times...
                                $message .= '<div id="newsletter-organic" style="float:left; width:50%;">
                                  <h1> Organik </h1>
                                    <p><img src="http:mywebsite/images/'.$item['product_img_name'].'" width:150px; height:200px;"></p>
                                      <a href="http://mywebsite.com/get-referal.php?referal_identity='.$referal_identity.'&referal_address='.$referal_address.'&productName='.$item['product_name'].'">
                                        <p>Beras Putih</p></a><br/>
                                </div>';  
                              }             

                    $message .= '         </div>

                        </body>
                        </html>';
          }

是的,正如Ram Sharma写的那样,你正在使用对象$mysqli,然后使用mysql函数mysql_fetch_array ......

答案 3 :(得分:1)

您有多个错误

请勿混用mysqlimysql

您使用mysql_fetch_array($results) as $item代替mysqli

也将$message =更改为$message.=