dongtui4038 2019-06-20 10:32
浏览 75
已采纳

PHP迭代cookie数组

In my cakephp application I am trying to create a commenting functionality. So I use an ajax request to get the comments and then clone an html template and append it to the page:

//load comments
    $(document).ready(function(){
        $.ajax({
                type: 'POST',
                url: '/app/tasks/getComments',
                datatype: 'json',
                beforeSend: function (xhr) { // Add this line
                           xhr.setRequestHeader('X-CSRF-Token', $('[name="_csrfToken"]').val());
                        },
                data: {
                        task_id : "<?php echo $task->id; ?>"
                },
                success: function( data )
                {
                    for (var i=0; i < data.length; i++)
                    {
                        //Clone the template
                        var item = $(template).clone();

                        document.cookie = 'photos['+i+']='+data[i].user.photo;
                        document.cookie = 'names['+i+']='+data[i].user.username;

                        //Find the  element
                        $(item).find('#comment_photo').html('<?php if(isset($_COOKIE["photos[$num]"])) {echo $this->Html->image($_COOKIE["photos[$num]"], ["class" => "avatar avatar-online", "alt" => $currentUser->username]); }  ?>');


                        $(item).find('#comment_username').html('<?php if(isset($_COOKIE["names[$num]"])) { echo $_COOKIE["names[$num]"]; }?>');

                        $(item).find('#comment_time').html(moment(data[i].created_date, 'DD-MM-YYYY hh:mm:ss').format('MMMM Do YYYY, h:mm:ss a'));

                        $(item).find('#comment_text').html(data[i].comment);
                        $(item).find('#comment_id').html(data[i].id);
                        //Append to the source
                        $('#target').append(item);


                    }

                }
            });
    });

In order to access the returned results in php I store them in cookie arrays (photos[], names[]). How I can set and update the value of $num in order to iterate the arrays in every html code append? Is it possible to achieve what I want with this approach? Or I need a complete new one?

  • 写回答

1条回答 默认 最新

  • doumu1873 2019-06-24 08:35
    关注

    The solution was much simpler than I actually thought... I did it with JQuery like this:

    .......
    
    $(item).find('#comment_photo').prepend("<img src='/app/img/' class='avatar avatar-online'/>");
    $(item).find('.avatar.avatar-online').prop('src', '/app/img/'+data[i].user.photo);
    ......
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭