2016-06-29 61 views
1

我正在使用维基百科API来提取和显示有关主题的信息。使用维基百科API从主题数组中提取并显示内容

我有单个主题细作品的代码:

$(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> 

在上面的代码中,题目是“痴呆”由图所示:

&page=Dementia 

上面的代码正常工作要SINGLE主题,但现在我想修改它以遍历主题的ARRAY,并使用数组中每个主题的“wikipedia_page_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 } ?> 

每个主题的“wikipedia_page_url”值确定页面从维基百科拉,下面的代码所示:

如何修改上面的JS脚本工作拉并显示每个话题的内容?我知道我需要替换每个“wikipedia_page_url”的剧本里这样的值:

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(){ 
 

 
     $.ajax({ 
 
      type: "GET", 
 
      url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=Tourette_syndrome&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) { 
 
      } 
 
     });  
 
    
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="article"></div>

回答

1

您可以通过将它们保存主题在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();