Writing to the top of a text file and paging issue help

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
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Writing to the top of a text file and paging issue help

Post by pallarisari »

Hi,

Is there a way to write to the top of a text file rather than to the bottom. I need to display my latest post at the top of the page. Currently it display this last.

Also i have a paging system in place which works, although it has a small downfall.
It creates page links but unfortuntely doesnt truncate them to 1 2 3....9 10 11 for example and rather display then as 1 2 3 4 5 6 and so on.

does anyone now a fix for this?
heres my code

Code: Select all

<?php
include("header.inc");

$page = $_GET['page'];
if ($page == "") { $page = 1; }
$fwd = $page - 1;
$rwd = $page +1;

// Setting the default values for number of records per page -------------------------
$perpage = 100;
$filename = "data.php";

// Reading in all the records, putting each guestbook entry in one Array Element -----

$fd = fopen ("data.txt", "r");
while (!feof ($fd)) 
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

// Counting the total number of entries (lines) in the data text file ----------------

$result = count($lines);
$count = $result-1;

// Caclulate how many pages there are ----------------------------------------

if ($count == 0) { $totalpages = 0; }
else { $totalpages = intval(($count - 1) / $perpage) + 1; }

$end = ($page * $perpage) - 1;
$start = $end - ($perpage-1); if ($start <= 1) { $start = 0; }


if ($start < 0) { $start = 0; }

for ($i = $start; $i<=$end; $i++) 
{
	echo (stripslashes($lines[$i]));
}

echo "<center>";

// Creating the Forward and Backward links -------------------------------------

if ($fwd > 0 && $rwd > 0 && $rwd<$totalpages+1)
{
echo "<br><a href=\"$filename?page=$fwd\">&lt&lt</a>";
echo "<a href=\"$filename?page=$rwd\">&gt&gt</a><br>";
}
else if ($fwd == 0)
{ echo "<a href=\"$filename?page=$rwd\">&gt&gt</a><br>"; }
else if ($rwd == 0)
{ echo "<br><a href=\"$filename?page=$fwd\">&lt&lt</a>"; }
else if ($rwd == $totalpages+1)
{ echo "<a href=\"$filename?page=$fwd\">&lt&lt</a><br>"; }


for ($i = 1; $i<=$totalpages; $i++)
{
echo " [<a href=\"$filename?page=$i\">$i</a>] ";
}
echo "</center>";


?>
<?php
include("footer.inc");
?>


thx
ari
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've told you already.. if you read from the file correctly, you should be able to use usort() to order the posts in any manor.

As for your page number "issue" look through the pagination tutorials available online.
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

thanks ill look into it.
thought it might have been easier to read into the top and skip the sorting array :cry:
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

i created a sorting array. which loads via two php pages. data.php loading as is posted in the text file and data2.php loads information from the bottom up. Unfortuantely the results are too unpredicatable.

Do you know a way of writing to the top of a text file??
or possibly script which i may use instead of my own

thanks
ari
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

typically, it's done by writing the new data to a temporary file, copying in all old data, deleting the old file, and renaming the temporary to the original name.

You will have to be VERY careful of concurrent changes to the file because of this operation. It is often, not so good to prepend to a file because of the actions required. Even reading in the original file, prepending to that string and writing out can have memory problems. This is why data is often appended and sorted on the fly.
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

i see, thats definately something i dont want to do.
So a reverse array seems the only way to go. Are there any tutorials i cud use for this / code i can view and modified?

ta
ari
Post Reply