Letters

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

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

Post by volka »

you close the file at every while(list($array) = each($file))-iteration
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok so i close it after i finish the while statment and everything in it?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that in any case.
what does you script look like. I'm a little bit confused about the snippet.

(may be related to the fact that it's 5 am here and I'm only waiting for eDonkey to complete a -of course legal- download)
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

Code: Select all

<?php
$file = file("file.ext");
$filename = "chart.txt";
$fe = fopen ($filename, "a+");
$ff = fread ($fe, 100000);
$hmm = fopen ($filename, "r");
$hmm2 = fread ($hmm, 100000);
$userfile = "users.txt";
$users = fopen ($userfile, "r");
$userread = fread ($users, 100);
$table = "</TABLE>";
$a = 62;
$b = 4320;


while(list($array) = each($file))&#123;

if ($array > $a and $array < $b)&#123;
fputs ($fe, $file&#1111;$array], 100000);
&#125;
&#125;

if (file_exists($filename))&#123;
fputs ($fe, $table, 10);
&#125; else &#123;
print "error";
&#125;

if (file_exists($filename))&#123;
include 'style.css';
include 'header.php';
print $hmm2;
end ($file);
&#125; else &#123;
print "error";
&#125;
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

wow, gets harder to concentrate from minute to minute ;)
while(list($array) = each($file))
{
if ($array > $a and $array < $b)
fputs ($fe, $file[$array], 100000);
}
the while condition assigns in every iteration a (/the next) line from the array $file to $array - not the index. So (I think) your if-condition is not what you want. Same reason why $file[$array] will probably fail. It tries to access an entry with the index equal to the current line's value. Probably you're looking for something like this

Code: Select all

for ($a=$a+1; $a<count($file) && $a<$b; $a++)
	fputs ($fe, $file&#1111;$a], 100000);
if (file_exists($filename))
you're checking this after you've tried to write to a filedescriptor of that file. Your script would have produced a lot of warnings at this point.


can't find any unlink in your script !??!


ahhh...you're opening the same file twice, reading it's contents before you append something. do you want to display the old contents?
Last edited by volka on Mon Aug 19, 2002 10:39 pm, edited 1 time in total.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

i removed unlink since it didnt work but i added it at the script because i want it to delete the file after the script runs.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

and could you explain that script you just showed me some more there are so many $a's and $b's agh (the $a and $b was for the starting line and ending line so it would show everything in between when it sent it to the text file)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please, first explain what you're trying to achieve.
I can imagine but I'm not quite sure
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok first im having it read the array from the $file at teh top and then i have it put that information into the text file, then i want to add </TABLE> tot he end of the text file. and then finally i want it to read back all the info from that text file i had it create.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

so you want to keep only lines 63 to 4319 in the original file and add </table>, right?

what is
if (file_exists($filename)){
include 'style.css';
include 'header.php';
print $hmm2;
end ($file);
} else {
print "error";
}
supposed to do?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

yeah and then print those lines
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

if (file_exists($filename)){
include 'style.css';
include 'header.php';
print $hmm2;
end ($file);
} else {
print "error";
}

is supposed to check if the file exists and thing print out those things if it does
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ok, if there's no other need for storing the data (I think there is none, since you try to delete it afterwards ;) )why not directly print it to the browser?

Code: Select all

<?php 
$if_name = "file.ext"; // inputfile_name

if (file_exists($if_name)) 
&#123;
	$f_contents = file($if_name); 
	$ln_gt = 62;  // linenumber_greaterthan
	$ln_lt = 4320; // linenumber_lesserthan

	// include 'style.css';  <- uncertain about this
	// include 'header.php';  <- uncertain and that

	for ($ln = $ln_gt+1; $ln < count($f_contents) && $ln < $ln_lt; $ln++)
		print($f_contents&#1111;$ln]);
	print("</table>");
&#125;
else
   printf('file not found: '.$if_name);
?>
the for-condition expresses
if ($array > $a and $array < $b)
but also assures that the loop while not iterate beyond the end of the file if it contained less lines than you expected.
'$ln = $ln_gt+1' | means starting at the next number after $ln_gt ($array > $a)

$ln < count($file) | keep within the bounds
&& $ln < $ln_lt | and '$array < $b'

$ln++ | after each iteration ('print($f_contents[$ln]);') increase the line number by 1
Last edited by volka on Mon Aug 19, 2002 11:07 pm, edited 1 time in total.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

i can print it directly to the browser but i want to store it because i plan on removing specific lines and adding my own later, so that is why i wish to store it.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

and because some of the data is statistics so im going to need that to do a day to day progress type thing. so i would need to store the data. but for now i just want it to store it and read it back.
Post Reply