tpl41803 Posted July 13, 2009 Share Posted July 13, 2009 Hey everyone, I've been searching endlessly for a pagination script that will work exactly how I want it to. I dont understand PHP well enough to write my own from scratch so I've been working with a few others. The current script I'm using allows me to sort my database by different columns / fields, but I can't get it to paginate and then I came across a script to paginate--here on these forms at http://www.phpfreaks.com/forums/index.php/topic,245920.0.html--but I can't get it to sort my data. my current, paginated code looks like this: <?php $Page = (int)(!empty($_GET['page']))?$_GET['page']:1; $Entries = 5; $logfile = "inventory.txt"; 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!"); } $log = explode("\n", trim($log)); $NumRecords = count($fulllog); $totalPages = ceil($NumRecords/$Entries); $end = $Entries*$Page; $start = $end-$Entries; $end = ($end > $NumRecords)?$NumRecords:$end; $log = array(); for ($i = $start; $i < ($end); $i++) { $log[$i] = trim($fulllog[$i]); $log[$i] = explode('|', $fulllog[$i]); } ?> <?php require_once('head.php'); ?> <div id="content"> <ul class="sort"> <?php echo '<li>'. count($fulllog) .' vehicles found</li>'; // create first page link if($Page>2) { echo '<li><a href="'.$_SERVER['PHP_SELF'].'?page='.($Page==1).'"><< First</a></li>'; } else { echo '<li><< First</li>'; } // create previous link if($Page>1) { echo '<li><a href="'.$_SERVER['PHP_SELF'].'?page='.($Page-1).'">< Prev</a></li>'; } else { echo '<li>< Prev</li>'; } // create intermediate links for ($n = 1; $n <= $totalPages; $n++) { echo "<li><a href='?page=".$n."' >$n</a></li>"; } // create next link if($Page<$totalPages) { echo '<li><a href="'.$_SERVER['PHP_SELF'].'?page='.($Page+1).'">Next ></a></li>'; } else { echo '<li>Next ></li>'; } // create last page link if($Page<$totalPages && $Page!=($totalPages-1)) { echo '<li><a href="'.$_SERVER['PHP_SELF'].'?page='.($totalPages).'">Last >></li></a>'; } else { echo '<li>Last >></li>'; } ?> </ul> <?php foreach ($log as $logline) { if ($logline['1'] == '') { echo ' <div id="container"> <a href=' . $logline['1'] . '><img src="inventory/thumbs/' . $logline['0'] . ' alt="' . $logline['2'] . '" title="' . $logline['2'] . '" /></a> <h2><a href=' . $logline['1'] . '>' . $logline['2'] . '</a></h2> <ul> <li><b>Year:</b> ' . $logline['3'] . '</li> <li><b>Make:</b> ' . $logline['4'] . '</li> <li><b>Model:</b> ' . $logline['5'] . '</li> <li><b>Color:</b> ' . $logline['7'] . '</li> </ul> <ul class="right"> <li><b>MPG <small>[City/Hwy]</small>:</b> ' . $logline['6'] . '</li> <li><b>VIN:</b> ' . $logline['8'] . '</li> <li><b>Price:</b> $' . $logline['9'] . '</li> <li><b>Comment:</b> ' . $logline['11'] . '</li> </ul> </div> '; } else { echo ' <div id="container"> <a href=' . $logline['1'] . '><img src="inventory/thumbs/' . $logline['0'] . ' alt="' . $logline['2'] . '" title="' . $logline['2'] . '" /></a> <h2><a href=' . $logline['1'] . '>' . $logline['2'] . '</a></h2> <ul> <li><b>Year:</b> ' . $logline['3'] . '</li> <li><b>Make:</b> ' . $logline['4'] . '</li> <li><b>Model:</b> ' . $logline['5'] . '</li> <li><b>Color:</b> ' . $logline['7'] . '</li> </ul> <ul class="right"> <li><b>MPG <small>[City/Hwy]</small>:</b> ' . $logline['6'] . '</li> <li><b>VIN:</b> ' . $logline['8'] . '</li> <li><b>Price:</b> $' . $logline['9'] . '</li> <li><b>Comment:</b> ' . $logline['11'] . '</li> </ul> </div> '; } } ?> </div> <?php require_once('foot.php'); ?> the script works perfectly, I just can't figure out how to sort it by fields. I've tried several different things and basically exhausted my own abilities, just wondering, really, if anyone has any suggestions to a guide that would walk me through it, or something along those lines. Thanks :-D Quote Link to comment https://forums.phpfreaks.com/topic/165812-pagination-sorting-flat-file-db/ 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.