从PHP到jQuery,在数组中存储多个具有相同名称的变量并使用.each

时间:2016-07-23 12:01:06

标签: php jquery arrays

我有一个PHP文件将数据发送到外部jQuery,它是一个dropzone,上传多个图像(每个图像内部都有不同的信息)。我已经构建了一个 foreach PHP循环,它生成数组( foreach image ),每个都应该在jQuery中执行相同的功能(删除,更新等)。 / p>

如何从PHP发送和处理循环到jQuery?

我正在尝试使用.each方法,但我想我错了,因为生成的循环显示重复数组的相同图像(具有相同的信息)!

我的PHP

<?php
        $args = array(
            'order'             => 'ASC',
            'orderby'           => 'post_date',
            'post_type'         => 'attachment',
            'author'            => $current_user->ID,
            'meta_key'          => 'is_portfolio',
            'meta_value'        => '1',
            'numberposts'       => -1,
        );
        $attachments = get_posts($args);
        if ($attachments) 
            {
               foreach ($attachments as $attachment) 
                {
                    $url = $attachment->guid;
                    $imggg = $attachment->post_mime_type; 
                    $url = wp_get_attachment_url($attachment->ID); 
                    $thumb = wp_get_attachment_image_src($attachment->ID, 'thumbportfolio');
                    ?>
                    //here is my jQuery array generated for X number of attachments uploaded
                    var mockFile = { 
                        name: "<?php echo $attachment->post_title ?>", 
                        size: 12345, 
                        serverId: '<?php echo $attachment->ID ?>',
                        post_title : "<?php echo $attachment->post_title ?>",
                        post_desc : "<?php echo $attachment->post_content ?>",
                        thumb : "<?php echo $thumb[0] ?>"
                    };
                    <?php           
                }
            } ?>

我的jQuery .each的{​​{1}}(应该使用相对信息和其他内容启动Dropzone Thumb)。

mockFile

结果呢?重复了相同的图像。

你能帮助我吗?

2 个答案:

答案 0 :(得分:2)

您需要逐渐构建mockFile JS变量。目前你的循环可能会将多个名为mockFile的变量回显到输出页面(在加载页面时尝试查看源代码)。与任何程序一样,如果您多次重新声明变量,则只会使用最后一个版本。 请记住,PHP只是构建一个输出字符串以发送到浏览器并解释为HTML / Javascript。它们之间没有内在的交互 - PHP只是一种动态调整输出到浏览器的内容的机制。浏览器不知道(或关心)内容是来自静态.html文件还是由PHP脚本拼凑在一起。

无论如何,尝试这样的事情:

    $attachments = get_posts($args);
    //next, we declare a JS array "mockFile" to be output to the browser
    ?>
    var mockFile = [
    <?php
    if ($attachments) 
    {
           $count = 0;
           foreach ($attachments as $attachment) 
           {
                $url = $attachment->guid;
                $imggg = $attachment->post_mime_type; 
                $url = wp_get_attachment_url($attachment->ID); 
                $thumb = wp_get_attachment_image_src($attachment->ID, 'thumbportfolio');
                if ($count > 0) echo "," //put comma in front of previous array entry, if there is one
                ?>
                //here is my jQuery array generated for X number of attachments uploaded
                { 
                    name: "<?php echo $attachment->post_title ?>", 
                    size: 12345, 
                    serverId: '<?php echo $attachment->ID ?>',
                    post_title : "<?php echo $attachment->post_title ?>",
                    post_desc : "<?php echo $attachment->post_content ?>",
                    thumb : "<?php echo $thumb[0] ?>"
                }
                <?php
                $count++;
           }
    } ?>
    ];
    <?php
    //just above this comment is the end of the JS array. Even if there are no elements, the array will still be declared and be valid;
    ?>

答案 1 :(得分:0)

感谢ADyson的宝贵帮助,我会尝试他的解决方案。我实际上后来在搜索和研究一些jQuery文档时解决了。假设我有两个互相交互的文件:

  • main.php
  • myscript.js

第一个可以通过对象发送具有相同名称的多个变量(基本上这是我的问题)。所以我在循环之前声明了array,并为每个.push使用$attachment来存储该地方的所有内容,如(main.php):

var myarray = [];
<?php
        $args = array(
            'order'             => 'ASC',
            'orderby'           => 'post_date',
            'post_type'         => 'attachment',
            'author'            => $current_user->ID,
            'meta_key'          => 'is_portfolio',
            'meta_value'        => '1',
            'numberposts'       => -1,
        );
        $attachments = get_posts($args);
        if ($attachments) 
            {
               foreach ($attachments as $attachment) 
                {
                    $url = $attachment->guid;
                    $imggg = $attachment->post_mime_type; 
                    $url = wp_get_attachment_url($attachment->ID); 
                    $thumb = wp_get_attachment_image_src($attachment->ID, 'thumbportfolio');
                    ?>
                    var mockFile = { 
                        name: "<?php echo $attachment->post_title ?>", 
                        size: 12345, 
                        serverId: '<?php echo $attachment->ID ?>',
                        post_title : "<?php echo $attachment->post_title ?>",
                        post_desc : "<?php echo $attachment->post_content ?>",
                        thumb : "<?php echo $thumb[0] ?>"
                    };
                    myarray.push(mockFile);
                    <?php           
                }
            } ?>

Console.log(myrray)显示多维数组,如

[{attachment1},{attachment2},{attachment3}]

所以我创建了我的.each jQuery函数,如(myscript.js)

$.each( myarray, function( i, el ){
        //blablabla accessing keys and values.
    });

再一次,我不知道这是否是实现它的最佳方式,但工作正常,我的页面加载速度比以前更快。也许我应该更多地了解json enconding交换信息,但现在我为自己感到骄傲。