I don't understand why my if-condition is not working.
From this html page:
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:
Men's Individual (FITA Olympic Round - 70m) Men's Team (FITA Olympic Round - 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)
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 ');
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. Is it not the same of the other if ($element->class == 'man')?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.