jakebur01 Posted April 6, 2009 Share Posted April 6, 2009 How can I optimize this to make it run better. As of now it will not even do a few zip codes. Is there any way to like sleep in between each loop or something? $result = mysql_query("SELECT * FROM zip_code where `state_prefix` = 'OK'", $db) or die(mysql_error()); while($myrow = mysql_fetch_array ($result)) { $zip=$myrow['zip_code']; $html = file_get_html('http://www.my-site.com/test.asp?transaction=search&template=map_search&search1=0&pwidth=400&pheight=700&proxIconId=400&proxIcons=1&search2=0&search3=1&country=US&searchQuantifier=AND&address=&city=&stateProvince=+&postalCode=$zip&radius=500&x=78&y=16'); $i = 0; $tmp = $html->find('span[class=mqEmp], span[class=Black11]'); $cnt = count($tmp) - 1; foreach($tmp as $e) { if($i > 0 && $i < $cnt){ echo $e->plaintext . '<br>'; } $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/ Share on other sites More sharing options...
jakebur01 Posted April 6, 2009 Author Share Posted April 6, 2009 I put double quotes instead of single quotes on file_get_html(), which corrected my $zip variable. But, it is still taking a few seconds even when I use LIMIT 1 in the query. Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/#findComment-802757 Share on other sites More sharing options...
phil88 Posted April 6, 2009 Share Posted April 6, 2009 The main problem as far as efficiency goes with your code is that you have a loop inside a loop inside a loop. Think of it like this; Loop a goes around 10 times Loop b goes around 5 times Loop c goes around 10 times //code End loop c End loop b End Loop a Those loops will be run through a total of 500 times despite the small number in each loop's stop condition. Adding 1 to the number of times any one of those loops will cause the total number of times the code is executed to rocket. Loops inside loops should be avoided where possible and loops inside loops that are inside loops should definitely be avoided - but unfortunately, it's not always possible. As far as your code goes: I have no idea what your code is supposed to do, so I can't really advise as to how to make your code more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/#findComment-802772 Share on other sites More sharing options...
thepip3r Posted April 6, 2009 Share Posted April 6, 2009 your loops are fine.. i use 3-5 nested loops regularly but as jake pointed out above, you need to control your resultset. only limit your results to 20-50 and make a (next,previous) button to provide a method to the user to get the next set of results. Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/#findComment-802776 Share on other sites More sharing options...
jakebur01 Posted April 6, 2009 Author Share Posted April 6, 2009 It is not to bad if I limit it by one state at a time. It is actually faster than I expected after I fixed that little quote issue. I have search and not been able to figure how to tab after x number of loops when writing to a text file. I would like to write 8 fields then tab to a new line. Something like: $i = 0; $tmp = $html->find('span[class=mqEmp], span[class=Black11]'); $cnt = count($tmp) - 1; foreach($tmp as $e) { if($i > 0 && $i < $cnt){ $outputstring=$e->plaintext . '\t'; fwrite($fp, $outputstring, strlen($outputstring)); // if eighth field, then start a new row something like: fwrite($fp, \n); } $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/#findComment-802785 Share on other sites More sharing options...
thepip3r Posted April 6, 2009 Share Posted April 6, 2009 instead of tabbing to a new line why not use the new line, character feed??? \n\r Edit: remember that tabs have nothing to do with new lines. the fact that some text editors WRAP lines after a certain amount of characters has nothing to do with line termination, has to do with visible line space. =D Quote Link to comment https://forums.phpfreaks.com/topic/152859-solved-too-much-to-process/#findComment-802793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.