dflow Posted December 17, 2010 Share Posted December 17, 2010 i have an array of website links i want to loop through each and get a certain html id foreach whats wrong with what i did here? :confused: foreach($links as $page) { $phtml = file_get_html($page); // find all td tags with attribite align=center foreach($phtml->find('span[id=name]') as $name) echo $name->plaintext.'<br><br>'; } Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/ Share on other sites More sharing options...
sloth456 Posted December 17, 2010 Share Posted December 17, 2010 You haven't enclosed the second foreach statement in curly brackets. It should be: foreach($links as $page) { $phtml = file_get_html($page); // find all td tags with attribite align=center foreach($phtml->find('span[id=name]') as $name){ echo $name->plaintext.'<br><br>'; } } Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148752 Share on other sites More sharing options...
dflow Posted December 17, 2010 Author Share Posted December 17, 2010 im getting error: Parse error: syntax error, unexpected T_FOREACH in parser2.php on line 9 Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148836 Share on other sites More sharing options...
BlueSkyIS Posted December 18, 2010 Share Posted December 18, 2010 there aren't 9 lines in the code snippet. can we see the latest? Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148839 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 <?php // example of how to use basic selector to retrieve HTML contents include('simple_html_dom.php'); $links=array("http://www.example.com/page1.htm"," http://www.example.com/page2.htm") foreach($links as $page) { $phtml = file_get_html($page); // find all td tags with attribite align=center foreach($phtml->find('span[id=name]') as $name){ echo $name->plaintext.'<br><br>'; } }?> Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148847 Share on other sites More sharing options...
QuickOldCar Posted December 18, 2010 Share Posted December 18, 2010 Was no semicolon after links array <?php // example of how to use basic selector to retrieve HTML contents include('simple_html_dom.php'); $links=array("http://www.example.com/page1.htm"," http://www.example.com/page2.htm"); foreach($links as $page) { $phtml = file_get_html($page); // find all td tags with attribite align=center foreach($phtml->find('span[id=name]') as $name){ echo $name->plaintext.'<br><br>'; } }?> Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148849 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 thanks now i run the whole script and their are 2 problems 1. i get still get non unique links(3 of a kind) 2. error: Notice: Undefined variable: links in parser.php on line 41 Warning: Invalid argument supplied for foreach() in parser.php on line 41 <?php // example of how to use basic selector to retrieve HTML contents include('../simple_html_dom.php'); // get DOM from URL or file $html = file_get_html('http://www.example.com/AllApartments.aspx'); //foreach($html->find('td[class=stableline1]') as $f) { // $arrayf[] = $f->innertext; //} // find all link foreach($html->find('a') as $e) { $arraylinks[] = $e->href . '<br>'; } foreach ($arraylinks as $link) { $category = basename(dirname($link)); $page = basename($link); if ($category == "apartments") { $url="http://www.example.com/apartments/"; echo $page.'<br />'; echo $url.$page.'<br />'; } } foreach($links['apartments'] as $page) { $phtml = file_get_html($url.$page); // find all td tags with attribite foreach($phtml->find('span[id=lblCodiceAppartamento]') as $name){ echo $name->plaintext.'<br><br>'; } }?> Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148854 Share on other sites More sharing options...
trq Posted December 18, 2010 Share Posted December 18, 2010 And which is line 41? Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148855 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 Quote And which is line 41? foreach($links['apartments'] as $page) { Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148858 Share on other sites More sharing options...
trq Posted December 18, 2010 Share Posted December 18, 2010 The error is pretty self explanatory. $links is not defined anywhere in your code. Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148866 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 Quote The error is pretty self explanatory. $links is not defined anywhere in your code. sorry indeed $links=array(); how would i define only unique results for $links ? Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1148872 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 Quote thanks now i run the whole script and their are 2 problems 1. i get still get non unique links(3 of a kind) 2. error: Notice: Undefined variable: links in parser.php on line 41 Warning: Invalid argument supplied for foreach() in parser.php on line 41 <?php // example of how to use basic selector to retrieve HTML contents include('../simple_html_dom.php'); // get DOM from URL or file $html = file_get_html('http://www.example.com/AllApartments.aspx'); //foreach($html->find('td[class=stableline1]') as $f) { // $arrayf[] = $f->innertext; //} // find all link foreach($html->find('a') as $e) { $arraylinks[] = $e->href . '<br>'; } foreach ($arraylinks as $link) { $category = basename(dirname($link)); $page = basename($link); if ($category == "apartments") { $url="http://www.example.com/apartments/"; echo $page.'<br />'; echo $url.$page.'<br />'; } } $links=array(); foreach($links['apartments'] as $page) { $phtml = file_get_html($url.$page); // find all td tags with attribite foreach($phtml->find('span[id=lblCodiceAppartamento]') as $name){ echo $name->plaintext.'<br><br>'; } }?> now i get a memory issue although i set the php.ini to maximum when looping through 20 different urls Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 700 bytes) in/simplehtmldom/simple_html_dom.php on line 829 Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1149056 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 i set the maximum memory to 80mb but why is this script taking so much memory?? Fatal error: Allowed memory size of 83886080 bytes exhausted (tried to allocate 700 bytes) Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1149058 Share on other sites More sharing options...
dflow Posted December 18, 2010 Author Share Posted December 18, 2010 ok fixed Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1149064 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2010 Share Posted December 18, 2010 Try: VALUES( '$name' ) Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1149065 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2010 Share Posted December 18, 2010 Actually, if the data isn't being escaped prior to the query string, you should also do that. Link to comment https://forums.phpfreaks.com/topic/221982-loop-through-different-websitesconfused/#findComment-1149067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.