Hello,
I am so close on this one. I just need a tiny weenie bit of input from a clever PHP developer and I can do the rest.
I have a site that returns data from a txt file. The problem is that there is so much of this data that I need it to be paginated. I have two php files that do both of these functions perfectly, but my problem is combining them.
I have created a .txt file that I am using as a Flat File Database. I specified that it is | deliminated (however, I am only using one value separated by line breaks, so there aren't acctually any |'s in the .txt file). Here is an extract of flatfiledatabase.txt
<!-- Comment1 --><div class="class"><a href="http://www.url.com/1" target="blank"><img src="http://www.url.com/image1.jpg" border="0"></a></div>
<!-- Comment2 --><div class="class"><a href="http://www.url.com/2" target="blank"><img src="http://www.url.com/image2.jpg" border="0"></a></div>
<!-- Comment3 --><div class="class"><a href="http://www.url.com/3" target="blank"><img src="http://www.url.com/image3.jpg" border="0"></a></div>
<!-- Comment4 --><div class="class"><a href="http://www.url.com/4" target="blank"><img src="http://www.url.com/image4.jpg" border="0"></a></div>
...
You get the idea.
I have found a code that will load this database and echo all of this html (using a simple loop) to a browser and it works like a treat.
<?php
/* Open the file */
$fp = fopen('flatfiledatabase.txt','r');
if (!$fp) {echo 'Error: Unable to open txt file.'; exit;}
/* Loop started to read the txt file to the end */
while (!feof($fp)) {
$line = fgets($fp, 1024);
list ($fp) = split ('\|', $line);
echo ''.$fp.'';
/* 'file pointer' incremented by one, so the loop will start reading from the next line of the database. */
$fp++;
/* End the loop */
}
/* Close the file */
fclose($fp);
?>
This returns the entire database to one page. The problem I face is that I need this data to be paginated.
I have found the following PHP code on the web that illustrates exactly what I need.
<? $data = array(
1 => array('user' => 'Bob', 'comment' => 'I like your site!'),
2 => array('user' => 'George', 'comment' => 'I like your site more than he does!'),
3 => array('user' => 'Tom', 'comment' => 'I like your site, but mine\'s better.'),
4 => array('user' => 'Alex', 'comment' => 'This site is awful. Mine is MUCH better.'),
5 => array('user' => 'Admin', 'comment' => 'Stop insulting month!!!'),
6 => array('user' => 'Harry', 'comment' => 'Woof!'),
);
$perpage = 2; //Obviously you would want to change this to something higher, depending on the content.
if(isset($_GET['start'])) $start = $_GET['start']; else $start = 0;
$numposts = count($data);
$data = array_slice($data, $start, $perpage);
foreach($data as $k => $v)
{
echo $data[$k]['user'].' said: '.$data[$k]['comment'].'<br/>'."\n";
}
if($start > 0)
{
$text .= '<a href="index.html?start='.($start - $perpage).'">< Previous Page </a>';
}
if($start > 0 && $numposts > $perpage && $start < $numposts - $perpage)
{
$text .= ' | ';
}
if($numposts > $perpage && $start < $numposts - $perpage)
{
$text .= '<a href="index.html?start='.($start + $perpage).'">Next Page ></a>';
}
echo $text;
?>
This code works perfectly too. So all I need to do is implement the paginated viewing illustrated in this PHP file to the loading functionality of the first PHP file.
Any ideas are much appreciated.
Merry Christmas & A Happy New Year,
David Apple