La paginazione viene utilizzata per dividere le righe di una tabella MySQL in più pagine. In questo tutorial vi mostrerò come è possibile creare una versione semplice.
< ?php
/**
* SORT OUT VARIABLES
*/
$offset = mysql_escape_string(strip_tags($_GET[‘offset’])); // Protect the variables in the url from injection
$limit = mysql_escape_string(strip_tags($_GET[‘limit’])); // Protect the variables in the url from injection
if(empty($limit)){ // Check if $limit is empty or not.
$limit = "10"; // If $limit is empty set it to 10.
}
if(empty($offset) || $offset < 0){ // Check if $offset is empty or is smaller than 0
$offset = "0"; // If $offset is empty set it to 0.
}
/**
* RETRIEVE THE TABLE AND ECHO ANY INFORMATION IN IT.
*/
$result = mysql_query("SELECT * FROM `table` LIMIT " . $limit . " OFFSET " . $offset); // Select from the table as usual but adding the limit and offset paramaters.
while($r=mysql_fetch_array($result)) // Go through row by row
{
echo $r[‘column1′] . " " . $r[‘column2′] . ""; // Echo the row and the data inside.
}
/**
* CREATE THE LINKS
*/
$previous = $offset - $limit; // Workout offset for previous link
$next = $offset + $limit; // Workout offset for next link
if($previous < 0){ // Check if either $previous is smaller than 0
$previous = "0"; // Set $previous to 0
}
echo "Previous | Next"; // Echo the links.
?>
fonte: www.sastgroup.com » Vai al post originale