Jump to content

Spliting data into pages


dcole07

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?
Link to comment
Share on other sites

  • 2 years later...
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.
Link to comment
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:

[code]
<?php
$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];
?>
[/code]

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