Skip to content
Advertisement

Display the value of variables in PHP, as soon as they are specified

I want to get about 50 variables from several different sites and show them to user, with PHP Simple HTML DOM Parser. My Code Structure is something like this:

                    <?php


                    include('simple_html_dom.php');



                    $html = file_get_html('https://site1.com/name1/');

                        foreach ($html->find('span#name1') as $e) {
                            $name1 = $e->outertext;
                            echo $name1;
                            break;
                        }

                    $html = file_get_html('https://site2.com/family1/');

                        foreach ($html->find('span#family1') as $e) {
                            $family1 = $e->outertext;
                            echo $family1;
                            break;
                        }

                    $html = file_get_html('https://site3.com/degree1/');

                        foreach ($html->find('span#degree1') as $e) {
                            $height1 = $e->outertext;
                            echo $height1;
                            break;
                        }

                    $html = file_get_html('https://site4.com/height1/');

                        foreach ($html->find('span#height1') as $e) {
                            $height1 = $e->outertext;
                            echo $height1;
                            break;
                        }


                    // About 30 other blocks of code similar to the above code

                    ?>

At the moment, this code works well and displays the variables, but as expected, it takes about two or three minutes to do this process and show all the variables to the user, which is a lot of time.

Now I’m looking for a way to show to the user (send result to browser), each variable that was found and then find the next variable.

For example, when the value of $name1 is specified, show it to the user and then go to specify the value of $family1

Apparently I should use flush() to fix this problem, but unfortunately I could not find a solution to fix this problem.

Advertisement

Answer

You can use ob_flush approach like:

<?php

include('simple_html_dom.php');

$html = file_get_html('https://site1.com/name1/');

    foreach ($html->find('span#name1') as $e) {
        $name1 = $e->outertext;
        echo $name1;
        break;
    }

    flush();
    ob_flush();

$html = file_get_html('https://site2.com/family1/');

    foreach ($html->find('span#family1') as $e) {
        $family1 = $e->outertext;
        echo $family1;
        break;
    }

    flush();
    ob_flush();

$html = file_get_html('https://site3.com/degree1/');

    foreach ($html->find('span#degree1') as $e) {
        $height1 = $e->outertext;
        echo $height1;
        break;
    }

    flush();
    ob_flush();

$html = file_get_html('https://site4.com/height1/');

    foreach ($html->find('span#height1') as $e) {
        $height1 = $e->outertext;
        echo $height1;
        break;
    }

    flush();
    ob_flush();
// About 30 other blocks of code similar to the above code

?>

This code after each block flush PHP output to browser.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement