我已经设置好站点以使用gitter弹出系统。 javascript看起来像这样:

$(document).ready(function(){
    setTimeout(function() {
        $.gritter.add({
            title: 'Welcome!',
            text: 'This appears to be your first time playing. Blablabla',
            time: '25000'
        });
    }, 500);
    setTimeout(function() {
        $.gritter.add({
            title: 'Walkthrough',
            text: 'A walkthrough was setup ...',
            time: ''
        });
    }, 30000);
    setTimeout(function() {
        $.gritter.add({
            title: 'You got mail',
            text: 'You have received a message from x ',
            time: ''
        });
    }, 36000);


这是3个弹出窗口的示例。我想做的是有这样的数组:

    $popup_title[0] = "Welcome!";
    $popup_text[0] = "This appears to be ...");
    $popup_time[0] = 25000;

        $popup_title[1] = "Walkthrough";
        $popup_text[1] = "A basic blabla ...");
        $popup_time[1] = "";


并遍历此数组(如果它们不为空),并根据该数组构造javascript。

最佳答案

这样的事情应该做的工作

<?php
$popups = array();
foreach ( $popup_title as $key => $title )
{
    $popups[$key] = new stdClass();
    $popups[$key]->title = $title;
}
foreach ( $popup_text as $key => $text )
{
    $popups[$key]->text = $text;
}
foreach ( $popup_time as $key => $time )
{
    $popups[$key]->time = $time;
}
?>
<script type="text/javascript">
$(document).ready(function(){
    <?php foreach ( $popups as $popup ): ?>
    setTimeout(function() {
        $.gritter.add(<?php echo json_encode($popup);?>);
    }
    <?php endforeach; ?>
}
</script>

07-24 09:50
查看更多