php - Page loading progressively -
i'm building basic scraping tool, i'm running locally on laptop, backup data cms.
the basic procedural script wrote loads urls database , each url, scraps page, saves content database , echoes page saved.
the problem when manages go through urls @ once (a few hundreds of @ times), output of script loads progressively in browser.
in firefox can see part of echo statements few pages (indicating pages saved), rest comes in batches , @ bottom firefox indicates me "transferring data localhost..."
i'm confused because thought when php script runs, outputs , sends response single block, when it's done, , not this, progressively.
maybe i'm forgetting in code? think it?
here basic structure of script:
<?php try { // login cms // connect db urls ($i = 0; $i < count($urls); $i++) { // data page $data = $scraper->getdata($urls[$i]); // store data page if ( $db->save($data) ) { echo 'data saved "' . $url[$i] . '"<br>'; } else { echo 'problem when saving data "' . $url[$i] . '"<br>'; } } } catch (exception $e) { echo $e->getmessage() . '<br>'; } ?>
i thought of using output buffering thing if script fails or times out, thought i'm not going output @ all.
some type of output buffer want use. append these string , echo string when you're done:
<?php try { // login cms // connect db urls $html = ''; ($i = 0; $i < count($urls); $i++) { // data page $data = $scraper->getdata($urls[$i]); // store data page if ( $db->save($data) ) { $html .= 'data saved "' . $url[$i] . '"<br>'; } else { $html .= 'problem when saving data "' . $url[$i] . '"<br>'; } } echo $html; } catch (exception $e) { echo $e->getmessage() . '<br>'; } ?>
just echoing in php starts sending document immediately, if script not finish loading. why seeing happen line-by-line.
you @ ob_start() http://php.net/manual/en/function.ob-start.php . standard way of echoing were, holding output until ready show all.
as @lithis mentioned in comment, if want wait display information after script has stopped running, wrap in
<div style="display:none"></div>
then use javascript on document.ready change display type "block" avoid appearing line-by-line.
Comments
Post a Comment