确定我使用这个foreach
循环来解析xml
以将结果数据添加到我的数据库中我需要设置$win
值但它总是返回3时它不应该... .. < / p>
$myinfo = simplexml_load_file('http://mysite/results.xml');
foreach ($myinfo->Event as $info):
$cid=$info['EventID'];
$a=$info->Match['AScoreNT'];
$b=$info->Match['BScoreNT'];
if($a > $b){
$win =1;
} elseif($a < $b){
$win =2;
} else{
$win =3;
}
$sql = "
INSERT IGNORE INTO `engine4_event_wins`
SET `win_result` = $win,
`comp_id` = $cid;
";
mysql_query($sql);
endforeach;
<MyFeed>
<Event Sport="AFL" Description="AFL - Finals - Week 1" EventID="651239" FinalDate="2012-09-07T00:00:00">
<Match TeamA="Hawthorn" TeamB="Collingwood" AScoreNT="135" BScoreNT="97"/>
</Event>
<Event Sport="AFL" Description="AFL - Finals - Week 1" EventID="651240" FinalDate="2012-09-08T00:00:00">
<Match TeamA="Adelaide Crows" TeamB="Sydney Swans" AScoreNT="42" BScoreNT="71"/>
</Event>
</MyFeed>
答案 0 :(得分:0)
echo
$a
和$b
的值,看看为什么它总是等于:)
你可以做的另一件事是:
foreach ($myinfo->Event as $info):
$a=$info->Match['AScoreNT'];
$b=$info->Match['BScoreNT'];
if($a === $b){
$win =3;
elseif($a > $b){
$win =1;
} elseif($a < $b){
$win =2;
} else{
$win =4;
}
echo $win;
endforeach;
我的猜测是它会打印4
...
<强>更新强>
我的怀疑是正确的,你是在比较字符串而不是整数,这就是你得到equal
结果的原因(双方都不是空字符串,因此,根据PHP - 它们是相似的...我知道,它不是直观,我推荐reading about it!)。
要修复它,请将代码更改为:
if((int)$a > (int)$b){
$win =1;
} elseif((int)$a < (int)$b){
$win =2;
} else{
$win =3;
}