2017-05-24 20 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>` 

私はこれを解決するために多くの方法で試してみましたが、私はまだ十分なknowlageを持っていません。本当にあなたが私を助けてくれることを願っています。前もって感謝します。

答えて

0

ただ解決しました! 私は近くにあった;) これはrss.xmlファイルです:私は、選択ボックスのオプションとしてXMLからタイトルをつけdynamiclyたい

<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

です。そして、同じページの説明を表示するために選択したタイトルよりも。どのようにそれを行うにはどのようなアイデア? – Mesecarkv

関連する問題