Jump to content

Pagination


phpsycho

Recommended Posts

I'm working on a flat file database that I can search.

I got most of it done, but wondering how I can paginate the results..

 

<?php
ini_set('display_errors', "On");
$count = 0;
$time_start = microtime(true);
$handle = fopen('/var/txtdb/links.txt', "r"); 
if ($handle) {
   while (!feof($handle) && $count < 15) { 
        $line = fgets($handle, "4096");
        $pos = strpos($line, "keyword");
if ($pos == true) {
		preg_match('~\[desc = ([^\]]*)\]~is', $line, $desc);
		echo "$desc[1]<br>";
		$count++;
}

   } 
   fclose($handle); 

} 

$time_end = microtime(true);
$time = $time_end - $time_start; 

echo '<br>Script took '.$time.' seconds to execute';
?>

 

So I want to display 15 results per page.. I know I could just loop through all the results each page just start from the last result +15 more. but.. that would take too long considering the amount of data I have.

So is there a way to start the loop at a specific line? That way I won't have to loop through them all and waste time and slow things down..

Link to comment
Share on other sites

Well I am using MySQL currently, but when I search anything it takes about 18 seconds for the results to load lol and I am not too enthused about that.

Soo I tried doing this method and its super fast soo I thought I would stick with it.

But I suppose I could loop through all lines to search.. just have more than one file with data in it..(each file will contain 3 million lines of data) so some how search through the first file and when I get to the last 'page' have it switch files and so on?

Is that a good idea you think? and any idea of a simple way to code that?

Link to comment
Share on other sites

fseek is the only method of randomly accessing data in a file (unless you can read the whole file into an array.) To use fseek to accomplish pagination, you would either need to maintain an 'index' of your file with the starting position of each line so that you can easily find where each line is at or you need to make every line the same length so that you can calculate the starting position of each line.

 

Edit: If the make each entry in the index file the same length, you can easily and simply calculate where to fseek into the index file to find the start/end positions of the data you want in the data file.

 

 

Link to comment
Share on other sites

The above description is essentially what a database engine does for you without you needing to write and test any code. If your database is properly designed and with appropriate table indexes and you are using the database engine to do the searching, you can expect a database driven solution to operate faster than a flat file solution. You should first probably find out why your database searches are taking that long.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.