0
我是新來的PHP,我試圖從XML文件中獲取數據並相應地顯示它。 現在,我成功地提取數據並顯示它。
下面是代碼 -如何顯示默認的分區,當要顯示的內容不存在
if($xml_response = @simplexml_load_file('http://www.test.com/Testapp/TestSite?code=Test'))
{
foreach ($xml_response->promotions as $promotions) {
//print_r($promotions);
foreach ($promotions->promotion as $promotion) {
if(isset($promotion)){
$text=$promotion->text;
if (strlen($text) > 80) {
$stringCut = substr($text, 0, 80);
$text = substr($stringCut, 0, strrpos($stringCut, ' ')).' ... ';
}
?>
<div class="ads" style="margin-right:0px;">
<a href="<?=$promotion->link;?>" title="" target="_blank">
<h2><?php echo $promotion->title;?></h2>
<img src="<?php echo $promotion->image;?>" align="left" width="100px" height="80px">
<h2 style="vertical-align:text-top"><?php echo $text ?></h2></a>
</div>
<?php }
else
{?>
<div class="ads" style="margin-right:0px;">
<h2>Advertisement Title 1</h2>
<img src="images/side-banner.jpg" align="left" width="100px" height="80px">
<h2 style="vertical-align:text-top">We improve your way of working and communicating with your groups.</h2>
</div>
<?php } }
現在,我能夠顯示圖像時的內容是存在的,但是當有應顯示任何內容默認的內容。所以我檢查了isset
條件,仍然無法顯示默認圖像。 下面是XML -
<promotions>
<promotion>
<title>Test</title>
<text>Sign off for limited time offer</text>
<link>NewCentrescdr.jpg</link>
<image>test.jpg</image>
<date>2013-07-16</date>
</promotion>
<promotion>
<title>Test</title>
<text>Test text</text>
<link>http://www.test.com</link>
<image>test.jpg</image>
<date>2013-09-28</date>
</promotion>
</promotions>
現在的基礎上,促進XML的號碼,我想在頁面中顯示only three
。如果沒有促銷,則顯示默認促銷。在上面的XML中只有2個促銷,所以第三個應該是一個默認的。我怎樣才能做到這一點