php - Automatically creating next and previous type pages using glob -
trying make glob function displays first 10 results (text files folder) , automatically next 10 next button.
currently, used array_slice display first 10, i'm not sure how automate process.
foreach(array_slice(glob("*.txt"),0,9) $filename) { include($filename); }
oh , while i'm @ it, if possible, display "displaying 1-10" , last/first links. figure out myself after past first obstacle, don't bother unless it's super easy solution.
what you're trying accomplish called pagination. example, can dynamically choose ten you're viewing setting variable determines number (file) start at.
example:
<?php $start = isset( $_get['start']) ? intval( $_get['start']) : 0; $glob_result = glob("*.txt"); $num_results = count( $glob_result); // check make sure $start value makes sense if( $start > $num_results) { $start = 0; } foreach( array_slice( $glob_result, $start, 9) $filename) { include( $filename); // warning! }
if want increments of 10, can add check make sure $start
divisible 10, after $start
retrieved $_get
array:
$start = ( $start % 10 == 0) ? $start : 0;
now, links, need output <a>
tags have start parameter set correctly. have logic calculate next , previous values correctly, here simple example:
$new_start = $start + 10; echo '<a href="page.php?start=' . $new_start . '">next</a>';
edit: comments above suggest, not want include()
these files, include()
attempt interpret these files php scripts. if need contents of text files, use file_get_contents instead.
Comments
Post a Comment