superkingkong Posted April 2, 2009 Share Posted April 2, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/ Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799123 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 hi, thanks for the reply. it is displaying according to the $Entries. Where can i get the link to the following page? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799138 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799140 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 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 Sorry, i'm not good at programming. Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799151 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 somehow, when i type the url?page=2 ..., it is still showing the same page. Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799169 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 did you update the code for the page ? ie $Page = (int)(!empty($_GET['page']))?$_GET['page']:1; Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799360 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799395 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 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), Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799403 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799414 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799443 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799883 Share on other sites More sharing options...
MadTechie Posted April 2, 2009 Share Posted April 2, 2009 oops change $end = ($end > $NumRecords)?$NumRecords:$end; $start = $end-$Entries; to $start = $end-$Entries; $end = ($end > $NumRecords)?$NumRecords:$end; aka just swap them Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799898 Share on other sites More sharing options...
superkingkong Posted April 2, 2009 Author Share Posted April 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799905 Share on other sites More sharing options...
superkingkong Posted April 3, 2009 Author Share Posted April 3, 2009 i can't edit my previous post. i have managed to figured out the previous/next link. but i'm stuck with the.. when i'm on page 3, page 3 will not be hyperlinked. would you be able to help, please? thank you. Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799969 Share on other sites More sharing options...
redarrow Posted April 3, 2009 Share Posted April 3, 2009 can you post your current code then we can not see it can we? Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-799984 Share on other sites More sharing options...
superkingkong Posted April 3, 2009 Author Share Posted April 3, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-800014 Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 try this // create intermediate links for ($n = 1; $n <= $totalPages; $n++) { //Add the if($n != $Page) if($n != $Page) echo "<a href='?page=".$n."' >$n</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-800119 Share on other sites More sharing options...
superkingkong Posted April 3, 2009 Author Share Posted April 3, 2009 thank you very very much Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-800164 Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 Solved ? Quote Link to comment https://forums.phpfreaks.com/topic/152159-solved-pagination-on-flat-database-please-help/#findComment-800165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.