Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TinyMCE中的PHP foreach_Php_Html_Html Email - Fatal编程技术网

TinyMCE中的PHP foreach

TinyMCE中的PHP foreach,php,html,html-email,Php,Html,Html Email,我有一个从db加载的电子邮件(html)模板,其中包含foreach循环(或者可能是其他一些PHP操作),用于动态加载/处理API响应的内容 $content = html_entity_decode($template); //$template loaded from db $content的html <p style="text-align:center">Hi {name},</p> <p style="text-align:

我有一个从db加载的电子邮件(html)模板,其中包含
foreach
循环(或者可能是其他一些PHP操作),用于动态加载/处理API响应的内容

$content = html_entity_decode($template); //$template loaded from db
$content的html

<p style="text-align:center">Hi {name},</p>
<p style="text-align:center">This is header</p>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($response as $item){ ?>
        <tr>
            <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
在发送电子邮件之前,我使用
str_replace({name},$name,$content)替换令牌
,但我不知道如何执行$content的
foreach
(或者可能是其他一些PHP操作)来填充$response的所有动态数据,然后发送电子邮件

我的尝试

我也尝试过在$response中使用token,但是,如果我需要多个$template设计和多个API响应(用于其他用途的电子邮件),那么这种方法就不好了。例如,在这个模板中,我有产品表,但在其他模板中,我需要内联
,内联
样式
来列出一些功能等

请告诉我如何做到这一点


PS:我在前端使用TinyMCE设计电子邮件模板并存储在DB中。

据我所知,您希望在电子邮件模板中添加更多信息/数据


解决方案#1 假设“$content”包含与您希望包含在模板中的消息相同的消息,您可以执行以下操作:

<?php
$content = " Thank You!!!";   // some data
foreach($response as $item){ ?>
        <tr>
            <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
            <td><?php echo $content; ?></td>  // just add the variable.
        </tr>
        <?php } ?>
有关多个阵列上的foreach循环的更多详细信息,请参阅文章: 希望对我有所帮助我找到了解决办法

简而言之,我需要使用输出缓冲例如ob_start()

解释

步骤1:我将模板存储在
.html
文件中

步骤2:API响应后,放入
ob_start()

步骤3:使用
Include('path/to/file.html')

步骤4:获取缓冲区的输出并作为电子邮件发送

我的代码

//Consider HTML template in question is saved as template.html

//Response from API 
$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

ob_start();
include('path/to/template.html');
$message = ob_get_clearn();

//now use $message in sending email
$this->emnail->message($message);
ob_start()
将打开缓冲区并保存HTML的输出,在其中执行PHP,然后
ob_get_clean()
将输出返回到$message并清理缓冲区

效益


通过这种方法,我可以在
sendmail()
中使用任何html模板。我只需要设计电子邮件模板,其余的事情将由
输出缓冲区

完成,你昨天问了这个问题,但他们你包括了一些更多的信息,你能再添加一次吗
foreach (array_combine($courses, $sections) as $course => $section)
//Consider HTML template in question is saved as template.html

//Response from API 
$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

ob_start();
include('path/to/template.html');
$message = ob_get_clearn();

//now use $message in sending email
$this->emnail->message($message);