我正在使用PHP输出我们的XML体育比分,在这个例子中是男子篮球。该页面目前显示我们对手的名字,然后是分数和日期。主队和客队也有XML属性(例如,斯坦福大学的俄勒冈州立大学是:hc =“stan”vc =“orst”)。
我的问题是,如果俄勒冈州是客场球队,或者如果俄勒冈州是俄勒冈州立大学,则可以显示“vs俄勒冈州”,而不是仅显示对手的名字,即“俄勒冈州立大学”。主队。
<?php
$xml = simplexml_load_file('http://www.gostanford.com/data/xml/events/m-baskbl/2010/index.xml');
// Reverse chronological order
$xmlArray = array();
foreach ($xml as $event_date) $xmlArray[] = $event_date;
$xmlArray = array_reverse($xmlArray);
foreach ($xmlArray as $event_date)
{
// Only output if complete
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
// Only show opponents' name
$name = ($event_date->event['vc'] == 'stan') ? $event_date->event['hn'] : $event_date->event['vn'];
echo '<li>';
echo '<h3>', $name, '</h3>';
echo '<p><strong>', $event_date->event['vn'], ' ', $event_date->event['vs'], ' - ', $event_date->event['hn'], ' ', $event_date->event['hs'], '</strong></p>';
echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
echo '</li>';
}
}
?>
答案 0 :(得分:1)
$ preposition =($ event_date-&gt; event ['vc'] =='stan')? 'vs':'at';
然后在团队名称前使用$preposition
。
答案 1 :(得分:0)
这样的事可能吗?
if($name == $event_date->event['vs']) {
// vs
} else {
// at
}
或
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
// Only show opponents' name
$name = ($event_date->event['vc'] == 'stan') ? $event_date->event['hn'] : $event_date->event['vn'];
$playing_location = 'at'; // default
if($name == $event_date->event['vs']) {
$playing_location = 'vs';
}
echo '<li>';
echo '<h3>', $name, '</h3>';
echo '<p><strong>', $event_date->event['vn'], ' ', $event_date->event['vs'], $playing_location, $event_date->event['hn'], ' ', $event_date->event['hs'], '</strong></p>';
echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
echo '</li>';
}