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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配