Jump to content


Photo

Spliting data into pages


  • Please log in to reply
2 replies to this topic

#1 dcole07

dcole07
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 01 March 2006 - 06:13 PM

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?

#2 ken311

ken311
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 April 2008 - 02:51 AM

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.
--
Ken
http://www.dzineit.net - Web Development

#3 Fadion

Fadion
  • Members
  • PipPipPip
  • Advanced Member
  • 1,710 posts
  • LocationAlbania

Posted 30 April 2008 - 03:07 AM

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:

<?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];
?>

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users