help need on ob_start()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
codingislife
Forum Newbie
Posts: 4
Joined: Thu Aug 13, 2009 9:49 am

help need on ob_start()

Post by codingislife »

Hi

I am a newbie in php, I have a code

Code: Select all

 
 
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
    sleep(1);
    echo $i;
    ob_flush();
}
?>
 
 
I am trying to display one number in each second, but the browser showing all the numbers at once. where is the error ? how can I modify this code to show such a way

Thanks :D
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: help need on ob_start()

Post by neuroxik »

You flush the buffer inside the first loop, that's why.

I'm not sure if it would work, but in any case, you should close the buffer AFTER your looping, like this:

Code: Select all

 
ob_start();
for($i=1;$i<=10;$i++)
{
    sleep(1);
    echo $i;
}
ob_flush();
 
UPDATE: JUst tried it and doesn't work, just as thought. Buffers normally allow you to do stuff that normally can't be done before headers have started to be sent or similar, but you'd have to (from my knowledge) use front-end coding, example javascript, to accomplish that.
Last edited by neuroxik on Thu Aug 13, 2009 10:09 am, edited 2 times in total.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: help need on ob_start()

Post by SidewinderX »

Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: help need on ob_start()

Post by neuroxik »

SidewinderX wrote:Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
That's what I was thinking, I was just telling him that it would already be more reasonable to flush outside the loop if he tried.
codingislife
Forum Newbie
Posts: 4
Joined: Thu Aug 13, 2009 9:49 am

Re: help need on ob_start()

Post by codingislife »

hi thanks for your reply

but I want to display 1,2,3,5.... in browser in each seconds. now all the contents are flushing out at the 10th second. I need to 1 in first second , 2 in the 2nd second , 3 in the 3rd second etc...

now it shows 12345... at the 10th second, the code that I am using so far is

Code: Select all

 
 
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
    sleep(1);
    echo $i;
    ob_flush();
}
?>
 
 
codingislife
Forum Newbie
Posts: 4
Joined: Thu Aug 13, 2009 9:49 am

Re: help need on ob_start()

Post by codingislife »

hi thanks for your reply

but I want to display 1,2,3,5.... in browser in each seconds. now all the contents are flushing out at the 10th second. I need to 1 in first second , 2 in the 2nd second , 3 in the 3rd second etc...

now it shows 12345... at the 10th second, the code that I am using so far is

Code: Select all

 
 
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
    sleep(1);
    echo $i;
    ob_flush();
}
?>
 
 
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: help need on ob_start()

Post by neuroxik »

Did you even read what we wrote? You even quoted your own uncorrected code.
codingislife
Forum Newbie
Posts: 4
Joined: Thu Aug 13, 2009 9:49 am

Re: help need on ob_start()

Post by codingislife »

neuroxik wrote:Did you even read what we wrote? You even quoted your own uncorrected code.
I am talking about an example showing at http://in3.php.net/manual/en/function.ob-start.php (the 2nd one ) edited by Hudson on 14-May-2009 09:16 , please check and reply me thanks
corecoder
Forum Newbie
Posts: 6
Joined: Fri Aug 14, 2009 3:57 am

Re: help need on ob_start()

Post by corecoder »

please use the following code to do so

<?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
corecoder
Forum Newbie
Posts: 6
Joined: Fri Aug 14, 2009 3:57 am

Re: help need on ob_start()

Post by corecoder »

neuroxik wrote:
SidewinderX wrote:Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
That's what I was thinking, I was just telling him that it would already be more reasonable to flush outside the loop if he tried.
hey buddy, he can do so by the following code

please use the following code to do so

<?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
User avatar
neuroxik
Forum Commoner
Posts: 25
Joined: Tue Aug 11, 2009 10:32 pm

Re: help need on ob_start()

Post by neuroxik »

corecoder wrote: <?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
8O
Wow, this works.. never thought it would. Thanks gfor sharing :)
wwuster
Forum Newbie
Posts: 6
Joined: Sat Nov 08, 2008 6:01 am

Re: help need on ob_start()

Post by wwuster »

I'm trying to do the same thing as the original poster. But running the last code posted doesn't display one item per second in the browser. After 3 seconds it displays everything at once. So flush() is not working. This is using apache2, firefox, Ubuntu linux. Any idea why this doesn't work? I've tried MANY combinations of ob_flush(), flush(), setting compression options etc.

thanks,
William

$n = 3;
ob_end_flush();

for($i=0; $i<$n; $i++) {
sleep(1);
$str = sprintf("%d / %d", $i+1, $n);
echo $str;
flush();
}
$str = 'DONE!';
echo $str;
Post Reply