体育比分 XML:如果离开,则回显“at”;如果在家,则回显“vs”
我使用 PHP 输出 XML 体育比赛成绩,在本例中是男子篮球。该页面当前显示对手的姓名,后面是比分和日期。还有主队和客队的 XML 属性(例如,斯坦福大学的俄勒冈州立大学是:hc="stan" vc="orst")。
我的问题是,如果俄勒冈州立大学是客队,是否可以显示“对俄勒冈州立大学”,或者如果俄勒冈州立大学是主队,则可以显示“在俄勒冈州立大学”,而不是只显示对手的名字,即“俄勒冈州立大学” 。
<?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>';
}
}
?>
I'm using PHP to output our XML sports scores, in this example men's basketball. The page currently displays our opponents' names followed by the score and date. There are also XML attributes for home team and away team (for example, Oregon State at Stanford is: hc="stan" vc="orst").
My question is, instead of displaying only the opponents' name, i.e. "Oregon State", is it possible to display "vs Oregon State" if Oregon State is the away team OR display "at Oregon State" if Oregon State is the home team.
Here is my page, XML and code:
<?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>';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$介词 = ($event_date->event['vc'] == 'stan') ? 'vs' : '在';
然后在团队名称前使用
$preposition
。$preposition = ($event_date->event['vc'] == 'stan') ? 'vs' : 'at';
and then use
$preposition
before the team name.也许是这样的?
或者
Something like this maybe?
or