Whats wrong in this???

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

Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Whats wrong in this???

Post by Nik »

<?php
ob_start();
error_reporting(E_ALL); $i=0;
echo "<body bgproperties='fixed' background='pics/anemos.bmp'>";

$stream = fopen("e:\\web\\data.txt", "r");

while(($x=fgetc($string))!=EOF)
{
echo "<font face='Com' size='7' color='Yellow'>" .$x;
header("Refresh: 2; URL=http://localhost/test.php");
}

ob_end_flush();
?>

I wanted after each letter thet would be readen from the file to refresh ti after 2 secs and the display the next so it would be lik1

h refresh
he refresh
hel refresh
hell refresh
hello refresh

Can it be done and how? I also i think that i have to save the fgetc position after each refresh so that it would not start from the beggining!

right?
:)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're advising the browser to re-request the document each 2 seconds.
Sending such a header does not delay further execution of the script (it's not waiting for 2 seconds). Also read viewtopic.php?t=1157
For each request your script will start over again, not continuing at the last position.
Probably it's best to let a client-side langauge (like javascript) perform that task. E.g. take a look at http://emuseum.mnsu.edu/information/gif ... t/marquee/
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

The refresh works ok! The problem as u noticed is in the thatthe script start all over the beginning which means that i dosen remember where it left! i think that i must save somewhere where the fgetc last accesed!

i also wnted to ask if thsi can be done with $file_array=file("data.txt");
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

what volka is trying to say is that what you're trying to do won't work. to achive this kind of result you'd be better off using javascript.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

i dont want to use javascipt! i already done it eith php
but i want to do it in a more simple way from this one:

<?php

$file_name = "data.txt";
$file_size = filesize($file_name);

$file_offset = isset($_GET['file_offset']) ? $_GET['file_offset'] : 0;
$file_offset = min( $file_offset, $file_size );

$file_so_far = isset($_GET['file_so_far']) ? $_GET['file_so_far'] : '';

$file_stream = fopen($file_name, "r");
fseek($file_stream,$file_offset,SEEK_SET);
$file_so_far .= fread($file_stream,1);
fclose($file_stream);

if ( $file_size > $file_offset ) {
$file_offset++;
$more_to_come = 'yes';
$url_file_so_far = htmlentities(urlencode($file_so_far));
header("Refresh: 0; URL={$PHP_SELF}?file_offset={$file_offset}&file_so_far={$url_file_so_far}");
} else {
$more_to_come = 'start over';
header("Refresh: 3; URL={$PHP_SELF}?file_offset=0");
}

print <<<END
<fieldset style="width:300px">
<legend>Info</legend>
file name = {$file_name} <br/>
file size = {$file_size}<br/>
next offset = {$file_offset}<br/>
more to come ? {$more_to_come}<br/>
</fieldset>
<br />
<br />
<font face='Com' size='7'>{$file_so_far}</font>
END;

?>

So, it can be donw with php!
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

well, then your previous method won't work as you don't send any status parameters between each refresh as you do with this longer on. if you can figure how to send these parameters along with the refresh (session maybe?) then you should be fine.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

Ye nut as i have told i dont want this to become in that way! i always seek better and more siple ways! Evilcoder if u read this pls respond!
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

<?php
ob_start(); error_reporting(E_ALL); $i=$j=0;
echo "<body bgproperties='fixed' background='pics/anemos.bmp'>";

if (@mysql_connect('localhost', 'nik', 'macgyver'));
else @mysql_connect('localhost', 'root');
mysql_select_db('db_nik');

mysql_query("UPDATE visitor SET i=i+1");
$sql = mysql_query("SELECT i FROM visitor");
$i = mysql_fetch_array($sql);

$f = fopen("data.txt", "r");
$keimeno = fread($f, filesize("data.txt"));

if ($keimeno[$j]=='EOF') {mysql_query("UPDATE visitor SET i=0"); exit;}
?>

<center><table border='1' cellspacing='1' width='80%' id='AutoNumber1' bordercolor='Yellow' style='border-style: solid; border-width: 5' background='pics/brick.jpg'>
<tr>
<td align=left bordercolor='#ffcc00'> <font face='Com' size='4' color='LightBlue'>
<? while ($j<=$i[0]) echo $keimeno[$j++]; ?>
</td>
</tr>
</table>

<?
header("Refresh: 0; URL=http://localhost/test.php");
ob_end_flush();
?>


Everythings seems to work ok but i cannot seem to end the loop when the file ends why?

If u pls help!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

because you're sending the refresh-header wether there is more data or not.
You're also aware that 2 people requesting the same page at the same time will double the speed?
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

really?? No am not aware of this!! can u pls show me how to know wehere the string ends s that i will not have to refresh?? :))

Thanks!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
session_start();
if (!isset($_SESSION['i']))
	$_SESSION['i'] = 0;

$f = fopen("data.txt", "r");
$keimeno = fread($f, ++$_SESSION['i']);

if(strlen($keimeno) == $_SESSION['i'])
	header("Refresh: 1; URL=http://localhost/test.php"); // changed timeout to 1 second for testing
// if you want to enable "re-writing" you need something like
// else
//    $_SESSION['i']=0;    --or--  session_destroy();    here
?>
<html><body>
	<center>
		<table border="1" cellspacing="1" width="80%" id="AutoNumber1" bordercolor="Yellow" style="border-style: solid; border-width: 5" background="pics/brick.jpg">
			<tr>
				<td align=left bordercolor="#ffcc00">
					<font face="Com" size="4" color="LightBlue">
<?php echo $keimeno; ?> 
					</font>
				</td>
			</tr>
		</table>
	</center>
</body></html>
Are you still sure you do not want to use javascript? I mean, always a complete request for only one character...?
Anyways ;)
btw: which version of php are you using? Maybe $_SESSION doesn't work
Last edited by volka on Wed Feb 05, 2003 12:47 pm, edited 1 time in total.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

lol, i guess u r right man!!

But i dont know nthing about javascript!!

Why js is better in this occasion?
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

By the way!! The script u gave me rocks!! Works great! And u dont even save the files position to a text file or to a mysql table!!! wooww!!! How do u do that man? its great!!!

This session thing is cool!! Can u tell me what exaclt did u do?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

waste of valuable server computing time e.g. ;)
as mentioned it's a complete new request for each single character.
You're testing it at your local, personal box where only you access the script, but think about putting it on a shared webserver with multiple concurrent requests (not only your script)
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post by Nik »

I also wantes to sk if i could refresh the page at 1/5 of a sec insted of 1 sec with header command can it be done??

sorry for the bother man!
Post Reply