简体   繁体   English

如何在php数组上使用json_encode(),以及如何使用jQuery.getJSON()?

[英]how to use json_encode() on a php array, and also how to use jQuery.getJSON()?

http://api.jquery.com/jQuery.getJSON/ http://api.jquery.com/jQuery.getJSON/

http://www.w3schools.com/jquery/ajax_getjson.asp http://www.w3schools.com/jquery/ajax_getjson.asp

http://api.jquery.com/jQuery.parseJSON/ http://api.jquery.com/jQuery.parseJSON/

http://www.json.org/js.html http://www.json.org/js.html

http://php.net/manual/en/function.json-encode.php http://php.net/manual/zh/function.json-encode.php

http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/ http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/

Those are the sites I've been researching the most trying to wrap my head around json 这些是我一直在研究的网站,试图将我的头放在json上

This is my php 这是我的PHP

$fvsc=array();
query_posts('meta_key=featuredv');  
if (have_posts()) :  
    while (have_posts()) : 
        the_post();
        $fvsc []= "\n" . '<div id="featuredv-' . "$wp_query->current_post\">" . "$post->post_content;" . '</div>';
    endwhile;  
endif;
$jsonObject= json_encode($fvsc);   

and this is my .js 这是我的.js

jQuery(document).ready(function($){
    jQuery.getJSON("<?php TEMPLATEPATH . '/featuredv.php'?>",function(data){
        var fvsJA=jQuery.parseJSON(<?php $jsonObject; ?>);
        var fts = $("#ttContent>[id^=featuredt]");  
        var fvs = $("#inCenter>[id^=featuredv]");
    });
    fts.click( function(){   
        $("#inCenter>[id^=featuredv]").replaceWith( function(){
            $(fvsJA).eq(fts.index(this))
        });
    });

What I'm trying to do is make my $fvsc array into a jQuery array. 我想做的是将$ fvsc数组变成jQuery数组。 $fvsc is an array of videos, when a fts is clicked it should take the contents of a div and replace it with another video that has the same index as whichever fts was clicked. $ fvsc是一个视频数组,单击fts时,应将div的内容替换为具有与单击fts相同的索引的另一个视频。 If you just load all the fts and fvs into one place you can use the click function here and it works great but if you echo out 5 videos and then later 10 and so on the site loads worse and worse. 如果仅将所有fts和fvs加载到一个位置,则可以在此处使用click功能,效果很好,但是如果您回显5个视频,然后回显10个视频,则该站点的加载情况越来越糟。 I thought if I could convert my php array into a jquery object that loads json strings then on click converts back into an object and replaces the div contents the load would degrade a lot better. 我以为如果可以将php数组转换为可加载json字符串的jquery对象,则在单击时将其转换回对象并替换div内容,负载会更好。

I see several issues in your code: 我在您的代码中看到了几个问题:

  1. $fvsc []= "\\n" . '<div id="featuredv-' . "$wp_query->current_post\\">" . "$post->post_content;" . '</div>';

    I don't think you can wrap class's variable in double quote. 我认为您不能将类的变量用双引号引起来。 It should be something like this: 应该是这样的:

     $fvsc []= '<div id="featuredv-' . $wp_query->current_post . '">' . $post->post_content . '</div>'; 
  2. There's issue in your JS, specifically, the way you access URL. JS中存在问题,特别是访问URL的方式。 You see, ajax in wordpress is a bit different than the regular php. 您会发现,wordpress中的ajax与常规php有所不同。 Look here for more information: https://wordpress.stackexchange.com/questions/9231/whats-the-preferred-method-of-writing-ajax-enabled-plugins/9236#9236 在此处查找更多信息: https : //wordpress.stackexchange.com/questions/9231/whats-the-preferred-method-of-writing-ajax-enabled-plugins/9236#9236

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM