开发者

Display Win if our score is greater than opponent's score or Loss if our score is less than theirs

I'm using PHP to output an XML feed of our Stanford Athletics scores, in this example men's basketball. Two of the attributes are both teams' scores, 'hs' is home score and 'vs' is visitor score (for example, Oregon State vs Stanford XML attributes are: hs="67" vs="69"). 'hn' and 'vn' are the home name and the visitor name (for example, 'Stanford' or 'Oregon State'). Additional 'hc' and 'vc' are the home city and the visitor city,开发者_如何学Python and contains the first 4 letters of the city name (for example, Stanford would be 'stan' and 'Oregon State' would be 'oreg'). Note: as Cat only is three letters long, so is the 'hc' and 'vc' for Cat being 'cat'.

My question is, is it possible to display "Win" if the Stanford score is greater than the opponent's score, or display "Loss" is the Stanford score is less than the opponent's score? For example, Oregon State beat Stanford, so how can I display "at Oregon State - Loss"?

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'];
    // 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>';
    }
}   
?>


$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>';

Put these in your foreach loop, and i've also added a "Tie" thingy.

By defining the $home variable you could replace these lines:

 // 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';

with these:

// 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';

and you wouldn't need the $home in the code i wrote.

So here is the complete 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']))
    {
    // 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>';
    }
}   
?>

Expanded code with "if's" and more comments:

<?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>';
    }
}   
?>


Assuming $event_date->event['hs'] is the Home Score and $event_date->event['vs'] is the Versus Score:

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

Or, if there's the potential of a tie game:

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

Note: The output of this answer is all based on the Home Team. Since you need it to always be in reference to Stanford, @Tyilo's answer is a better match.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜