I think the best way is to display the data in a text box like $address=explode("<br />",$contact[address]); $stringData .= "\"".$contact[name]."\",\"".trim($address[0])."\",\"".trim($address[1])."\",\"".trim($address[1])."\",\"".trim($contact[phone])."\"\n"; Code (markup): at end of the file add <textarea rows=10 cols=50><?php echo $stringData; ?></textarea> Code (markup): This way you can copy contents from the box and paste it to notepad save as .csv Just a thought
<?php $name1= $_POST['name1']; $name2= $_POST['name2']; $name3= $_POST['name3']; $name4= $_POST['name4']; ?> <html> <link rel=stylesheet type="text/css" href="CSS/default0.css"> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> Last Name 1 : <input type=text name=name1 value=<?php echo $_POST['name1']; ?>><br> Last Name 2 : <input type=text name=name2 value=<?php echo $_POST['name2']; ?>><br> Last Name 3 : <input type=text name=name3 value=<?php echo $_POST['name3']; ?> ><br> Last Name 4 : <input type=text name=name4 value=<?php echo $_POST['name4']; ?>><br> <label for="city">City : </label><span class="field"><select id="city" name="city"><option value=""></option> <optgroup label=England> <option>Bath <option>Brighton and Hove <option>Canterbury <option>Chichester <option>Durham <option>Gloucester <option>Lancaster <option>Lichfield <option>London <option>Norwich <option>Peterborough <option>Preston <option>Salisbury <option>St Albans <option>Truro <option>Westminster <option>Worcester <option>Birmingham <option>Bristol <option>Carlisle <option>Coventry <option>Ely <option>Hereford <option>Leeds <option>Lincoln <option>Manchester <option>Nottingham <option>Plymouth <option>Ripon <option>Sheffield <option>Stoke-on-Trent <option>Wakefield <option>Winchester <option>York <option>Bradford <option>Cambridge <option>Chester <option>Derby <option>Exeter <option>Kingston upon Hull <option>Leicester <option>Liverpool <option>Newcastle upon Tyne <option>Oxford <option>Portsmouth <option>Salford <option>Southampton <option>Sunderland <option>Wells <option>Wolverhampton </optgroup> <optgroup label="Northern Ireland"> <option>Armagh <option>Lisburn <option>Belfast <option>Newry <option>Londonderry </optgroup> <optgroup label="Scotland"> <option>Aberdeen <option>Glasgow <option>Dundee <option>Inverness <option>Edinburgh <option>Stirling </optgroup> <optgroup label="Unitary Authorities of Wales"> <option>Bangor <option>St Davids <option>Cardiff <option>Swansea <option>Newport </optgroup> </select> <input type="submit" value="submit" name="submit"> </form> </html> <?php if($_POST['submit']=="submit"){ $location=$_POST['city']; include_once('simple_html_dom.php'); $namedata[]=array($name1,$name2,$name3,$name4); foreach($namedata as $name){ $page_exist=true; $i=0; while($page_exist){ $i+=1; $myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name[$i-1].'&Location='.$location.'&pageSize=50&pageNumber='.$i; echo $myurl; if (function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $myurl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); $content = curl_exec($ch); curl_close($ch); } $html=str_get_html($content); if(!$html->find('div[class=searchResult]',0)==""){ foreach($html->find('div[class=searchResult]') as $result){ $resultdata[]=array( 'name' => $result->find('h2',0)->innertext, 'address' => $result->find('div[class=address]',0)->innertext, 'phone' => $result->find('div[class=telephoneNumber]',0)->innertext ); } }else $page_exist=false; } } //YOU CAN NOW DO WHATEVER YOU NEED WITH THE RESULT ARRAY //$mycsvFile = "testFile.csv"; //$fh = fopen($mycsvFile, 'w') or die("can't open file"); ?> <? echo "<table border=1 id=myTable><tr><th>Name</th><th>Address</th><th>City</th><th>Post Code</th><th>Phone</th></tr>"; foreach($resultdata as $contact){ $address=explode("<br />",$contact[address]); $stringData .= "\"".$contact[name]."\",\"".trim($address[0])."\",\"".trim($address[1])."\",\"".trim($address[1])."\",\"".trim($contact[phone])."\"\n"; echo "<tr><td>".$contact[name]."</td><td>". str_replace('<br />', '</td><td>', $contact[address]) ."</td><td>".$contact[phone]."</td></tr>"; } echo "</table>"; } ?> <textarea rows=50 cols=250 locked=true><?php echo $stringData; ?></textarea> Code (markup): Found a small error : http://www.118.com/people-search.mv...cation=Wolverhampton&pageSize=50&pageNumber=1 http://www.118.com/people-search.mv...cation=Wolverhampton&pageSize=50&pageNumber=2 http://www.118.com/people-search.mv...cation=Wolverhampton&pageSize=50&pageNumber=3 Whereas Smith should pass through 1,2,3,4 pages and then again PageNumber shld be reset to 1 for William.
Sorry for that, Just delete the [$i-1] in $name[$i-1] at the line $myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name[$i-1].'&Location='.$location.'&pageSize=50&pageNumber='.$i; Code (markup):
So, it wld be : $myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name.'&Location='.$location.'&pageSize=50&pageNumber='.$i;
Sorry for the confusion, It should be $myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name[0].'&Location='.$location.'&pageSize=50&pageNumber='.$i; Code (markup):
http://www.118.com/people-search.mv...mith&Location=Bangor&pageSize=50&pageNumber=1 http://www.118.com/people-search.mv...mith&Location=Bangor&pageSize=50&pageNumber=2 $name[0] will always be zero ? I think we need to use another variable as "j". and then increment that j value.
Yes, $name[0] will always be zero because it will get a new value every time it passes through the foreach loop
http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=Bangor&pageSize=50&pageNumber=1 http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=Bangor&pageSize=50&pageNumber=2 Code (markup): I want name1, and then name2 and etc to use and Not always name1.
It searches for the results with the keyword like this: Name1 - page 1, 2, 3, 4(if no results in page 4 - mean there are only 3 pages of results) <<then moves to next>> Name2 - page 1, 2,... Name3 - page 1, 2,... Let me know if it does not work that way Try adding echo $myurl."<br>"; to find how many url it queries
It shld be : Name1 - page 1, 2, 3, 4(if no results in page 4 - mean there are only 3 pages of results) <<then moves to next>> Name2 - page 1, 2,... Name3 - page 1, 2,... Name4 - page 1, 2,...
got it, I think the below code works good <?php ini_set('max_execution_time', 200); if($_POST['name1']) $name[]= $_POST['name1']; if($_POST['name2']) $name[]= $_POST['name2']; if($_POST['name3']) $name[]= $_POST['name3']; if($_POST['name4']) $name[]= $_POST['name4']; ?> <html> <head> <link rel=stylesheet type="text/css" href="CSS/default0.css"> </head> <body> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> Last Name 1 : <input type=text name=name1 value=<?php echo $_POST['name1']; ?>><br> Last Name 2 : <input type=text name=name2 value=<?php echo $_POST['name2']; ?>><br> Last Name 3 : <input type=text name=name3 value=<?php echo $_POST['name3']; ?> ><br> Last Name 4 : <input type=text name=name4 value=<?php echo $_POST['name4']; ?>><br> <label for="city">City : </label><span class="field"><select id="city" name="city"><option value=""></option> <optgroup label=England> <option>Bath <option>Brighton and Hove <option>Canterbury <option>Chichester <option>Durham <option>Gloucester <option>Lancaster <option>Lichfield <option>London <option>Norwich <option>Peterborough <option>Preston <option>Salisbury <option>St Albans <option>Truro <option>Westminster <option>Worcester <option>Birmingham <option>Bristol <option>Carlisle <option>Coventry <option>Ely <option>Hereford <option>Leeds <option>Lincoln <option>Manchester <option>Nottingham <option>Plymouth <option>Ripon <option>Sheffield <option>Stoke-on-Trent <option>Wakefield <option>Winchester <option>York <option>Bradford <option>Cambridge <option>Chester <option>Derby <option>Exeter <option>Kingston upon Hull <option>Leicester <option>Liverpool <option>Newcastle upon Tyne <option>Oxford <option>Portsmouth <option>Salford <option>Southampton <option>Sunderland <option>Wells <option>Wolverhampton </optgroup> <optgroup label="Northern Ireland"> <option>Armagh <option>Lisburn <option>Belfast <option>Newry <option>Londonderry </optgroup> <optgroup label="Scotland"> <option>Aberdeen <option>Glasgow <option>Dundee <option>Inverness <option>Edinburgh <option>Stirling </optgroup> <optgroup label="Unitary Authorities of Wales"> <option>Bangor <option>St Davids <option>Cardiff <option>Swansea <option>Newport </optgroup> </select> <input type="submit" value="submit" name="submit"> </form> <?php if($_POST['submit']=="submit"){ if(!$_POST['city']) die('No City selected'); $location=$_POST['city']; include_once('simple_html_dom.php'); for($loop=0; $loop<count($name); $loop++){ $page_exist=true; $i=0; while($page_exist){ $i+=1; $myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name[$loop].'&Location='.$location.'&pageSize=50&pageNumber='.$i; echo $myurl."<br>"; if (function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $myurl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); $content = curl_exec($ch); curl_close($ch); } $html=str_get_html($content); if(!$html->find('div[class=searchResult]',0)==""){ foreach($html->find('div[class=searchResult]') as $result){ $resultdata[]=array( 'name' => $result->find('h2',0)->innertext, 'address' => $result->find('div[class=address]',0)->innertext, 'phone' => $result->find('div[class=telephoneNumber]',0)->innertext ); } }else $page_exist=false; } } //YOU CAN NOW DO WHATEVER YOU NEED WITH THE RESULT ARRAY //$mycsvFile = "testFile.csv"; //$fh = fopen($mycsvFile, 'w') or die("can't open file"); echo "<table id=\"myTable\" border=1><tr><th>Name</th><th>Address</th><th>City</th><th>Post Code</th><th>Phone</th></tr>"; foreach($resultdata as $contact){ //$address=explode("<br />",$contact[address]); //$stringData = "\"".$contact[name]."\",\"".trim($address[0])."\",\"".trim($address[1])."\",\"".trim($address[1])."\",\"".trim($contact[phone])."\"\n"; //fwrite($fh, $stringData); echo "<tr><td>".$contact[name]."</td><td>". str_replace('<br />', '</td><td>', $contact[address]) ."</td><td>".$contact[phone]."</td></tr>"; } echo "</table>"; //fclose($fh); } ?> </body></html> Code (markup): Gives results http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=London&pageSize=50&pageNumber=1 http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=London&pageSize=50&pageNumber=2 http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=London&pageSize=50&pageNumber=3 http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=London&pageSize=50&pageNumber=4 http://www.118.com/people-search.mvc?Supplied=true&Name=smith&Location=London&pageSize=50&pageNumber=5 http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=London&pageSize=50&pageNumber=1 http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=London&pageSize=50&pageNumber=2 http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=London&pageSize=50&pageNumber=3 http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=London&pageSize=50&pageNumber=4 http://www.118.com/people-search.mvc?Supplied=true&Name=william&Location=London&pageSize=50&pageNumber=5 http://www.118.com/people-search.mvc?Supplied=true&Name=david&Location=London&pageSize=50&pageNumber=1 http://www.118.com/people-search.mvc?Supplied=true&Name=david&Location=London&pageSize=50&pageNumber=2 http://www.118.com/people-search.mvc?Supplied=true&Name=david&Location=London&pageSize=50&pageNumber=3 http://www.118.com/people-search.mvc?Supplied=true&Name=david&Location=London&pageSize=50&pageNumber=4 http://www.118.com/people-search.mvc?Supplied=true&Name=david&Location=London&pageSize=50&pageNumber=5 Code (markup): Check and do let me know
<div class="nameDetailsInnerContainer InnerContainer"><h2 class='nameDetails '>Smith A</h2></div></div><div class="clearfix"><div class="entry_content"><div class="full_listing"><div class="contact_highlight"><div id='entry_0_listing' class="listingBody"><div class="phoneNo"><table><tr><td><span class='phoneNoText '> (03) 9663 5832 </span></td><td style="width:10px;"></td><td class="share_link" wpol:entryId="715585944V00W" wpol:contactPointId="715585944V00W"><div class="share_icon"></div><a class="screen_reader_only" rel="nofollow" href="/wp/mobile/send-to-mobile-accessible?entryId=715585944V00W&listingId=715585944V00W&searchType=R&channel=WP" name="Smith">Send this listing to your mobile</a></td></tr></table></div><div class="addressContainer"><span class="streetLine">25 Spring St</span><br /><span class="locality">Melbourne</span><span class="state"> VIC</span><span class="postcode"> 3000</span> Code (markup): Smith A (03) 9663 58XX 25 Spring St Melbourne VIC 3000 What will be this part : $resultdata[]=array( 'name' => $result->find('h2 class=nameDetails ',0)->innertext, 'address' => $result->find('div[class=addressContainer]',0)->innertext, 'phone' => $result->find('div[class=phoneNoText ]',0)->innertext ); Code (markup):
$resultdata[]=array( 'name' => $result->find('h2[class=nameDetails]',0)->innertext, 'streetLine' => $result->find('span[class=streetLine]',0)->innertext, 'locality' => $result->find('span[class=locality]',0)->innertext, 'state' => $result->find('span[class=state]',0)->innertext, 'postcode' => $result->find('span[class=postcode]',0)->innertext, 'phone' => $result->find('span[class=phoneNoText]',0)->innertext ); Code (markup):
Thanks, Can u explain me in Brief above code so that Next time If I have to do it, I shld be able to do it myself !!1
I get this link when I do echo : http://whitepages.com.au/wp/resSearch.do?subscriberName=smith&location=Sydney&page=1 When I paste the link in Browser I do see there are 21 records. But when I run the Code, it doesn't Return me anything. <?php ini_set('max_execution_time', 200); if($_POST['name1']) $name[]= $_POST['name1']; if($_POST['name2']) $name[]= $_POST['name2']; if($_POST['name3']) $name[]= $_POST['name3']; if($_POST['name4']) $name[]= $_POST['name4']; if($_POST['name5']) $name[]= $_POST['name5']; ?> <html> <head> <link rel=stylesheet type="text/css" href="CSS/default0.css"> </head> <body> <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> Last Name 1 : <input type=text name=name1 value=<?php echo $_POST['name1']; ?>><br> Last Name 2 : <input type=text name=name2 value=<?php echo $_POST['name2']; ?>><br> Last Name 3 : <input type=text name=name3 value=<?php echo $_POST['name3']; ?> ><br> Last Name 4 : <input type=text name=name4 value=<?php echo $_POST['name4']; ?>><br> Last Name 5 : <input type=text name=name5 value=<?php echo $_POST['name5']; ?>><br> <label for="city">City : </label><span class="field"><select id="city" name="city"><option value=""></option> <optgroup label="New South Wales"> <option>Sydney <option>Newcastle <option>Central Coast <option>Wollongong <option>Maitland <option>Wagga Wagga <option>Port Macquarie <option>Tamworth <option>Orange <option>Dubbo <option>Bathurst <option>Nowra-Bomaderry <option>Lismore <option>Coffs Harbour <option>Richmond-Windsor <option>Albury-Wodonga <option>Darwin <option>Palmerston </optgroup> <optgroup label=" Queensland"> <option>Brisbane <option>Sunshine Coast <option>Townsville-Thuringowa <option>Cairns <option>Toowoomba <option>Mackay <option>Rockhampton <option>Bundaberg <option>Hervey Bay <option>Gladstone <option>Gold Coast-Tweed Heads </optgroup> <optgroup label="South Australia"> <option>Adelaide <option>Mount Gambier </optgroup> <optgroup label="Tasmania"> <option>Hobart <option>Launceston </optgroup> <optgroup label="Victoria"> <option>Melbourne <option>Geelong <option>Ballarat <option>Bendigo <option>Shepparton-Mooroopna <option>Melton <option>Mildura <option>Sunbury <option>Warrnambool </optgroup> <optgroup label="Western Australia"> <option>Perth <option>Mandurah <option>Rockingham <option>Bunbury <option>Kalgoorlie-Boulder <option>Geraldton <option>Albany </optgroup> </select> <input type="submit" value="submit" name="submit"> </form> <?php if($_POST['submit']=="submit"){ if(!$_POST['city']) die('No City selected'); $location=$_POST['city']; include_once('simple_html_dom.php'); for($loop=0; $loop<count($name); $loop++){ $page_exist=true; $i=0; while($page_exist){ $i+=1; $myurl='http://whitepages.com.au/wp/resSearch.do?subscriberName='.$name[$loop].'&location='.$location.'&page='.$i; //$myurl='http://www.118.com/people-search.mvc?Supplied=true&Name='.$name[$loop].'&Location='.$location.'&pageSize=50&pageNumber='.$i; echo $myurl."<br>"; if (function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $myurl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); $content = curl_exec($ch); curl_close($ch); } $html=str_get_html($content); if(!$html->find('div[class=searchResult]',0)==""){ foreach($html->find('div[class=searchResult]') as $result){ $resultdata[]=array( 'name' => $result->find('h2[class=nameDetails]',0)->innertext, 'streetLine' => $result->find('span[class=streetLine]',0)->innertext, 'locality' => $result->find('span[class=locality]',0)->innertext, 'state' => $result->find('span[class=state]',0)->innertext, 'postcode' => $result->find('span[class=postcode]',0)->innertext, 'phone' => $result->find('span[class=phoneNoText]',0)->innertext ); } }else $page_exist=false; } } //YOU CAN NOW DO WHATEVER YOU NEED WITH THE RESULT ARRAY foreach($resultdata as $contact){ echo $contact[name]; $stringData .= $contact[name].",".$contact[streetline].",".$contact[locality].",".$contact[state].",".$contact[postcode].",".$contact[phone]."\n"; } } ?> <textarea rows=50 cols=100 locked=true><?php echo $stringData; ?></textarea> </body></html> Code (markup): I presume I need to change if(!$html->find('div[class=searchResult]',0)==""){ foreach($html->find('div[class=searchResult]') as $result){ Code (markup): to $html=str_get_html($content); if(!$html->find('div[class=nameDetailsInnerContainer InnerContainer]',0)==""){ foreach($html->find('div[class=nameDetailsInnerContainer InnerContainer]') as $result){ Code (markup): I tried : foreach($resultdata as $contact){ echo "inside"; But It didnt print anything. Looks like $resultdata is NOT getting data.
change this if(!$html->find('div[class=searchResult]',0)==""){ foreach($html->find('div[class=searchResult]') as $result){ Code (markup): to $html=$html->find('div[id=entries]',0); if(!$html->find('div[class=entry]',0)==""){ foreach($html->find('div[class=entry]') as $result){ Code (markup):