//error_reporting(E_ALL);
//ini_set("display_errors", 1);
/*
* Classe per la gestione dei DATABASE
*/
/*
* Costanti di controllo dell'output per la gestione della paginazione
*/
class mysql_dpm
{
// hostname, user e password
public $hostname; // hostname del db server
public $user; // username
public $password; // password
public $db; // nome database
public $table; // tabella contenente i dati del menu
public $connessione; // Connessione al db @public stringa
public $conn_attuale = NULL; // La più recente connessione al db @public stringa
public $sql = ''; // La più recente query SQL @public stringa
public $qry; // La query
public $rs = array();
public $conta_query = 0; // Il numero di query eseguite @public intero
public $errore = ''; // Il testo del più recente messaggio di errore @public stringa
public $errno = ''; // Il numero d'errore dell'errore più recente @public intero
public $msgerrore = "";
public $is_locked = false; // publicabile per verificare uno stato di lock @public boolean
public $show_errors = false; // publicabile per visualizzare messaggi d'errore @public boolean
public $num_record = 0;
public $record_interessati = 0;
public $ultimo_ins;
// public $result;
public $get_qsvar = QS_PAGE;
public $get_alias = "alias";
public $righe_pagina = RIGHE_PER_PAGINA;
public $str_avanti = STR_AVANTI;
public $str_indietro = STR_INDIETRO;
// public $all_rows;
// public $num_rows;
public $pagina;
public $num_pagine;
public $naviga;
// Metodo COSTRUTTORE
public function __construct($hostname, $user, $password, $db, $charset='utf8')
{
$this->connessione = @mysql_connect($hostname, $user, $password, $db);
if ($this->connessione)
{
mysql_set_charset($charset, $this->connessione);
if (@mysql_select_db($db, $this->connessione))
{
$this->conn_attuale = $this->connessione;
return $this->connessione;
}
}
else
{
//$this->raise_error("Non riesco a selezionare e/o connettermi al database: $db");
return $this->msgerrore();
}
}
// Metodo per settare il CHARSET della connessione
function set_charset($chr) {
@mysql_set_charset($chr, $this->connessione);
}
public function __destruct()
{
$this->chiudi_connessione();
}
public function chiudi_connessione()
{
if ($this->qry)
{
$this->libera_risorse($this->qry);
}
return @mysql_close($this->connessione);
}
// Metodi per gestire gli errori
public function errore()
{
$this->errore = (is_null($this->conn_attuale)) ? "" : mysql_error($this->conn_attuale);
return $this->errore;
}
public function errno()
{
$this->errno = (is_null($this->conn_attuale)) ? 0 : mysql_errno($this->conn_attuale);
return $this->errno;
}
public function msgerrore()
{
return "[".$this->errno()."] ".$this->errore()."\n";
}
public function libera_risorse($qry)
{
return @mysql_free_result($qry);
}
// Metodi per interrogare il DB ed estrarre record
public function query($sql, $solo_1_rec = false)
{
$this->qry = mysql_query($sql, $this->connessione);
$this->conta_query++;
if ($this->qry !== false)
{
if ($solo_1_rec)
{
$this->rs = $this->associativo($this->qry);
$this->libera_risorse($this->qry);
return $this->rs;
unset($this->rs);
}
else
{
return $this->qry;
}
}
else
{
//echo("C'è un errore nell'esecuzione della QUERY:
\n".$sql."\n");
//echo("C'è un errore nell'esecuzione della QUERY.
\nContattare info@solidea.com\n");
//exit();
return FALSE;
}
}
public function associativo($qry)
{
return @mysql_fetch_assoc($qry);
}
public function numerico($qry)
{
return @mysql_fetch_array($qry, MYSQL_NUM);
}
public function v_associativo($sql)
{
$v = array();
$r = array();
$this->qry = $this->query($sql);
while ($v = $this->associativo($this->qry))
{
array_push($r, $v);
}
$this->libera_risorse($this->qry);
return $r;
}
public function v_numerico($sql)
{
$v = array();
$this->qry = $this->query($sql);
while ($v = $this->numerico($this->qry))
{
array_push($this->rs, $v);
}
$this->libera_risorse($this->qry);
return $this->rs;
}
public function num_record_sql($sql)
{
$this->qry = $this->query($sql);
$this->num_record = mysql_num_rows($this->qry);
$this->libera_risorse($this->qry);
return $this->num_record;
}
public function num_record($qry)
{
$this->num_record = mysql_num_rows($qry);
return $this->num_record;
}
public function record_interessati()
{
return @mysql_affected_rows($this->connnessione);
}
public function ultimo_inserito()
{
return @mysql_insert_id($this->connessione);
}
public function numero_query()
{
return $this->conta_query;
}
public function lock_tab($tab)
{
if ($tab != '')
{
if ($this->query("LOCK TABLES ".$tab." WRITE"))
{
$this->is_locked = true;
}
}
}
public function unlock_tab()
{
if ($this->is_locked)
{
$this->query("UNLOCK TABLES");
}
}
/*********************************************************************************************************
********************* Le PROPRIETA' che seguono servono per gestire la paginazione ******************
*********************************************************************************************************/
// Impostiamo il numero della pagina corrente
public function set_pagina()
{
//$this->pagina = (isset($_GET[$this->get_qsvar]) && $_GET[$this->get_qsvar] != "") ? $_GET[$this->get_qsvar] : 0;
if (isset($_GET[$this->get_qsvar]) && $_GET[$this->get_qsvar] != "")
{
$this->pagina = substr($_GET[$this->get_qsvar], strlen(($_GET[$this->get_alias])."-"));
}
else
{
$this->pagina = 0;
}
return $this->pagina;
}
// Restituisce il numero totale di pagine
public function numero_pagine($qry)
{
$this->num_pagine = ceil($this->num_record($qry) / $this->righe_pagina);
return $this->num_pagine;
}
// Metodo 1 per la paginazione vera e propria: ritorna SQL formattato
public function sql_paginazione($sql)
{
$inizio = ($this->set_pagina() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
//$this->qry = $this->query($this->sql);
return $this->sql;
}
// Metodo 2 per la paginazione vera e propria: ritorna QRY
public function qry_paginazione($sql)
{
$inizio = ($this->set_pagina() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
$this->qry = $this->query($this->sql);
return $this->qry;
}
// Metodo 3 per la paginazione vera e propria: ritorna Vettore dati
public function rs_paginazione($sql)
{
$inizio = ($this->set_pagina() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
$this->rs = $this->v_associativo($this->sql);
return $this->rs;
}
/*public function nuova_qs($escludi)
{
}*/
public function navigazione_semplice($sql, $separatore = " | ", $css = "", $avantindietro = false)
{
//$url = "index.php?alias=".$_GET['alias'];
$url = "/".$_GET[$this->get_alias]."/".$_GET[$this->get_alias]."-";
$nlink = NAV_LINKS;
$classica = false;
$corrente = $this->set_pagina();
$pippo = $corrente;
$tutte = ($this->numero_pagine($this->query($sql)) - 1);
if (!$avantindietro)
{
$nlink = ($nlink < 2) ? 2 : $nlink;
}
if (($corrente <= $tutte) && ($corrente >= 0))
{
if ($corrente > ceil($nlink / 2))
{
$inizio = (($corrente - ceil($nlink / 2)) > 0) ? ($corrente - ceil($nlink / 2)) : 1;
$fine = ($corrente + ceil($nlink / 2));
if ($fine > $tutte)
{
$fine = ($tutte + 1);
if ($tutte < $nlink)
{
$inizio = 0;
}
else
{
$inizio = (($tutte - ceil($nlink / 2)) > 0) ? ($tutte - ($nlink - 1)) : 1;
}
}
}
else
{
$inizio = 0;
$fine = ($tutte >= $nlink) ? $nlink : ($tutte + 1);
}
if ($tutte >= 1)
{
$avanti = ($corrente + 1);
$indietro = ($corrente - 1);
//$this->naviga = ($corrente > 0) ? "". $this->str_indietro ." " : " ";
$this->naviga = ($corrente > 0) ? "". $this->str_indietro ." " : " ";
if (!$avantindietro)
{
$c = ($inizio + 1);
for ($x = $c; $x <= $fine; $x++)
{
$prox = ($x - 1);
if ($prox != $corrente)
{
//$this->naviga .= "". $x ."";
$this->naviga .= "". $x ."";
}
else
{
if ($css != "")
{
$this->naviga .= "".$x."";
}
else
{
$this->naviga .= $x;
}
}
$this->naviga .= ($prox < ($fine - 1)) ? $separatore : "";
}
}
//$this->naviga .= ($corrente < $tutte) ? " ". $this->str_avanti ."" : " ";
$this->naviga .= ($corrente < ($fine - 1)) ? " ". $this->str_avanti ."" : " ";
}
}
if ($this->naviga != "")
{
return $this->naviga;
}
else
{
return "1";
}
}
/**************************************************************
METODI PER LA PAGINAZIONE CLASSICA (con parametri nell'URL)
**************************************************************/
// Impostiamo il numero della pagina corrente
public function set_pagina_classica()
{
$this->pagina = (isset($_GET['page']) && ($_GET['page'] != "")) ? $_GET['page'] : 0;
return $this->pagina;
}
// Metodo 1 per la paginazione vera e propria: ritorna SQL formattato
public function sql_paginazione_classica($sql)
{
$inizio = ($this->set_pagina_classica() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
//$this->qry = $this->query($this->sql);
return $this->sql;
}
// Metodo 2 per la paginazione vera e propria: ritorna QRY
public function qry_paginazione_classica($sql)
{
$inizio = ($this->set_pagina_classica() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
$this->qry = $this->query($this->sql);
return $this->qry;
}
// Metodo 3 per la paginazione vera e propria: ritorna Vettore dati
public function rs_paginazione_classica($sql)
{
$inizio = ($this->set_pagina_classica() * $this->righe_pagina);
$this->sql = sprintf("%s LIMIT %s, %s", $sql, $inizio, $this->righe_pagina);
$this->rs = $this->v_associativo($this->sql);
return $this->rs;
}
// Metodo per ricostruire la querystring senza il valore PAGE
public function rebuild_qs()
{
$ex = array('page');
$nqs = "";
foreach ($_GET as $chiave => $valore)
{
if (!in_array($chiave, $ex))
{
$nqs .= $chiave."=".$valore."&";
}
}
return substr($nqs, 0, (strlen($nqs) - 1));
}
// Metodo per la Paginazione Classica: URL con parametri, a cui appendiamo &pagina=n
public function navigazione_classica($sql, $separatore = " | ", $css = "", $avantindietro = false)
{
//$url = "index.php?alias=".$_GET['alias'];
$newqs = $this->rebuild_qs();
$url = $_SERVER['PHP_SELF']."?".$newqs;
$nlink = NAV_LINKS;
$classica = true;
$corrente = $this->set_pagina_classica();
$pippo = $corrente;
$tutte = ($this->numero_pagine($this->query($sql)) - 1);
if (!$avantindietro)
{
$nlink = ($nlink < 2) ? 2 : $nlink;
}
if (($corrente <= $tutte) && ($corrente >= 0))
{
if ($corrente > ceil($nlink / 2))
{
$inizio = (($corrente - ceil($nlink / 2)) > 0) ? ($corrente - ceil($nlink / 2)) : 1;
$fine = ($corrente + ceil($nlink / 2));
if ($fine > $tutte)
{
$fine = ($tutte + 1);
if ($tutte < $nlink)
{
$inizio = 0;
}
else
{
$inizio = (($tutte - ceil($nlink / 2)) > 0) ? ($tutte - ($nlink - 1)) : 1;
}
}
/*if ( ($tutte > $nlink) && ($fine > $tutte))
{
$fine = ($tutte + 1);
$inizio = (($tutte - ceil($nlink / 2)) > 0) ? ($tutte - ($nlink - 1)) : 1;
}
elseif ( ($tutte < $nlink) && ($fine > $tutte))
{
$fine = ($tutte + 1);
$inizio = 0;
}*/
}
else
{
$inizio = 0;
$fine = ($tutte >= $nlink) ? $nlink : ($tutte + 1);
}
if ($tutte >= 1)
{
$avanti = ($corrente + 1);
$indietro = ($corrente - 1);
$this->naviga = ($corrente > 0) ? "". $this->str_indietro ." " : " ";
//$this->naviga = ($corrente > 0) ? "". $this->str_indietro ." " : " ";
if (!$avantindietro)
{
$c = ($inizio + 1);
for ($x = $c; $x <= $fine; $x++)
{
$prox = ($x - 1);
if ($prox != $corrente)
{
$this->naviga .= "". $x ."";
//$this->naviga .= "". $x ."";
}
else
{
if ($css != "")
{
$this->naviga .= "".$x."";
}
else
{
$this->naviga .= $x;
}
}
$this->naviga .= ($prox < ($fine - 1)) ? $separatore : "";
}
}
$this->naviga .= ($corrente < $tutte) ? " ". $this->str_avanti ."" : " ";
//$this->naviga .= ($corrente < ($fine - 1)) ? " ". $this->str_avanti ."" : " ";
}
}
if ($this->naviga != "")
{
//return "".$this->naviga;
return $this->naviga;
}
else
{
return "1";
}
}
}
// http://www.giorgiotave.it/forum/php-mysql/105284-paginazione-php.html
/*include('costanti.php');
$db = new mysql_dpm(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
$s = "SELECT CodParco, Anno, Mese, TipoVeicoloIT, Portata, DesCarIt, Passo, Lungh, Largh, Altezza, Disponibile, Prezzo FROM `veicoli` ORDER BY dataarrivo ASC LIMIT 0,10";
$t = $db->v_numerico($s);
print_r($t);
echo "