[英]PHP if condition and foreach inside
I don't understand why my if-condition is not working. 我不明白为什么我的if条件不起作用。
From this html page: 从此html页面:
http://www.olympic.org/olympic-results/london-2012/archery http://www.olympic.org/olympic-results/london-2012/archery
I'm trying to extract information like Olympic edition, trial name etc. I extract the men's event with this code: 我正在尝试提取奥林匹克版本,审判名称等信息。我使用以下代码提取男子比赛:
$html=file_get_html('http://www.olympic.org/olympic-results/london-2012/archery');
foreach($html->find('div') as $element) {
if ($element->id == 'gamesSelect')
{
$Game = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->id == 'sportSelect')
{
$Sport = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->class == 'man')
{
foreach($html->find('span.trial_name') as $e_trial)
{
$Event = $e_trial->innertext . '<br>';
}
}
}
Inside the last if condition I put a foreach loop in order to extract only this text: 在最后一个if条件中,我放置了一个foreach循环以仅提取以下文本:
Men's Individual (FITA Olympic Round - 70m) Men's Team (FITA Olympic Round - 70m)
男子个人(FITA奥林匹克回合-70m)男子团体(FITA奥林匹克回合-70m)
But actually I get all four trial events: 但是实际上我得到了所有四个审判事件:
Men's Individual (FITA Olympic Round - 70m) Men's Team (FITA Olympic Round - 70m) Women's Individual (FITA Olympic Round - 70m) Women's Team (FITA Olympic Round - 70m)
男子个人(FITA奥林匹克回合-70m)男子团体(FITA奥林匹克回合-70m)女子个人(FITA奥林匹克回合-70m)女子团体(FITA奥林匹克回合-70m)
Why this behaviour? 为什么这种行为? I don't understand, since there is the if condition only for men.
我不明白,因为只有男人才有这种条件。 How can I fix this?
我怎样才能解决这个问题?
Try this : 尝试这个 :
$html=str_get_html(file_get_html('http://www.olympic.org/olympic-results/london-2012/archery'));
foreach($html->find('div') as $element) {
if ($element->id == 'gamesSelect')
{
$Game = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->id == 'sportSelect')
{
$Sport = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->class == 'man')
{
foreach($element->find('span.trial_name') as $e_trial)
{
$Event = $e_trial->innertext . '<br>';
}
}
Ok, after solved I went on with my script adding this code: 好的,解决之后,我继续我的脚本添加以下代码:
$html=file_get_html(' http://www.olympic.org/olympic-results/london-2012/archery '); $ html = file_get_html(' http://www.olympic.org/olympic-results/london-2012/archery ');
foreach($html->find('div') as $element) { foreach($ html-> find('div')as $ element){
if ($element->id == 'gamesSelect')
{
$Game = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->id == 'sportSelect')
{
$Sport = $element->find('option[selected]', 0)->plaintext . '<br>';
}
if ($element->class == 'man')
{
foreach($element->find('span.trial_name') as $e_trial)
{
$Event = $e_trial->innertext . '<br>';
if ($element->class == "medals gold")
{
foreach($element->find('td.name') as $e_name)
{
$Name = $e_name->innertext . '<br>';
echo $Name;
}
}
}
}
} }
I don't understand how to let it in in the last if condition. 我不明白如何在最后一个if条件中放进去。 Is it not the same of the other if ($element->class == 'man')?
($ element-> class =='man')是否与另一个不同?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.