2017-05-24 63 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