News using text files

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
downhalo
Forum Newbie
Posts: 3
Joined: Mon Jun 24, 2002 4:21 am
Location: The States
Contact:

News using text files

Post by downhalo »

I want to make a news page using text files, each new line bieng a variable (title, name, date, author etc...).
I have the display part down, but i am making a submit page, and here's where the trouble is:
I want my script to check if a file exists or not, starting with n000.nwz, and it it exists go on to look at n001.nwz, and if that exists to check if n003.nwz exists and so on until the file doesn;t exist, create a new file called n004.nwz (or whataver the next number is) and add the appropriate info.


Code:

Code: Select all

$file = "news\\n000.nwz";
$i = 0;
while (file_exists($file)) {
$i++;
if ($i < 10) &#123;
$i_o = $i;
$i = 0;
$i .= $i_o;
&#125;
if ($i < 100) &#123;
$i_o = $i;
$i = 0;
$i .= $i_o;
&#125;
$file = "news&quote;;
$file .= "n";
$file .= "$i";
$file .= ".nwz";
&#125;
if (!file_exists($file)) &#123;
$news = "
$HTTP_POST_VARS&#1111;title]
$HTTP_POST_VARS&#1111;date]
$HTTP_POST_VARS&#1111;time]
$HTTP_POST_VARS&#1111;name]
$HTTP_POST_VARS&#1111;body]
";
$filepath = fopen ($file, "w+");
fputs ($filepath, $news);
fclose ($filepath);
echo "Created $file";
&#125;
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

you could do...

Code: Select all

$news_id=1;  //i'd probably start at 1, not zero

while(file_exists("n".sprintf("%03d",$news_id).".nwz")) &#123;
    $news_id++;
&#125;

echo '$news_id = '.$news_id;
only problem, is that if you have 100 articles, and number 47 gets deleted for some reason, the next new one will be 47 instead of 101. A fix to that would be to get directory contents, sort by name, and then parse the name to get the highest news_id instead of this method here. if this doesn't seem like a possible risk, then the above method will work fine.
downhalo
Forum Newbie
Posts: 3
Joined: Mon Jun 24, 2002 4:21 am
Location: The States
Contact:

Post by downhalo »

i don't really unstand what you did in the script, and where would i put the "create file" code?
I won't be deleting news, either, if that simlifies things.
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

sorry, i just did an echo instead of something else... how bout a quick run-through....

first, set you id variable to 1

Code: Select all

$news_id=1;  //i'd probably start at 1, not zero

sprintf("%03d",$news_id) will make the number three digits and pad it with zeroes. therefore "1" becomes "001".
i concatenate the strings together.... "n" . "001" . ".nwz"
and then check to see if that file exists

Code: Select all

while(file_exists("n".sprintf("%03d",$news_id).".nwz")) &#123;
if it does, then increment by one, and try it again

Code: Select all

$news_id++;
&#125;
that will continue to loop until it comes to a filename that doesn't exist. whatever that filename is, is what you want to name your new news file.

to show what that number is, i just printed it out. this is where you would place your code to create the new file.

Code: Select all

echo '$news_id = '.$news_id;

to test this, put three text files in a directory named n001.nwz, n002.nwz, and n003.nwz. then run this script.... it should print out "$news_id = 4".
Post Reply