Working on php progress bar

hello everyone i have this code
public static function progressBar($done, $total, $msg = null) {
$perc = floor(($done / $total) * 100);
echo date(“H:i:s”) . “: {$msg}: {$perc}%\r”;
if ($perc >= 100) {
echo “\n”;
}
}

its ouput looks like this
: Starting Server 23:45:06: Loading Classes: 9% 23:45:06: Loading Classes: 18%

its just a progress bar but u;d like it to look like this…

: Starting Server
23:45:06: Loading Classes: 9%
23:45:06: Loading Scripts: 18%
23:45:06: Loading types: 83%
ect.

Thanks Brian

where is that displayed? in a browser? then use <br/> as line delimiter.

Hi chorn thanks for replying and yes it does… i’ve tried this too but still no luck

public static function progressBar($done, $total, $msg = null) {
        $perc = floor(($done / $total) * 100);    
        while ($perc >= 100) {
            echo "<br/>";
            echo date("H:i:s") . ": {$msg}: {$perc}%\r";
            echo "<br/">; //echo "\n";
        }
    }

it’s almost right i’m getting this now
09:14:30: Starting server
09:14:30: Loading classes: 100%

09:14:30: Loading classes: 100%

09:14:30: Loading classes: 100%

at least now its on seperate lines but not quite right yet

Thanks Brian

within the browser look at the source code of that webpage, \r \n and
are all different line endings, so try skipping out everything except for

Thanks i’ll check that. i have been messing with the code a little bit here is the new code

public static function progressBar($done, $total, $msg = null) {
        $perc = floor(($done / $total) * 100);    
        if ($perc >= 100) {
            echo "";
            echo date("H:i:s") . ": {$msg}: {$perc}%\r";
            echo "<br/>"; // echo "\n";
        }
}

now it gives me this
: Starting Server 17:05:29: Loading Classes: 100%
17:05:29: Loading packets: 100%
17:05:29: Loading skills: 100%
17:05:29: Loading systems: 100%
17:05:29: Loading definitions: 100%
17:05:29: Loading types: 100%
17:05:29: Loading artificial intelligence: 100%
17:05:29: Loading gumps: 100%
17:05:29: Loading scripts: 100%
: Server Is Running On 127.0.0.1 At Port 2593

which is how i want it to look but the progress bar doesn’t actually do anything it just pops ups… it should be doing this
starting server should be 1st line
: Starting Server 17:05:29: Loading Classes: 100%

then progress bar should go to 100% then the next line
17:05:29: Loading Classes: 100%

then the next line ect.
17:05:29: Loading packets: 100%
17:05:29: Loading skills: 100%
17:05:29: Loading systems: 100%

Thanks Snoop

then it’s too fast. and you can use output buffering.

Thanks yeah i’m reading up on it now…

Thanks Snoop

If you are expecting the output to display in near real-time, line by line, it won’t. Web servers and browsers don’t interact like this. They work on a request, response basis. The browser makes a http request for a web page. The web server produces the page and outputs it back to the browser. Due to the buffering that takes place on the web server, in the communications, and in the browser, by the time you see the first character displayed in the browser, the web server has likely long since finished serving the request.

You also have a time scale issue. The time it is taking for this list of tasks to complete, is less than one second. You would need to display the Milliseconds and/or Microseconds, which requires using a datetime object, for you see any more than the unit seconds change by one.

What is your overall goal for doing this at all? If this is for debugging, it would be better if you used microtime(), and then calculated and displayed the time difference for each section of code.

Also, please use bbcode code tags [code][/code] or markdown (three back-ticks ```) around your code when posting it. I have added these to the last two pieces of code.

Sponsor our Newsletter | Privacy Policy | Terms of Service