I made a piece of code that reads a text file with the following format and displays the entries with 5 on each page.
...TEXT FILE FORMAT...
john|uk|comment|mail@mail.com|site|date
john2|uk2|comment2|mail@mail.com2|site2|date2
john3|uk3|comment3|mail@mail.com3|site3|date3
john4|uk4|comment4|mail@mail.com4|site4|date4
The following is the code i made. Is there any better way of doing it?
...MY CODE...
<?
if ($page==""){
$page=1;
}
//enter your filename (+ path if necessary) below
$fileName = "users.txt";
$file_pointer = fopen($fileName, "r");
$data = fread($file_pointer, filesize($fileName));
fclose($file_pointer);
$data = ereg_replace("\n", "#", $data);
$disp = ereg_replace("#", "<br>", $data);
$count=0;
$len=strlen($data);//get the length of the file in characters
for($i=0;$i<$len;$i++) {
$chrcheck=$data[$i];
if($chrcheck=="#") {
$count=($count+1);// $count is the number of LINES in the document
}
}
$count=$count+1;
$line=explode("#", $data);
$no = 0;
for($i=0;$i<$count;$i++) {
$echo = $line[$i];
$lenLine=strlen($line[$i]);
if($lenLine < 2) {
$lines=$i;
break;
}
else{
$detail[$no]=explode("|",$line[$i]);
$no = $no + 1;
}
}
$entriesPerPage = 5;
$to = ($page*$entriesPerPage);
$from = ($to - $entriesPerPage);
$from = $from + 1;
if($lines < $from){
echo"No More";
}
if($lines >= $from){
if($lines < $to) {
$to=($to-($to-$lines));
$to=$to+1;
for($i=$from;$i<$to;$i++) {
$j = $i - 1;
$name = $detail[$j][0];
$location = $detail[$j][1];
$comment = $detail[$j][2];
$mail = $detail[$j][3];
$site = $detail[$j][4];
$date = $detail[$j][5];
// THIS ECHO IS YOUR DISPLAY! MUST BE SAME AS BELOW
echo"Entry No. - ${i}<br>Name - ${name}<br>Location - ${location}<br>
Comment - ${comment}<br>E-Mail - ${mail}<br>Website - ${site}<br>
Date - ${date}<br><br>";
}
$prev = $page - 1;
if($prev > 0) {
echo"<a href='ignorelines.php?page=${prev}'>Previous</a> | Next";
}
else {
echo"Previous | Next";
}
}
}
if($lines >= $to){
$to=$from+5;
for($i=$from;$i<$to;$i++) {
$j = $i - 1;
$name = $detail[$j][0];
$location = $detail[$j][1];
$comment = $detail[$j][2];
$mail = $detail[$j][3];
$site = $detail[$j][4];
$date = $detail[$j][5];
// THIS ECHO IS YOUR DISPLAY! MUST BE SAME AS ABOVE
echo"Entry No. - ${i}<br>Name - ${name}<br>Location - ${location}<br>
Comment - ${comment}<br>E-Mail - ${mail}<br>Website - ${site}<br>
Date - ${date}<br><br>";
}
$prev = $page - 1;
$next = $page +1;
if($prev > 0) {
if($next <= $lines){
echo"<a href='ignorelines.php?page=${prev}'>Previous</a> | <a href='ignorelines.php?page=${next}'>Next</a>";
}
}
if($prev > 0){
$n = ($next*$entriesPerPage)-$entriesPerPage;
if($lines < $n) {
echo"<a href='ignorelines.php?page=${prev}'>Previous</a> | Next";
}
}
if($prev < 1){
$n = ($next*$entriesPerPage)-$entriesPerPage;
if($n < $lines) {
echo"Previous | <a href='ignorelines.php?page=${next}'>Next</a>";
}
}
if($prev < 1){
$n = ($next*$entriesPerPage)-$entriesPerPage;
if($n > $lines){
echo"Previous | Next";
}
}
}
?>