Jump to content
Sign in to follow this  
seblondres

Multiple search terms

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,

 

Share this post


Link to post
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;?>

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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;}?>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...