PDA

View Full Version : Dividere una tabella di dati con php


Virgo80
16-10-2008, 10:32
Ciao,
avrei un piccolo problemino di logica e PHP. :)

Ecco il quesito: ho un database MySql da cui prelevo tutta una serie di dati che imposto in una tabella. Il mio scopo è di dividere questa tabella su più pagine. Sono riuscita a creare l'avanti, ma non l'indietro!

Qualcuno saprebbe aiutarmi?

Metto qui il codice:

<?php
// Richiamo la variabile "pos" dalla pagina precedente.
$pos_da = $_REQUEST['pos'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<?php
echo "<script type=\"text/javascript\">
function funReload(form, next) {
form.action='".$_SERVER['PHP_SELF']."?pos='+next;
form.submit();
}
</script>";
?>
</head>
<body>
<form id="x" method="post">
<?php
// richiamo lo script responsabile della connessione a MySQL
require 'connect.php';

// preparo la query. Se c'è pos_da allora esegue una query restrittiva, altrimenti seleziona dal primo record.
if($pos_da){
$query = "Select id, nome,email,sesso,newsletter,attivita,messaggio From utenti WHERE id >= $pos_da LIMIT 12";
}else{
$query = "SELECT id, nome,email,sesso,newsletter,attivita,messaggio FROM utenti ORDER BY ID ASC LIMIT 12";
}

// invio la query
$result = mysql_query($query);

// controllo l'esito
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}

$sesso_arr = array('&nbsp;','M','F');
$attivita_arr = array('&nbsp;','studente','lavoratore','disoccupato');

echo '
<table border="1">
<tr>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
<th>Sesso</th>
<th>Newsletter</th>
<th>Attivit&agrave;</th>
<th>Messaggio</th>
</tr>';
while (($row = mysql_fetch_assoc($result)) and ($contatore<10)) {
$ID = ($row['id']);
$nome = htmlentities($row['nome']);
$email = htmlentities($row['email']);
$messaggio = htmlentities($row['messaggio']);
$messaggio = nl2br($messaggio);

if(!$email) $email = '&nbsp;';
if(!$messaggio) $messaggio = '&nbsp;';

$sesso = $sesso_arr[$row['sesso']];
$newsletter = $row['newsletter'] ? 'Si' : 'No';
$attivita = $attivita_arr[$row['attivita']];

echo "<tr>
<td>$ID</td>
<td>$nome</td>
<td>$email</td>
<td>$sesso</td>
<td>$newsletter</td>
<td>$attivita</td>
<td>$messaggio</td>
</tr>";
// mi serve per bloccare il ciclo a 10 righe
$contatore ++;
}
echo '</table>';
//memorizzo l'ID che è l'11° della lista, mi servirà per la query della pagina seguente
$nuova_pos = $row['id'];

// libero la memoria di PHP occupata dai record estratti con la SELECT
mysql_free_result($result);

// chiudo la connessione a MySQL
mysql_close();
//Indietro
echo "<input type=\"button\" value=\"<<\" onClick="" />"; :cry:

//Avanti
if($nuova_pos!=""){
echo "<input type=\"button\" value=\">>\" onClick=\"javascript:funReload(document.forms[0], '$nuova_pos')\" />";
}
?>
</form>
</body>
</html>


Grazie. :)

VICIUS
16-10-2008, 14:10
Solo un piccolo suggerimento. La clausola LIMIT permette di specificare sia l'offset che il numero di dati da prelevare quindi non ti serve creare due query diverse.

Virgo80
16-10-2008, 15:25
Ciao,

ho risolto :)

Ecco il codice, spero possa servire ad altri in futuro (io mi sono dannata per trovarne uno che facesse al caso mio, e alla fine l'ho costruito prendendo un po' di qua e un po' di là):

<?php
// Richiamo la variabile "pagina" dalla pagina precedente.
$page=$_REQUEST["pagina"];

if(!$page)$page=0;

$page_prec = $page - 1;

$page_succ = $page + 1;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<?php
echo "<script type=\"text/javascript\">
function funReload(form, page) {

form.action='".$_SERVER['PHP_SELF']."?pagina='+page;
form.submit();
}
</script>";
?>
</head>
<body>
<form id="x" method="post" action="">
<?php
//Serve a limitare la ricerca nel database a 10 record
$itemsPerPage=10;
//Serve a indicare da quale record deve partire la select
$start=$page*$itemsPerPage;

// richiamo lo script responsabile della connessione a MySQL
require 'connect.php';

$query = "SELECT SQL_CALC_FOUND_ROWS * from utenti LIMIT $start, $itemsPerPage";

// invio la query
$result = mysql_query($query);

//vedo quanti record ci sono in tutto nella tabella e li memorizzo in $numRow;
$totRecord = mysql_query("Select FOUND_ROWS()");
$arrayRow=mysql_fetch_row($totRecord);
$numRow=$arrayRow[0];

// controllo l'esito
if (!$result) {
die("Errore nella query $query: " . mysql_error());
}

$sesso_arr = array('&nbsp;','M','F');
$attivita_arr = array('&nbsp;','studente','lavoratore','disoccupato');

echo '
<table border="1">
<tr>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
<th>Sesso</th>
<th>Newsletter</th>
<th>Attivit&agrave;</th>
<th>Messaggio</th>
</tr>';
while ($row = mysql_fetch_assoc($result)) {
$ID = ($row['id']);
$nome = htmlentities($row['nome']);
$email = htmlentities($row['email']);
$messaggio = htmlentities($row['messaggio']);
$messaggio = nl2br($messaggio);

if(!$email) $email = '&nbsp;';
if(!$messaggio) $messaggio = '&nbsp;';

$sesso = $sesso_arr[$row['sesso']];
$newsletter = $row['newsletter'] ? 'Si' : 'No';
$attivita = $attivita_arr[$row['attivita']];

echo "<tr>
<td>$ID</td>
<td>$nome</td>
<td>$email</td>
<td>$sesso</td>
<td>$newsletter</td>
<td>$attivita</td>
<td>$messaggio</td>
</tr>";
}
echo '</table>';

// libero la memoria di PHP occupata dai record estratti con la SELECT
mysql_free_result($result);

// chiudo la connessione a MySQL
mysql_close();

if($page_prec>=0){
echo "<input type=\"button\" value=\"<<\" onClick=\"javascript:funReload(document.forms[0], '$page_prec')\" />";
}

if(($numRow / $itemsPerPage)>$page_succ){
echo "<input type=\"button\" value=\">>\" onClick=\"javascript:funReload(document.forms[0], '$page_succ')\" />";
}
?>
</form>
</body>
</html>

Naturalmente è un'ossatura della pagina, andrà abbellita e quant'altro, ma funziona. :)


Ciao.