Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * class: DBManager, gestor per a la base de dades amb les funcions
- * d'imprimir, inserir, esborrar, editar i mostrar detall
- */
- class DBManager
- {
- function __construct() {
- try {
- $host = 'localhost';
- $dbName = 'agenda';
- $user = 'root';
- $pass = '';
- // Conexió
- $this->con = mysqli_connect($host, $user, $pass) or die("Error de conexió amb la base de dades");
- // Selecció DB
- mysqli_select_db($this->con, $dbName) or die("No es troba la BBDD");
- } catch(Exception $ex) {
- throw $ex;
- }
- }
- public function addRegister($contact) {
- $query = "INSERT into contactes
- (nom, cognoms, direccio, localitat, provincia, cp, telefon1, telefon2, fax, mail)
- values ('$contact[0]', '$contact[1]', '$contact[2]', '$contact[3]', '$contact[4]',
- '$contact[5]', '$contact[6]', '$contact[7]', '$contact[8]', '$contact[9]');";;
- var_dump($query);
- mysqli_query($this->con, $query);
- if(mysqli_affected_rows($this->con) <= 0) {
- return false;
- } else {
- return true;
- }
- }
- public function editRegister($contact) {
- $query = "UPDATE contactes SET nom='$contact[1]', cognoms='$contact[2]', direccio='$contact[3]', localitat='$contact[4]',
- provincia='$contact[5]', cp='$contact[6]', telefon1='$contact[7]', telefon2='$contact[8]', fax='$contact[9]', mail='$contact[10]'
- where id=$contact[0];";
- mysqli_query($this->con, $query);
- if(mysqli_affected_rows($this->con) <= 0) {
- return false;
- } else {
- return true;
- }
- }
- public function delRegister($id) {
- $query = "DELETE FROM contactes
- WHERE id='$id'";
- mysqli_query($this->con, $query);
- if(mysqli_affected_rows($this->con) <= 0) {
- return false;
- } else {
- return true;
- }
- }
- public function showOne($thisOne) {
- $query = 'SELECT *
- FROM contactes
- WHERE id = '.$thisOne.';';
- $res = mysqli_query($this->con, $query);
- $data = NULL;
- while ($file = mysqli_fetch_assoc($res)) {
- $data[] = $file;
- }
- return $data;
- }
- /*
- * Consulta de totes les dades de la taula per l'ordre indicat,
- * es retorna un array
- */
- public function showAll() {
- $query = 'SELECT *
- FROM contactes
- ORDER BY '.(isset($_SESSION["sorted"])?$_SESSION["sorted"]:"id").';';
- $res = mysqli_query($this->con, $query);
- $data = NULL;
- while ($file = mysqli_fetch_assoc($res)) {
- $data[] = $file;
- }
- return $data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement