I am quite unused with php. I want to create a menu by taking objects from a database, but i am confused by all the different methods of doing so (both from school and internet) and i need to make some order in my mind.
This is what i have done until now, i use two different classes for doing so.
Db.php
class Db extends PDO {
private $uname;
private $passwd;
private $hostname;
private $dbname;
public function __construct() {
$this->uname = "root";
$this->passwd = "";
$this->hostname = "127.0.0.1";
$this->dbname = "myclienti";
try {
parent::__construct('mysql:charset=utf8mb4;host=' . $this->hostname . ';dbname=' . $this->dbname, $this->uname, $this->passwd);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
Servizie.php
class Servizie {
private $codice_utente;
private $desc_servizio;
private $puntopresa;
function getCodice_utente() {
return $this->codice_utente;
}
function getDesc_servizio() {
return $this->desc_servizio;
}
function getPuntopresa() {
return $this->puntopresa;
}
function setCodice_utente($codice_utente) {
$this->codice_utente = $codice_utente;
}
function setDesc_servizio($desc_servizio) {
$this->desc_servizio = $desc_servizio;
}
function setPuntopresa($puntopresa) {
$this->puntopresa = $puntopresa;
}
public function getAllServizie() {
$db = new Db();
$query = 'select CODICE_UTENTE, DESC_SERVIZIO, PUNTOPRESA from serviziele';
$statement = $db->prepare($query);
$statement->execute();
$object = $statement->fetchAll(PDO::FETCH_ASSOC);
return $object;
}
}
Part of my index page where i want to put my menu.
<?php
$servizie = new Servizie();
$list = $servizie->getAllServizie();
echo '<div id="dropdown-contentelectr">';
foreach ($list as $element){
echo '<a href="#">' . $element->PUNTOPRESA . '</a>';
}
echo '</div>';
?>
Sorry for my english.