seblondres Posted February 4, 2015 Share Posted February 4, 2015 Hi, I have the script below which works fine when searching 1 term (city). Now what I like to do, it's to search through multiple terms, so how can I pass one term after the other into the $url so I can get the result for each term on the same page. Ideally I'd like to be able to read a txt file with all the terms (cities). <?phpinclude_once 'simple_html_dom.php';$search_term = "paris";$url = "http://www.domain.net/annuaire/?TypeServices=0&VilleLieu={$search_term}&annusearch=1&Chercher=Chercher";$html = file_get_html($url);$ret = $html->find('div[class=Annuaire-int]');foreach($ret as $story) echo $story;?> Thanks, Link to comment Share on other sites More sharing options...
justsomeguy Posted February 4, 2015 Share Posted February 4, 2015 Are you talking putting multiple search terms into a single URL? Link to comment Share on other sites More sharing options...
seblondres Posted February 4, 2015 Author Share Posted February 4, 2015 I just want to search one term at the time, something with foreach perhaps Link to comment Share on other sites More sharing options...
seblondres Posted February 4, 2015 Author Share Posted February 4, 2015 I've tried the following but I can only display the information for Paris: <?phpinclude_once 'simple_html_dom.php';$search_term = array( "lyon", "cannes", "paris" ); foreach ($search_term as $search_term) { $url = "http://www.pole-emploi.fr/annuaire/?TypeServices=0&VilleLieu={$search_term}&annusearch=1&Chercher=Chercher";}$html = file_get_html($url);$ret = $html->find('div[class=Annuaire-int]');foreach($ret as $story) echo $story;?> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 4, 2015 Share Posted February 4, 2015 Your loop doesn't do anything except define a variable called $url, so after that loop finishes $url will have the last item in the array. You should move the code to use that URL into the loop so that you do everything you need to do for each URL inside the loop. Link to comment Share on other sites More sharing options...
seblondres Posted February 5, 2015 Author Share Posted February 5, 2015 Thanks for your reply. I'm not sure what you mean exactly, would you be able to send me an example or to point me to a link/doc? Many Thanks, Link to comment Share on other sites More sharing options...
justsomeguy Posted February 5, 2015 Share Posted February 5, 2015 Just think about what you're trying to do. You have a list of terms. For each term, you want to build a URL, get the contents of that URL, find an element on the page, and then print it. Just listen to how you describe it in English, "for each term...", it's just like the code. Your code has a foreach loop, but the only thing the loop does is build a URL. You don't get the page and find the element until after the loop. All of that code needs to be inside the loop. Link to comment Share on other sites More sharing options...
seblondres Posted February 7, 2015 Author Share Posted February 7, 2015 Got it, thanks! <?phpinclude_once 'simple_html_dom.php';$search_term = array( "lyon", "cannes", "paris", "marseille" );for($i=0;$i<count($search_term);$i++){ $city = $search_term[$i]; $url = "http://www.pole-emploi.fr/annuaire/?TypeServices=0&VilleLieu={$city}&annusearch=1&Chercher=Chercher";$html = file_get_html($url);$ret = $html->find('div[class=Annuaire-int]');foreach($ret as $story) echo $story;}?> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now