• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

fwrite

Wojtek

W as in Whisky
NLC
PHP:
if($_POST['do']=='addnew') {
	$title=$_POST['title'];
	$news=$_POST['news'];
	$fp=fopen(NEWS_FILE,"a");
	$formatted=str_replace("\r\n","<br>",$news);
	$formatted=str_replace("\n","<br>",$formatted);
	$newsto=date("d M Y")."%~#".$title."%~#".$formatted;
	fwrite($fp,StripSlashes($newsto)."\n");
	fclose($fp);
	echo '<center>-- news added --</center>';
}
?>

This ads the content to the end of the file.
What if I want it to be added at the begining?

I've been thinking that putting the content of the whole file in a va, clearing the file, writing the thing to write, and then write the var content, so the actual content to ad is at the begining.

However, wont this be problematic if the file is 50, 100, 400kb in size? can a var hold that much?

Help please :)
 
Im pretty sure if you open it with w it will be at the beginning of the file. If you need to reset to beginning after read/write, use rewind();
 
Ok,

I took a look at that site an "r+" corresponds to what I need. (Open for reading and writing; place the file pointer at the beginning of the file.)

However, now Im having a problem with the news file.


Write news PHP
PHP:
	$fp=fopen(NEWS_FILE,"r+");
	$formatted=str_replace("\r\n","<br>",$news);
	$formatted=str_replace("\n","<br>",$formatted);
	$newsto=date("d M Y")."%~#".$title."%~#".$formatted;
	fwrite($fp,StripSlashes($newsto)."\n");


Read news PHP
PHP:
	$startpage=$_GET['page'];
	$xnews=file(NEWS_FILE); 
	$xnews=array_reverse($xnews);
	$startpage-=1;
	$ubound=count($xnews);

	// Next / Prev Buttons

	if($startpage<0 || $startpage>=$ubound/PERPAGE) $startpage=0;

	if (PERPAGE<$ubound)
	if($startpage==0) echo "";
	else	echo "Previous Link";

	if($ubound>PERPAGE)
	for($j=1;$j<=ceil($ubound/PERPAGE);$j++) {
	if($j==$startpage+1)
		echo "";
	else
		echo "";
	}

	if(PERPAGE<$ubound)
	if($startpage+1>=$ubound/PERPAGE) echo "";
	else	echo "Next Link";


	//below we format the news we need and print it to webpage	

	for($i=$startpage*PERPAGE;$i<$startpage*PERPAGE+PERPAGE && $i<$ubound;$i++)
	{
	$crtsplit="";
	$crtsplit=explode("%~#",$xnews[$i]);

	echo "<br><br><table border=\"0\" cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#000000\" width=\"100%\"><tr><td>";
	echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" bgcolor=\"#FF9DD0\" width=\"100%\"><tr><td 	align=left>";
	echo "<font color=\"#ffffff\"><center><b>$crtsplit[1]</b></center></font>";
	echo "</td></tr></table></td></tr></table><br><br>$crtsplit[2]<br><br>";

	}

the news file format is supposed to be:

DATE%~#TITLE%~#CONTENT
DATE%~#TITLE%~#CONTENT



But whenever I add a news using the add news, it always gets overwritten.

------------------------------------------
29 Oct 2003%~#test1%~#test1

------------------------------------------

becomes

------------------------------------------
29 Oct 2003%~#test2%~#test2

------------------------------------------

instead of

------------------------------------------
29 Oct 2003%~#test2%~#test2
29 Oct 2003%~#test1%~#test1
------------------------------------------
 
just store the new info in a var
read the file
append the file info to the new info
re write the file.

BOOM shaka laka.
 
Moving the file pointer around won't accomplish what you're trying to do as you found out; there isn't a way to "append" data besides at the end of a file without doing it in memory.
I think spec meant something like:
$newsto=date("d M Y")."%~#".$title."%~#".$formatted."\n".file_get_contents("newsfile.txt");
file_put_contents("newsfile.txt", $newsto);
(what's in bold is what I added)

My personal opinion is that this won't scale at all though. As the file containing the news item grows, it will take longer and longer to both write and read the news items.
I'm aware that this might never get to a point where it becomes a problem but on shared hosting and a busy site where many people might be browsing through the news little things quickly add up.
 
What would you recomment then?
Mysql?
I went with flat-file because I dont know how to handle sql databases.
 
You could possibly append to the end of the file and just reverse it before printing.
 
agree with corn. the file should be read reversely.

i presume, there is a memory-allocating problem that makes new file-content adding mechanism in front of file pointer unable to reserve the old contents.
 
Originally posted by Wojtek
What would you recomment then?
Mysql?
I went with flat-file because I dont know how to handle sql databases.
yes, go with mysql before you learn too much about flat-file and realize how useless it is.
 
Thanks carebear for the code,
its working fine, except 'file_put_contents' gave me an error so i replaced with the 3 open/write/close.

Working perfectly now :)


Keith, got any websites where I could learn the basics of sql?
 
i should probably rephrase that... it's not useless, just extremely clunky when your database gains any kind of popularity or size.

you could check out php.net and mysql.com
 
My suggestion would be to get comfortable with the general SQL syntax first before anything else. http://sqlcourse.com/ has two fairly good courses building up from basic to more advanced.
http://www.databasejournal.com (which is the same site actually) has a bunch of articles you might want to browse through as well.
http://www.databasejournal.com/features/mysql/archives.php/200201 is a step by step mixed PHP/SQL/MySQL tutorial. The first part only deals with installing everything on your own computer so unless that's something you want to do I'd suggest to skip ahead and start with part two.
 
fusion news or coranto
neither?

look at my 2nd post, thats all the php code.
It's a Very basic code, but does the job ok :)
 
instead of trying to write to the beginning of the file, why not just explode it into an array and use rsort() to reverse the order?
 
Originally posted by keith
instead of trying to write to the beginning of the file, why not just explode it into an array and use rsort() to reverse the order?
Adding a new entry will always take about the same amount of time if you add to the end but reading and parsing the whole thing back and then reverse sorting it will take longer with each new insert.
Since it's for site news you have reads >>> inserts so reads should be cheap which isn't the case here.
If he has MySQL on his hosting account he might as well use it once he gets over the learning curve :).
 
ok carebear...

he obviously wants to show the items in reverse chronological order.

he will write to the end of the file, so the items are stored in chronological order.

if he stores the date as a timestamp (and who wouldn't?), he can use rsort() when he retrieves the info from the file.

this will work because it will reverse sort the data numerically since the date is the first piece of info on each line.

then, using foreach(), he can print out the items however he likes.

yeah, it's a bit inconvenient, but not too much considering he is using flatfile. you have to understand flat-file is not the most efficient or user-friendly way of storing data. got it?

i do agree with you though that he should just drop this and learn mysql. it's not that hard once you get into it.
 
I think you missed my point :)

For the sake of arguement let's say reading one line take 1ms. One line won't take any time at all, but as the number of rows increases so will the time it takes to read the entire file.
Besides that, your script will consume more and more memory as the file grows and you'll suffer an ever increasing overhead from having to sort an array with a larger number of elements (and depending on the algorithm used sorting something that is in the reverse order you want it to be in is the least performant already).

If you want to poke at random values in a flat file you'd get rid of most of the downsides by using a balanced binary tree but the learning curve for that is just a bit steeper then learning MySQL, assuming PHP would even let you build one in a decent manner :)

Then again most people end up doing "SELECT *"'s so I guess in the end it doesn't make that much difference after all :confused2
 
Back
Top