2016-06-29 62 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();