如果我们的分数大于对手的分数,则显示胜利;如果我们的分数低于他们的分数,则显示失败

时间:2011-05-12 17:19:40

标签: php xml if-statement

我正在使用PHP输出我们斯坦福运动员得分的XML提要,在这个例子中是男子篮球。其中两个属性是球队的得分,'hs'是主场得分而'vs'是得分(例如,俄勒冈州立大学对斯坦福大学的XML属性是:hs =“67”vs =“69”)。 'hn'和'vn'是主页名称和访客名称(例如,'Stanford'或'Oregon State')。 附加的'hc'和'vc'是本地城市和访客城市,并且包含城市名称的前4个字母(例如,斯坦福将是'stan'而''Oregon State'将是'oreg')。注意:因为Cat只有三个字母长,所以Cat的'hc'和'vc'也是'cat'。

我的问题是,如果斯坦福大学的分数大于对手的分数,是否有可能显示“胜利”,或者显示“损失”是斯坦福分数低于对手的分数?例如,俄勒冈州立大学击败了斯坦福大学,那么我怎样才能展示“在俄勒冈州 - 失落”?

以下是我的pageXML和代码:

<?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'];
    // Output "vs" if home game or "at" if away game
    $preposition = ($event_date->event['hc'] == 'stan') ? 'vs' : 'at';
    echo '<li>';
        echo '<h3>', $preposition, ' ', $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>';
    }
}   
?>

2 个答案:

答案 0 :(得分:3)

$home = ($event_date->event['hc'] == 'stan');
echo '<h3>', $preposition, ' ', $name, ' - ', ((int)$event_date->event['hs'] == (int)$event_date->event['vs'] ? "Tie" : (((int)$event_date->event['hs'] > (int)$event_date->event['vs']) ? ($home? 'Win' : 'Loss') : ($home ? 'Loss' : 'Win'))), '</h3>';

将这些放在你的foreach循环中,我还添加了一个“领带”东西。

通过定义$ home变量,您可以替换这些行:

 // Only show opponents' name
 $name = ($event_date->event['vc'] == 'stan') ? $event_date->event['hn'] : $event_date->event['vn'];
// Output "vs" if home game or "at" if away game
$preposition = ($event_date->event['hc'] == 'stan') ? 'vs' : 'at';

用这些:

// Home?
$home = ($event_date->event['hc'] == 'stan');
// Only show opponents' name
$name = $home ? $event_date->event['hn'] : $event_date->event['vn'];
// Output "vs" if home game or "at" if away game
$preposition = $home ? 'vs' : 'at';

你不需要我写的代码中的$ home。

所以这是完整的代码:

<?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']))
    {
    // Home?
    $home = ($event_date->event['hc'] == 'stan');
    // Only show opponents' name
    $name = $home ? $event_date->event['vn'] : $event_date->event['hn'];
    // Output "vs" if home game or "at" if away game
    $preposition = $home ? 'vs' : 'at';
    echo '<li>';
        echo '<h3>', $preposition, ' ', $name, ' - ', ((int)$event_date->event['hs'] == (int)$event_date->event['vs'] ? "Tie" : (((int)$event_date->event['hs'] > (int)$event_date->event['vs']) ? ($home? 'Win' : 'Loss') : ($home ? 'Loss' : 'Win'))), '</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>';
    }
}   
?>

使用“if's”和更多评论扩展代码:

<?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']))
    {
    // Home?
    $home = ($event_date->event['hc'] == 'stan');
    // Only show opponents' name
    $name = $home ? $event_date->event['vn'] : $event_date->event['hn'];
    // Output "vs" if home game or "at" if away game
    $preposition = $home ? 'vs' : 'at';
    if((int)$event_date->event['hs'] == (int)$event_date->event['vs']) // Tie?
    {
    $result = 'Tie'; // Tie
    }
    else if((int)$event_date->event['hs'] > (int)$event_date->event['vs']) // Home team won?
    {
    if($home) // Home?
    {
    $result = 'Win'; // You're home and the home team won
    }
    else
    {
    $result = 'Loss'; // You're away, but the home team won
    }
    }
    else // Home team lost
    {
    if($home) // Home?
    {
    $result = 'Loss'; // You're away, but the home team won
    }
    else
    {
    $result = 'Win'; // You're home and the home team won
    }
    }
    echo '<li>';
        echo '<h3>', $preposition, ' ', $name, ' - ', $result, '</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>';
    }
}   
?>

答案 1 :(得分:0)

假设$event_date->event['hs']主分数$event_date->event['vs']对比分

echo ($event_date->event['hs'] > $event_date->event['vs']) ? 'WIN' : 'LOSS';

或者,如果有平局游戏的潜力:

echo ($event_date->event['hs'] > $event_date->event['vs']) ? 'WIN' : ($event_date->event['hs'] < $event_date->event['vs']) ? 'LOSS' : 'TIE';

注意:此答案的输出全部基于主页。既然你需要它总是参考斯坦福,@ Tyilo的答案是更好的匹配。