Php 使用WikipediaAPI从一系列主题中提取和显示内容

Php 使用WikipediaAPI从一系列主题中提取和显示内容,php,jquery,arrays,ajax,wikipedia-api,Php,Jquery,Arrays,Ajax,Wikipedia Api,我正在使用WikipediaAPI来获取和显示有关主题的信息 我的代码可以很好地用于一个主题: $(document).ready(function(){ $.ajax({ type: "GET", url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=Dementia&callback=?

我正在使用WikipediaAPI来获取和显示有关主题的信息

我的代码可以很好地用于一个主题:

$(document).ready(function(){

    $.ajax({
        type: "GET",
        url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=Dementia&callback=?",
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {

        var markup = data.parse.text["*"];
        var i = $('<div></div>').html(markup);

        // remove links as they will not work
        i.find('a').each(function() { $(this).replaceWith($(this).html()); });

        // remove any references
        i.find('sup').remove();

        // remove cite error
        i.find('.mw-ext-cite-error').remove();

        $('#article').html($(i).find('p'));


        },
        error: function (errorMessage) {
        }
    });    

});

<div id="article"></div>
上面的代码适用于单个主题,但现在我想对其进行修改,使其在主题数组中循环,并使用数组中每个主题的“wikipedia\u page\u url”值确定要拉取哪个页面,然后输出页面上每个主题的内容:

<?php foreach ($resident_conditions as $resident_condition) { ?>

    <?php
    $condition_id = $resident_condition['condition_id'];
    $condition = sw::shared()->conditions->getForID($condition_id);
    $wikipedia_page_url = $condition['wikipedia_page_url'];
    ?>

    <h6><?php echo $condition['condition_name']; ?></h6>
    <div id="<?php echo $condition['condition_name']; ?>">

    <!-- This is where I want to place the content pulled from Wikipedia for each topic -->

    </div>

<?php } ?>


您可以将主题保存在javascript数组中,然后循环浏览它们

$(document).ready(function(){
    var topics = ['Dementia', 'Topic2', 'Topic3'];

    for(var i = 0; i < topics.length; i++) {
      $.ajax({
          type: "GET",
          url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page="+topics[i]+"&callback=?",

       ... the rest of your ajax config
      });  //end of ajax
    } //end of loop
}); //end of .ready();
$(文档).ready(函数(){
变量主题=[‘痴呆’、‘主题2’、‘主题3’];
对于(var i=0;i
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=<?php echo $condition['wikipedia_page_url']; ?>&callback=?",
$(document).ready(function(){
    var topics = ['Dementia', 'Topic2', 'Topic3'];

    for(var i = 0; i < topics.length; i++) {
      $.ajax({
          type: "GET",
          url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page="+topics[i]+"&callback=?",

       ... the rest of your ajax config
      });  //end of ajax
    } //end of loop
}); //end of .ready();