help codice

SolidSnake4

Utente Attivo
23 Ott 2007
505
0
0
PHP:
<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include '../library/config.php';
include '../library/opendb.php';

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while(list($val) = mysql_fetch_array($result))
{
	echo "$val <br>";
}

echo '<br>';

// how many rows we have in database
$query   = "SELECT COUNT(val) AS numrows FROM randoms";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
	$page = $pageNum - 1;
	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
	
	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
	$first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
	
	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
	$last = ' [Last Page] '; // nor 'last page' link
}

// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;

include '../library/closedb.php';
?>
</body>
</html>

qualcuno mi spiega come funziona questo script ? $_GET['page'] prende come parametro una pagina ?
 
ok grazie mille e in questo pezzo di codice

PHP:
// how many rows we have in database 
$query   = "SELECT COUNT(val) AS numrows FROM randoms"; 
$result  = mysql_query($query) or die('Error, query failed'); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 
$numrows = $row['numrows'];

numrows ricava il numero di righe presenti in uno specifico db? E volendolo modificare a seconda delle mie esigenze, facendo stampare a video i campi titolo data e anteprima, devo sostituire al posto di val questi campi ?
 
SELECT COUNT serve per contare, in output ricevi un numero non la visualizzazione di un campo
 
ok e per far visualizzare quei campi che ho scritto prima come dovrei modificarlo lo script ?
 
Ti basta modificare la query:
PHP:
$query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
estraendo i valori che ti interessano.
 

Discussioni simili