2017-05-24 64 views
1

我想動態地從xml中選擇標題作爲選項。而不是通過選擇標題在同一頁面上顯示說明。任何想法如何做到這一點?如何設置選項用php選擇框填充xml?

這是xml文件

<rss> 
    <channel> 
      <item> 
       <title>ananas</title> 
       <id>1</id> 
       <class></class> 
       <description>maskara</description> 

     </item> 
     <item> 
      <title>maskara</title> 
       <id>2</id> 
      <class></class> 
      <description>masakr</description> 

     </item> 
</channel> 

一些代碼,這是PHP文件,但我不知道將其設置爲選擇,請幫助。

<?php 
$xmlTekst = file_get_contents("rss.xml"); 
    $xml = new SimpleXMLElement($xmlTekst); 
     ?> 
    <select> 
<option value='-1'>Select News</option> 
    <?php 
    foreach($xml->channel->item as $vest){ 
    echo "<option " . " value='{$vest->id}'>{$vest->title}</option>"; 

    } 

?> 
    </select>` 

我試過很多方法來解決這個問題,但是我還沒有足夠的知識。真的希望你能幫助我。提前致謝。

回答

0

剛解決了!!! 我很接近;) 這是一個rss.xml文件:

<rss> 
    <channel> 
      <item> 
       <title>Egypt Christians killed in bus attack</title> 
       <id>1</id> 
       <description>The Copts killed on Friday had been travelling to St Samuel's monastery to pray. 
Their bus was in a small convoy that was stopped on a desert road near Adwa police station on the border between Minya and Beni Suef provinces. 
Between eight and 10 gunmen wearing military uniforms attacked the convoy, officials cited witnesses as saying. The gunmen then fired at the vehicles with automatic weapons before fleeing in three 4x4 vehicles, they added.</description> 

     </item> 
     <item> 
      <title>G7 summit agrees on countering terrorism but not climate change</title> 
      <id>2</id> 
      <description>Leaders of the world's leading industrial nations, the G7, have agreed on new action to counter terrorism at a summit in Taormina, Sicily. 
However, differences over climate change remain between US President Donald Trump and the others, said the host, Italian PM Paolo Gentiloni. 
Mr Trump and UK Prime Minister Theresa May reaffirmed plans to boost trade, including a post-Brexit trade deal. 
Mr Trump has welcomed the UK's vote to leave the European Union. 
They are both attending their first G7 summit, as are Mr Gentiloni and French President Emmanuel Macron. 
The G7 consists of Canada, France, Germany, Italy, Japan, the UK and the US, while the European Union (EU) also has representatives present.</description> 

     </item> 
     <item> 
      <title>Serbia country profile</title> 
      <id>3</id> 
      <description>The end of the Union of Serbia and Montenegro marked the closing chapter in the history of the separation of the six republics of the old Socialist Republic of Yugoslavia which was proclaimed in 1945 and comprised Serbia, Montenegro, Slovenia, Croatia, Bosnia-Herzegovina and Macedonia. 
Under Yugoslavia's authoritarian communist leader, Josip Broz Tito, the lid was kept on ethnic tensions. The federation lasted for over 10 years after his death in 1980, but under Serbian nationalist leader Slobodan Milosevic it fell apart through the 1990s. 
The secession of Slovenia and Macedonia came relatively peacefully, but there were devastating wars in Croatia and Bosnia. Serbia and Montenegro together formed the Federal Republic of Yugoslavia between 1992 and 2003.</description> 

     </item> 
    </channel> 
</rss> 

這是rss.php文件

<?php 

$selected_id=0; 


?> 
<h3>News Title</h3> 
    <select onchange="window.location='?nid='+this.value" name = "selNews"> 
<option value='-1'>Select News</option> 
    <?php 
     include("rss.xml"); 
$xml = simplexml_load_file("rss.xml"); 

     foreach ($xml->channel->item as $vest) { 
    echo "<option " . (($_GET['nid']) == $vest->id?'selected':'') . " value='{$vest->id}'>{$vest->title}</option>"; 


} 
?> 
</select> 
<br /><br /> 
<div> 
<h3>Chosen Tittle</h3> 
<br /><br /> 

    <?php foreach ($xml->channel->item as $vest) { 
    if(($_GET['nid']) == $vest->id){ 
     echo $vest->description; 
    } 
} 
    ?> 
    </div> 
0
<?php 
$xmlTekst = <<<EODE 
    <rss> 
    <channel> 
      <item> 
       <title>ananas</title> 
       <id>1</id> 
       <class></class> 
       <description>maskara</description> 

     </item> 
     <item> 
      <title>maskara</title> 
       <id>2</id> 
      <class></class> 
      <description>masakr</description> 

     </item> 
    </channel> 
</rss> 
EODE; 
?> 

另存爲rss.php。

file.php:

<?php 
include "rss.php"; 
$xml = new SimpleXMLElement($xmlTekst); 


    foreach ($xml->children() as $vest) { 
     $var = ((string)$vest->item->title); 
     $var2 = ((string)$vest->item->description); 

    } 


?> 


<select name="select"> 
    <option><?php echo $var; ?></option> 
    <option><?php echo $var2; ?></option> 
</select> 

你能解釋一下究竟是什麼意思?

+0

我要動態地把冠軍從XML作爲在選擇框中的選項。而不是通過選擇標題在同一頁面上顯示說明。任何想法如何做到這一點? – Mesecarkv