Forum Moderators: coopster

Message Too Old, No Replies

Loading only so many rows

         

Sypher_5

8:45 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Here's what I've got working so far.

<?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.

jatar_k

9:23 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, you have the number per page ($npp) already in there so I am not sure what you want to change there. Unless you mean having it in another file as opposed to this one. You could always pull it into a conigure.php page and then include that in your script, if thats what you meant.

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.

Sypher_5

10:12 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Thanks :)

Sypher_5

9:03 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



Hmm, ok I'm getting some problems with this.

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. )

jatar_k

10:01 pm on Jul 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I used this as a debug version

<?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.

Sypher_5

10:35 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



Thanks Again.

jatar_k

12:07 am on Jul 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



oh also

$count++ and $count+1

are exactly the same thing so that wouldn't cause any problems. ;)

Sypher_5

11:32 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Yeah I realised that, I just finished some C++ on my Game Development Course ( wrote basic TicTacToe for Console ) but I wondered if something was messing up somewhere.

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.

jatar_k

2:38 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could use

$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 :)

Sypher_5

11:00 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



Wouldn't I need to define $data somewhere?

jatar_k

5:17 pm on Jul 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



while ($data = fgetcsv($id, filesize($filename), "¦")) {

it would be in there so $data would be the read line

Sypher_5

9:32 pm on Jul 21, 2004 (gmt 0)

10+ Year Member



Thanks, you've been much help :)