Ringrazio anticipatamente chiunque avrà la bontà di darmi una mano!
Ho messo a punto uno script di paginazione di un semplice motore di ricerca appoggiato a una mysql_table di un mio mysql_db. Pensando a futuri aumenti della dimensione di questo mio database, ho voluto approntare una classe di paginazione che consiste di 2 file:
1) pagination.php (che è la classe vera e propria);
2) index.php (che è il file contenente il codice di estrazione dei records + il form per la ricerca sul mysql_db).
Ora, il mio problema si evidenzia nel passaggio dalla prima pagina dei risultati a quelle successive. Mi spiego meglio.
Fino a quando si rimane sulla prima pagina, tutto funziona a meraviglia... Si ottengono i risultati cercati, nel limite imposto dalla class.pagination, i link delle pagine successive vengono creati correttamente in base al totale dei records trovati / i records per pagina... ecc, ecc.
Quando si passa alla seconda pagina mi ritrovo una pagina vuota, che riporta solo il messaggio d'errore con scritto "Query was empty"!
E' come se nel passaggio dalla prima alla seconda pagina e a quelle successive, si vada perdendo l'imput che correttamente ha dato il form di ricerca alla prima pagina.
Cosa posso fare? Aggiungo i codici della class e del file contenitore.
Questo il file contenitore index.php:
Ho messo a punto uno script di paginazione di un semplice motore di ricerca appoggiato a una mysql_table di un mio mysql_db. Pensando a futuri aumenti della dimensione di questo mio database, ho voluto approntare una classe di paginazione che consiste di 2 file:
1) pagination.php (che è la classe vera e propria);
2) index.php (che è il file contenente il codice di estrazione dei records + il form per la ricerca sul mysql_db).
Ora, il mio problema si evidenzia nel passaggio dalla prima pagina dei risultati a quelle successive. Mi spiego meglio.
Fino a quando si rimane sulla prima pagina, tutto funziona a meraviglia... Si ottengono i risultati cercati, nel limite imposto dalla class.pagination, i link delle pagine successive vengono creati correttamente in base al totale dei records trovati / i records per pagina... ecc, ecc.
Quando si passa alla seconda pagina mi ritrovo una pagina vuota, che riporta solo il messaggio d'errore con scritto "Query was empty"!
E' come se nel passaggio dalla prima alla seconda pagina e a quelle successive, si vada perdendo l'imput che correttamente ha dato il form di ricerca alla prima pagina.
Cosa posso fare? Aggiungo i codici della class e del file contenitore.
PHP:
<?php
class pagination {
var $php_self;
var $rows_per_page = 3;
var $total_rows = 0;
var $links_per_page = 5;
var $sql = 'SELECT (*) FROM `mysql_table` WHERE `ragionesociale` LIKE %$keyword% OR `indirizzo` LIKE %$keyword% OR `cap` LIKE %$keyword% OR `citta` LIKE %$keyword% OR `provincia` LIKE %$keyword% OR `regione` LIKE %$keyword% ';
var $debug = false;
var $conn = false;
var $page = 1;
var $max_pages = 0;
var $offset = 0;
function pagination($connection, $sql, $rows_per_page = 3, $links_per_page = 5)
{
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = (int)$rows_per_page;
if (intval($links_per_page ) > 0) {
$this->links_per_page = (int)$links_per_page;
} else {
$this->links_per_page = 5;
}
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
if (isset($_POST['page'] )) {
$this->page = intval($_POST['page'] );
}
}
function paginate() {
if (! $this->conn || ! is_resource($this->conn )) {
if ($this->debug)
echo "Connessione perduta<br />";
return false;
}
$all_rs = @mysql_query($this->sql );
if (! $all_rs) {
if ($this->debug)
echo "Controlla i termini di ricerca.<br /><br />Errore: ";
return false;
}
$this->total_rows = mysql_num_rows($all_rs );
@mysql_close($all_rs );
if ($this->total_rows == 0) {
if ($this->debug)
echo "Nessun risultato trovato con “" . $_POST['keyword'] . "”";
return FALSE;
}
$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
if ($this->links_per_page > $this->max_pages) {
$this->links_per_page = $this->max_pages;
}
if ($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
$this->offset = $this->rows_per_page * ($this->page - 1);
$result = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}" );
if (! $result) {
if ($this->debug)
echo "Controlla i termini di ricerca.<br /><br />Errore: ";
return false;
}
return $result;
}
function renderFirst($tag = 'First') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page == 1) {
return "$tag ";
} else {
return '<a href="' . $this->php_self . '?page=1">' . $tag . '</a> ';
}
}
function renderLast($tag = 'Last') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page == $this->max_pages) {
return $tag;
} else {
return ' <a href="' . $this->php_self . '?page=' . $this->max_pages . '">' . $tag . '</a>';
}
}
function renderNext($tag = '>>') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page < $this->max_pages) {
return '<a href="' . $this->php_self . '?page=' . ($this->page + 1) . '">' . $tag . '</a>';
} else {
return $tag;
}
}
function renderPrev($tag = '<<') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page > 1) {
return ' <a href="' . $this->php_self . '?page=' . ($this->page - 1) . '">' . $tag . '</a>';
} else {
return " $tag";
}
}
function renderNav($prefix = '<span class="page_link">', $suffix = '</span>') {
if ($this->total_rows == 0)
return FALSE;
$batch = ceil($this->page / $this->links_per_page );
$end = $batch * $this->links_per_page;
if ($end == $this->page) {
}
if ($end > $this->max_pages) {
$end = $this->max_pages;
}
$start = $end - $this->links_per_page + 1;
$links = '';
for($i = $start; $i <= $end; $i ++) {
if ($i == $this->page) {
$links .= $prefix . " $i " . $suffix;
} else {
$links .= ' ' . $prefix . '<a href="' . $this->php_self . '?page=' . $i . '">' . $i . '</a>' . $suffix . ' ';
}
}
return $links;
}
function renderFullNav() {
return $this->renderFirst() . ' ' . $this->renderPrev() . ' ' . $this->renderNav() . ' ' . $this->renderNext() . ' ' . $this->renderLast();
}
function setDebug($debug) {
$this->debug = $debug;
}
}
?>
Questo il file contenitore index.php:
PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><?php echo ($_POST['keyword']); ?></title>
</head>
<body>
<form action="index.php" method="post" name="search">
<fieldset>
<label for="search">Cerca... </label>
<input name="search" type="hidden" value="search">
<input name="keyword" autocomplete="on" type="text" value="<?php echo ($_POST['keyword']); ?>" size="60" maxlength="30">
<input name="cerca" type="submit" value="Cerca!">
</fieldset>
</form>
<?php
include('pagination.php');
$conn = mysql_connect('localhost','user','psw');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('mysql_db', $conn);
if(!$status) die("Failed to select database!");
if (@$_POST['search']=="search")
{
$keyword=$_POST['keyword'];
$sql = "SELECT * FROM `mysql_table` WHERE `ragionesociale` LIKE '%$keyword%' OR `indirizzo` LIKE '%$keyword%' OR `cap` LIKE '%$keyword%' OR `citta` LIKE '%$keyword%' OR `provincia` LIKE '%$keyword%' OR `regione` LIKE '%$keyword%' ";
}
$pager = new pagination($conn, $sql, 3, 5);
$pager->setDebug(true);
$result = $pager->paginate();
if(!$result) die(mysql_error());
while ($results = mysql_fetch_array($result))
{
$url=$results['pageUrl'];
$ragionesociale=$results['ragionesociale'];
$indirizzo=$results['indirizzo'];
$cap=$results['cap'];
$citta=$results['citta'];
$provincia=$results['provincia'];
$telefono=$results['telefono'];
$fax=$results['fax'];
echo "<h3><a href='$url' title=' Fai click per maggiori informazioni su: \r “ $ragionesociale ”' ?>$ragionesociale</a></h3>
<p>$indirizzo<br />$cap - $citta ($provincia)<br />
tel. $telefono - fax $fax</p>";
}
echo $pager->renderFullNav();
echo "<br />\n";
echo $pager->renderFirst();
echo $pager->renderPrev();
echo $pager->renderNav('<span>', '</span>');
echo $pager->renderNext();
echo $pager->renderLast();
?>
</body>
</html>
Ultima modifica di un moderatore: