Jump to content


This topic is now archived and is closed to further replies.


Spliting data into pages

Recommended Posts

I've tried to split data into group of 10 from a txt file but I need help

In the txt file each group is one line and I want 10 line to be put into something like ?page#1 page#2...

then it will display 1-10 if the url has ?page#1, 11-20 if the page is #2 ...

What would the script be?

Share this post

Link to post
Share on other sites
Can you give us an example of the text in the text file or the code you are trying?  this is very vague.

You might want to try using:
$array = explode("/r/n",$filecontents);

That will split your contents into an array and then you can use a for loop to manage the values.

Share this post

Link to post
Share on other sites
I imagine the text file is:

data1.1, data1.2, data1.3...
data2.1, data2.2, data2.3...
data11.1, data11.2, data11.3...
data12.1, data12.2, data12.3...

Why not use a database then? It would be way more efficient. Anyway, as suggested above, use explode to have the groups in an array. Seperate each ten group with a word (like: "--next group--") or two brakes to explode them easily. Then depending on the page, show the info. Smth like this:

$handle = fopen('file.txt', 'r'); //open file as read only
$contents = fread($handle, filesize('file.txt')); //read the file
$data = explode('--next group--', $file); //explode the groups
if(isset($_GET['page']) and $_GET['page'] != count($data) + 1){ //the whole block: sets the key of the array depending on the page
     $dataKey = $_GET['page'] - 1;
} else{
     $dataKey = 0;
echo $data[$dataKey];

The idea of all that, is to get the page data and use it as the key of the array. So for page=1, u could show array[0] (as array keys start from 0) and so on.

Share this post

Link to post
Share on other sites


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.