Click to See Complete Forum and Search --> : Adding new text at the beginning


Siddan
07-13-2006, 10:00 PM
Hey.
This might be difficult, but all in all I have this nice ultra small blog which lets me add new entries from the admin panel, nothing special there.

But when I add a new entry, the latest post will end up last, at the bottom. I would instead like it at the top. New entries are created in one texfile, all in one single row. So all new entries are placed at the end

I viewed the sourcecode and noticed there is a function called "fopen" it is set to a+. I checked what this was all about and apparantly it stands for placing the pointer at the end of the file.
So I changed this to w+ instead to place the pointer at the beginning of the file, but instead it erases all the other text and left is the newest entry only


This is the code:

if (is_writable($latest_news)) {

if (!$handle = fopen($latest_news, 'a+')) {
echo "Error #3";
exit;
}

if (fwrite($handle,smilies("<br><a href =index.php?act=blog&blogid=$blog_identifier>$date $header</a><br>")) === FALSE) {
echo "Error #4";
exit;
}



$latest_news is referred to blog.txt

NogDog
07-13-2006, 10:13 PM
As was discussed in another recent thread here, it might be simplest/cleanest to append entries to the text file (the newest will be at the end). Then, if you want to reverse the order, read the file into an array with the file() function, then reverse the array before outputting it, using the rsort() function.

Siddan
07-14-2006, 04:42 AM
ok great, so it can be done... hehe but how exactly...
I can only modify codes in php, I canīt add a diddly squat tho :)

Siddan
07-16-2006, 06:46 AM
amm i have read about file() and rsort() function but hae no clue how to implement them. When reading how to reverse the order I understood it as ther must be a number generated so there will be something to reverse. But there must be something that will just add the newest text first of the line.

some more help please? :)

Siddan
07-17-2006, 07:02 AM
I still donīt mind some more helping hand

bokeh
07-17-2006, 07:05 AM
Show the piece of code where you read the file into your blog.

Siddan
07-17-2006, 07:10 AM
i think that is it. It handles the latest blog file. Other codes are for the message itself and some bbcode stuff.

Should I upload the whole script?.. only 150 lines.. less if I remove empty lines and html code

Edit:a, sorry, misunderstood you. All the entries are in a blog.txt file. And it just includes the text file

<?php
$act = $_GET['act'];
if ($act == '')
{
include("../extra/smallblog/blog.txt");
}
else
{
include("../extra/smallblog/act/$act.txt");
}
?>


and this is under the /act folder
<?php
$blog_id = $_GET['blogid'];
if ($blog_id == '')
{
include("../extra/smallblog/blog.txt");
}
else
{
include("../extra/smallblog/blog_entries/$blog_id.txt");
}
?>

bokeh
07-17-2006, 07:12 AM
That is the bit that writes to the record file. Post the bit that reads from the record file.

Siddan
07-17-2006, 07:29 AM
i can ******** this a bit...

The new blogentries are only links (date and header), when clicking on a link it takes you to the message, the last part is not interesting in this matter

under admin.php is this imported code:

if (is_writable($latest_news)) {

if (!$handle = fopen($latest_news, 'w+')) {
echo "Error #3";
exit;
}

if (fwrite($handle,smilies("<br><a href =index.php?act=blog&blogid=$blog_identifier>$date $header</a><br>")) === FALSE) {
echo "Error #4";
exit;
}

echo "";

fclose($handle);


Under Blog.php is this:

<?php
$act = $_GET['act'];
if ($act == '')
{
include("../extra/smallblog/blog.txt");
}
else
{
include("../extra/smallblog/act/$act.txt");
}
?>


the previous will include the blog.txt file which have all the entries in one single line. And it will also include /act/blog.txt:

<?php
$blog_id = $_GET['blogid'];
if ($blog_id == '')
{
include("../extra/smallblog/blog.txt");
}
else
{
include("../extra/smallblog/blog_entries/$blog_id.txt");
}
?>

The blog_entries category are all the text files for each entry, which is not interesting in this matter.

I believe those three codes are what is needed to know and perhaps modify one a bit :)

bokeh
07-17-2006, 07:35 AM
Which file is in the wrong order? Is it this one? $latest_news If so post the code that reads that file.

Siddan
07-17-2006, 07:44 AM
yup, latest_news referrs to blog.txt, where all the title links are
This is how one line looks like:

<br><a href =index.php?act=blog&blogid=blog_2006-07-14(11.59)>2006-07-14(11.59) header15</a><br>

Next entry will just add last of the line

bokeh
07-17-2006, 08:14 AM
Ok! Post the code that reads that file.

Siddan
07-17-2006, 08:22 AM
that would be this one. On page Blog.php it includes the text file blog.txt

<?php
$act = $_GET['act'];
if ($act == '')
{
include("../extra/smallblog/blog.txt");
}
else
{
include("../extra/smallblog/act/$act.txt");
}
?>

bokeh
07-17-2006, 08:26 AM
You are not making this clear. What is the filename of the file that is in ascending order?

Siddan
07-17-2006, 08:41 AM
All messages are created with the file name, same as the name created as the header. All messages have its own seperate text file which are saved in the folder Blog_entries.
Those are not interesting

what is interesting is the blog.txt file, where all the headers are stored as links to each of those message text files. In blog.txt the headers are saved in one single row after one and another, like this I posted above

<br><a href =index.php?act=blog&blogid=blog_2006-07-14(11.59)>2006-07-14(11.59) header15</a><br>

That entry is named "header15". Next entry I make will end up after that line. Like when I make a new entry named "header16". Then the line will be something like this:

<br><a href =index.php?act=blog&blogid=blog_2006-07-14(11.59)>2006-07-14(11.59) header15</a><br><br><a href =index.php?act=blog&blogid=blog_2006-07-15(10.47)>2006-07-15(10.47) header16</a><br>

When viewing the page (blog.php) they will be lined up like this:

2006-07-14(11.59) header15

2006-07-15(10.57) header16

..etc

Those are linked to another textfile wich contains the message.. which again is not interesting in this matter, only this line up above.

Since the newest entry is placed last of the line it will show last on the page aswell. But if the newest entry would be placed first in the line, then it would also be displayed first on the page.

I could manually edit the blog.txt file placing the newest entry first in the line to have it display first on the page

Siddan
07-17-2006, 08:54 AM
.. you can view this original (http://www.siddan.net/extra/simplog/index.php) to have an overview. In admin you can add a new entry. Password is pass

this is the blog.txt (http://www.siddan.net/extra/simplog/blog.txt) file. Here you can see all new entries will be added last

bokeh
07-17-2006, 08:57 AM
include("../extra/smallblog/blog.txt");Change that line to this:print(implode(array_reverse(file('../extra/smallblog/blog.txt'))));And stick with the code you posted in post #1

bokeh
07-17-2006, 08:59 AM
Oh dear! New problem! When you append to that file you need to close with a line feed. "\n" like this:fwrite($handle,smilies("<br><a href =index.php?act=blog&blogid=$blog_identifier>$date $header</a><br>\n")

Also convert those <br> elements to <p> and </p>

Siddan
07-17-2006, 09:21 AM
hehe ok,, I tried on the fly after your edited message :)

but still no go. Alltho the lines in blog.txt are seperate, the recent entry doesnīt add on top.

I added your line here as you suggested, in blog.php:

<?php
$act = $_GET['act'];
if ($act == '')
{
print(implode(array_reverse(file("blog.txt"))));
//include("blog.txt");
}
else
{
include("act/$act.txt");
}
?>

bokeh
07-17-2006, 10:45 AM
You are not trying to add the new entry to the top. The whole point is to append the new entry at the end and then reverse the order on display. It does work, I tested the code before I posted it.

Siddan
07-17-2006, 10:57 AM
aha ok cool, but still hmm, then I must have not done it correctly.

there are two different places where this line occurs
include("../extra/smallblog/blog.txt");

Once in Blog.php and once in act/blog.txt

I have changed them both into this:
print(implode(array_reverse(file('../extra/smallblog/blog.txt'))));

and set live break as you mentioned
fwrite($handle,smilies("<p><a href =index.php?act=blog&blogid=$blog_identifier>$date $header</a></p>/n")

bokeh
07-17-2006, 11:06 AM
A line feed is represented wit a backslash. \n

Siddan
07-17-2006, 11:31 AM
lol, yes it is.. I accidentically added it here when posting since I didnīt copy the whole line. But it is correct in the script

Siddan
07-17-2006, 11:33 AM
I was thinking of an easier solution.. perhaps..

when I use the switch r+ it will replace the first line. But what if an empty line could be added first, so next entry will just replace that empty line and then create another empty line before....

bokeh
07-17-2006, 11:58 AM
I was thinking of an easier solution.. perhaps..

when I use the switch r+ it will replace the first line. But what if an empty line could be added first, so next entry will just replace that empty line and then create another empty line before....There's nothing easy about that. I really don't know what your problem is. I just ran this:print(implode(array_reverse(file('http://www.siddan.net/extra/simplog/blog.txt')))); And it prints out your blog file in reverse order. So what exactly is it that you believe is wrong?

Siddan
07-17-2006, 12:16 PM
the word "print" isnīt green in my editor. perhaps there is something wrong there?
this is the code again in blog.php page:

<?php
$act = $_GET['act'];
if ($act == '')
{
print(implode(array_reverse(file('blog.txt'))));
}
else
{
include("act/$act.txt");
}
?>



in this index (http://www.siddan.net/extra/simplog/index.php) page the news2 should be above news1.... but it just wonīt

bokeh
07-17-2006, 12:23 PM
Well I don't know what you are doing wrong but if you run this:<?php

print(implode(array_reverse(file('http://www.siddan.net/extra/simplog/blog.txt'))));

?> It outputs correctly. Looks like you are changing the wrong bit of code.

Siddan
07-17-2006, 12:31 PM
you know what... I pasted that code in a new blank page and it displayed the order correctly. There must be some other code that is preventing this from working thoroughly.

Siddan
07-17-2006, 12:38 PM
yes ok, I was the stupid one in this last section...

I was blind and only looked at blog.php, but the main index file is index.php and it is there I should change that code. I always thought somehow the index page only included the blog.php file ... well well

thanks a bunch and thanks again for your patience for bearing with me :)