Jump to content

Multiple search terms


seblondres
 Share

Recommended Posts

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...