Forum Moderators: coopster
<?php$startPoint=0;
$filename = "data.txt"; //here's the filename
$id = fopen($filename, "r"); //open the file
$esc = chr(92);
while ($data = fgetcsv($id, filesize($filename), "¦")) //start a loop
$table[] = $data; //put each line into its own entry in the $table array
fclose($id); //close file
echo "<table border=1>\n";
$npp=5;
$count=0;
foreach($table as $row)
{
while($count<$npp)
{
echo "<tr>";
echo '<td>'.$row[0].'</td>';
echo '<td>'.$row[1]." $count </td>";
echo "</tr>\n";
$count++;
}
}
echo "</table>\n";
var_dump($table);
?>
What I want to do is load a certain number of the database rows per page. The number is to be parsed as a varible through the query string. I'd also like to be able to change how many rows are shown through a fixed varible in the page I can change were relivant.
As for the rest, I would not read the whole array in and then display it. Just use an offset so that you can start and stop output. This way you only select needed data and only touch it once.
<?php
$start = $_GET['start']; // obviously this is where we start the display,
if (!is_numeric($start) ¦¦ $start < 1) $start = 1; // testing our GET string
$npp = 5; // 5 results per page
$count = 1; // used to count our displayed rows, initialized at 1 instead of 0
$end = $startPoint + $npp - 1; // this will tell us when to stop displaying records
$filename = "data.txt";
$id = fopen($filename, "r");
echo "<table border=1>\n"; // start the table
while ($data = fgetcsv($id, filesize($filename), "¦")) {
if ($count >= $start && $count < $end) {
echo "<tr>";
echo '<td>'.$row[0].'</td>';
echo '<td>'.$row[1]." $count </td>";
echo "</tr>\n";
}
$count++;
}
echo "</table>\n"; // close the table
fclose($id);
?>
then you can pass next and previous values by doing a little math on the $start and $end variables. Also remember to always replace ¦ characters with real pipe characters.
I cant figure out how to get it get each varible from the string. With some chopping and changing using the old code I go it to print some of it out, but it still wasn't doing it correct.
Also it would print the first line, the first line and the second line, the first line, the second line then the third line each time the while loop cycled.
$count++ would only print every second line, so I tried $count+1 and it would print everything once instead of the 1st,1st-2nd,1st-2nd-3rd, but still only half the amount of entries requested.
Have I missed something?
The data format in the data.txt looks like this if it's any help.
line1¦info1¦info2¦info3
line2¦info1¦info2¦info3
line3¦info1¦info2¦info3
line4¦info1¦info2¦info3
line5¦info1¦info2¦info3
line6¦info1¦info2¦info3
Where "lineX" can be a alpha numerical code and not sequential numbers. ( I plan on sorting the data.txt when adding new entries whihc is currently done with an admin page. )
Thanks for commenting the code you gave me aswell, helps alot towards understanding PHP better. ( Yes I'm only just getting into it. )
<?php
$start = $_GET['start']; // obviously this is where we start the display,
if (!is_numeric($start) ¦¦ $start < 1) $start = 1; // testing our GET string
$npp = 5; // 5 results per page
$count = 1; // used to count our displayed rows, initialized at 1 instead of 0
$end = $startPoint + $npp - 1; // this will tell us when to stop displaying records
echo "<p>start: ",$start," - end: ",$end;
$filename = "testfile.txt";
$id = fopen($filename, "r");
echo "<p>";
//echo "<p><table border=1>\n"; // start the table
while ($data = fgetcsv($id, filesize($filename), "¦")) {
if ($count >= $start && $count <= $end) {
// echo "<tr>";
// echo '<td>'.$data[0].'</td>';
// echo '<td>'.$data[1]." $count </td>";
// echo "</tr>\n";
echo "<br>",$data[0]," : ",$data[1]," - count: $count";
}
$count++;
}
//echo "</table>\n"; // close the table
fclose($id);
?>
i made a couple changes because i made some stupid errors but it seems to work as expected.
So I dont have to open it again where & how would I get the total amount of entires/lines in the file?
I tried count($filename) right after it was opened but it kept giving me 0 or 1. Going to use this to calculate how many page's it will take to display all the lines in the file to make links automatically.
$lines = file($filename);
$numlines = count($lines);
but if you did that it reads the whole file into an array, which is fine, but I would then use that array for output and get rid of the fgets and fopen stuff.
another thought I had was breaking out of the while loop once you have finished outputting the necessary lines instead of wasting process on finishing read through the file/array.
maybe something like
if ($count >= $start && $count <= $end) {
echo "<tr>";
echo '<td>'.$data[0].'</td>';
echo '<td>'.$data[1]." $count </td>";
echo "</tr>\n";
} else if ($count > $end) {
break;
} >> wrote basic TicTacToe for Console
I remember doing that :)