Jump to content

[SOLVED] Pagination on flat database? please help


superkingkong

Recommended Posts

Hi guys,

 

I have a flat text database file with about 200 over records. How can i paginate them?

 

Appreciate if you can help me out, thanks.

 

Below is the script to get the records from the file

 

<?php
// Open log file
$logfile = "log.db";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r");
$log = fread($handle, filesize($logfile));
fclose($handle);
} else {
die ("The log file doesn't exist!");
}

// Seperate each logline
$log = explode("\n", trim($log));
// After that it may be useful to get each part of each logline in a separate variable.
// This can be done by looping through each logline, and using explode again:

// Seperate each part in each logline
for ($i = 0; $i < count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|', $log[$i]);
}

echo "<p>Total Entries : " . count($log) . "</p>";

// Show a table of the logfile
echo '<style type="text/css">
body {
background-color: #000;
color: #ffff80;
text-align: center;
font-family: "Trebuchet MS", "Times New Roman", Arial, Times, serif;
font-size: 13px
}

.wrapper {
width: 90%;
text-align: center;
margin: 0 auto;
}

a {
color: #33ccff;
text-decoration: underline;
}

a:hover {
text-decoration: none;
}

img {
border: 0;
}

.italic {
font-style:italic;
}

table {
width: 100%;
border-collapse: separate;
text-align: center;
margin: 0 auto;
font-size: 13px
}

td {
background-color: #0f0f4f;
padding: 3px;
border: 1px ridge #ccc
}

td.a {
width: 12%;
}

td.b {
width: 32%;
}

td.c {
width: 56%;
}

td.d {
background-color: #000;
}

</style>';
echo '<div class="wrapper"><table>';
foreach ($log as $logline) {
if ($logline['1'] == '') {
	echo '<tr>';
	echo '<td colspan="2"><b>' . $logline['2'] . '</b></td>';
	echo '<td class="c"><span class="italic">' . htmlspecialchars(urldecode($logline['5'])) . '</span></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td class="a"><b>IP Address</b><br /><span class="italic">' . $logline['0'] . '</span></td>';
	echo '<td class="b"><b>Hostname</b><br /><span class="italic">' . $logline['4'] . '</span></td>';
	echo '<td class="c"><b>Browser/OS</b><br /><span class="italic">' . $logline['3'] . '</span></td>';
	echo '</tr>';
	echo '<tr><td class="d" colspan="3"></td></tr>';
} else {
	echo '<tr>';
	echo '<td class="a"><a href="' . htmlspecialchars(urldecode($logline['1'])) . '"><b>Referer</b></a>';
	echo '<td><b>' . $logline['2'] . '</b></td>';
	echo '<td class="c"><span class="italic">' . htmlspecialchars(urldecode($logline['5'])) . '</span></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td class="a"><b>IP Address</b><br /><span class="italic">' . $logline['0'] . '</span></td>';
	echo '<td class="b"><b>Hostname</b><br /><span class="italic">' . $logline['4'] . '</span></td>';
	echo '<td class="c"><b>Browser/OS</b><br /><span class="italic">' . $logline['3'] . '</span></td>';
	echo '</tr>';
	echo '<tr><td class="d" colspan="3"></td></tr>';
}
}
echo '</table></div>';
?>

your do it something like this

<?php
$Page = 1;
$Entries = 30;

// Open log file
$logfile = "log.db";

if (file_exists($logfile))
{
#$handle = fopen($logfile, "r");
#$log = fread($handle, filesize($logfile));
#fclose($handle);
$fulllog = file($logfile);
} else {
die ("The log file doesn't exist!");
}

// Seperate each logline
#$log = explode("\n", trim($log));
// After that it may be useful to get each part of each logline in a separate variable.
// This can be done by looping through each logline, and using explode again:



$totalPages = ceil(count($log)/$Entries);
$end = $Entries*$Page;
$start = $end-$Entries;

// Seperate each part in each logline
$log = array();
for ($i = $start; $i < ($end); $i++)
{
$log[$i] = trim($fulllog[$i]);
$log[$i] = explode('|', $fulllog[$i]);
}

echo "<p>Total Entries : " . count($fulllog) . "</p>";

// Show a table of the logfile
//rest of your code

 

if your using $_GET then just do a help like for "?Page=".($Page-1) and "?Page=".($Page+1) for previous and next

if you want a full page number list then just create loop from 1 to $totalPages with a simple echo "<a href='?page=".$n."' >$n</a>";

if your using $_GET then just do a help like for "?Page=".($Page-1) and "?Page=".($Page+1) for previous and next

if you want a full page number list then just create loop from 1 to $totalPages with a simple echo "<a href='?page=".$n."' >$n</a>";

 

Hi,

 

i did something like this

 

for ($n = 1; $n <= $totalPages; $n++)
{
echo "<a href='?page=".$n."' >$n</a>";
}

 

i think it's wrong, because it's not displaying anything :P

 

Sorry, i'm not good at programming.

did you update the code for the page ?

ie

$Page = (int)(!empty($_GET['page']))?$_GET['page']:1;

 

no, I just used the same code on post#2 as you posted.

 

where do i need to update this code?

$Page = (int)(!empty($_GET['page']))?$_GET['page']:1;

?

 

pardon me for my lack of knowledge in php.

 

update:

 

i managed to get the link (page number) to display after i change one of the variable:

 

$totalPages = ceil(count($fulllog)/$Entries);

 

is it correct?

did you update the code for the page ?

ie

$Page = (int)(!empty($_GET['page']))?$_GET['page']:1;

 

no, I just used the same code on post#2 as you posted.

 

where do i need to update this code?

$Page = (int)(!empty($_GET['page']))?$_GET['page']:1;

?

 

Replace the first $Page with the code above

 

pardon me for my lack of knowledge in php.

No worries your doing great,

 

update:

 

i managed to get the link (page number) to display after i change one of the variable:

 

$totalPages = ceil(count($fulllog)/$Entries);

 

is it correct?

Yep very correct.. i missed that (was untested),

thanks, the pagination is working, but there is a slight problem.

 

for eg, i have 55 records, so, it displays 6 pages (10 records/page)

 

on the 6th page (last page), after the last 5 records, the script continues to display the "title of the column" until it reaches "10" (the count of $Entries), without the value, since the record ends at 55, if u know what i mean.

 

example is here:

http://premium.sfdns.net/~superkin/logv.php?page=6

 

anyway to prevent that?

try this update

change

$totalPages = ceil(count($fulllog)/$Entries);
$end = $Entries*$Page;
$start = $end-$Entries;

to

$NumRecords = count($fulllog);
$totalPages = ceil($NumRecords/$Entries);
$end = $Entries*$Page;
$end = ($end > $NumRecords)?$NumRecords:$end;
$start = $end-$Entries;

try this update

change

$totalPages = ceil(count($fulllog)/$Entries);
$end = $Entries*$Page;
$start = $end-$Entries;

to

$NumRecords = count($fulllog);
$totalPages = ceil($NumRecords/$Entries);
$end = $Entries*$Page;
$end = ($end > $NumRecords)?$NumRecords:$end;
$start = $end-$Entries;

 

thanks.

 

it is still displaying 10 records on the last page. but right now, it is getting last five values from page 5th, and repeat it in page 6 as first five values, then follow by the real five values of page 6th as the last five values.

thanks a lot :)

it is displaying the right thing :)

 

about the page link,

 

instead of  1 2 3

 

do u know how can i display something like

 

1 2 3 ... Next > Last Page >>

or the < Previous and << first page?

 

and if i'm on page 3, page 3 will not be hyperlinked.

 

thank you very much :)

sure... thanks for the help

 

<?php
$Page = (int)(!empty($_GET['page']))?htmlspecialchars($_GET['page']):1;
$Entries = 20;

// Open log file
$logfile = "log.db";

if (file_exists($logfile))
{
$fulllog = file($logfile);
} else {
die ("The log file doesn't exist!");
}

$NumRecords = count($fulllog);
$totalPages = ceil($NumRecords/$Entries);
$end = $Entries*$Page;
$start = $end-$Entries;
$end = ($end > $NumRecords)?$NumRecords:$end;

// Seperate each part in each logline
$log = array();
for ($i = $start; $i < ($end); $i++)
{
$log[$i] = trim($fulllog[$i]);
$log[$i] = explode('|', $fulllog[$i]);
}

echo "<p>Total Entries : " . count($fulllog) . "</p>";

echo "<br />";

// create first page link

if($Page>2) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($Page==1).'"><< First</a>  ';
} else {
echo '<< First  ';
}

// create previous link

if($Page>1) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($Page-1).'">< Prev</a> ';
} else {
echo '< Prev ';
}

// create intermediate links

for ($n = 1; $n <= $totalPages; $n++) {
   echo "<a href='?page=".$n."' >$n</a>";
}

// create next link

if($Page<$totalPages) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($Page+1).'">Next ></a>  ';
} else {
echo 'Next >  ';
}

// create last page link

if($Page<$totalPages && $Page!=($totalPages-1)) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($totalPages).'">Last >></a>';
} else {
echo 'Last >>';
}

echo '<p></p>';

// Show a table of the logfile
echo '<style type="text/css">
body {
background-color: #000;
color: #ffff80;
text-align: center;
font-family: "Trebuchet MS", "Times New Roman", Arial, Times, serif;
font-size: 13px;
}

.wrapper {
width: 90%;
text-align: center;
margin: 0 auto;
}

a {
color: #33ccff;
text-decoration: underline;
}

a:hover {
text-decoration: none;
}

img {
border: 0;
}

.italic {
font-style:italic;
}

table {
width: 100%;
border-collapse: separate;
text-align: center;
margin: 0 auto;
font-size: 13px;
}

td {
background-color: #0f0f4f;
padding: 3px;
border: 1px ridge #ccc;
vertical-align: top;
}

td.a {
width: 12%;
}

td.b {
width: 32%;
}

td.c {
width: 56%;
border: 1px ridge #ccc;
}

td.d {
background-color: #000;
}

</style>';
echo '<div class="wrapper">';

echo '<table>';
foreach ($log as $logline) {
if ($logline['1'] == '') {
	echo '<tr>';
	echo '<td colspan="2"><b>' . $logline['2'] . '</b></td>';
	echo '<td class="c"><span class="italic"> ' . htmlspecialchars(urldecode($logline['5'])) . '</span></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td class="a"><b>IP Address</b><br /><span class="italic">' . $logline['0'] . '</span></td>';
	echo '<td class="b"><b>Hostname</b><br /><span class="italic">' . $logline['4'] . '</span></td>';
	echo '<td class="c"><b>Browser/OS</b><br /><span class="italic">' . $logline['3'] . '</span></td>';
	echo '</tr>';
	echo '<tr><td class="d" colspan="3"></td></tr>';
} else {
	echo '<tr>';
	echo '<td class="a"><a href="' . htmlspecialchars(urldecode($logline['1'])) . '"><b>Referer</b></a></td>';
	echo '<td class="b"><b>' . $logline['2'] . '</b></td>';
	echo '<td class="c"><span class="italic"> ' . htmlspecialchars(urldecode($logline['5'])) . '</span></td>';
	echo '</tr>';
	echo '<tr>';
	echo '<td class="a"><b>IP Address</b><br /><span class="italic">' . $logline['0'] . '</span></td>';
	echo '<td class="b"><b>Hostname</b><br /><span class="italic">' . $logline['4'] . '</span></td>';
	echo '<td class="c"><b>Browser/OS</b><br /><span class="italic">' . $logline['3'] . '</span></td>';
	echo '</tr>';
	echo '<tr><td class="d" colspan="3"></td></tr>';
}
}
echo '</table></div>';
?>

Archived

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

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