Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal, estou com alguns problemas para fazer um sistema de CRUD para músicas utilizando DAO. Será que alguém poderia me dar uma força? Até agora só consegui fazer a parte de inserção... o resto não consigo fazer NADA! Seguem os códigos:
controllers/musicController.php
<?php
require_once ("../models/music.php");
class musicController {
private $cadastre;
private $selection;
public function __construct() {
$action = $_POST['action'];
if ($action == "insert") {
$this->cadastre = new music();
$this->insert();
} elseif($action == "select"){
$this->selection = new music();
$this->select();
}
}
private function insert() {
$this->cadastre->setAlb_id($_POST['alb_id']);
$this->cadastre->setArtist($_POST['artist']);
$this->cadastre->setTitle($_POST['title']);
$this->cadastre->setTrack($_POST['track']);
$result = $this->cadastre->insert();
if ($result >= 1) {
echo "<script>alert('Registro incluído com sucesso!');
document.location='../view/index.php'</script>";
} else {
echo "<script>alert('Erro ao gravar registro!');
history.back()</script>";
}
}
private function select_m(){
}
}
new musicController();
?>
models/music.php
<?php
require_once ("database.php");
class music extends dataBase {
private $track;
private $alb_id;
private $title;
private $artist;
function __construct() {
parent::__construct();
}
public function setTrack($int) {
$this->track = $int;
}
public function setAlb_id($int) {
$this->alb_id = $int;
}
public function setTitle($string) {
$this->title = $string;
}
public function setArtist($string) {
$this->artist = $string;
}
public function getTrack() {
return $this->track;
}
public function getAlb_id() {
return $this->alb_id;
}
public function getTitle() {
return $this->title;
}
public function getArtist() {
return $this->artist;
}
public function insert() {
$this->table = "music";
$this->fields = array("track", "alb_id", "title", "artist");
$this->values = array($this->getTrack(), $this->getAlb_id(),
$this->getTitle(), $this->getArtist());
$result = $this->insertValues();
return $result;
}
public function delete($id) {
$this->fields = array("id");
$this->values = array("$id");
$result = $this->deleteValues();
return $result;
}
public function select($string){
$this->fields = array("music");
$this->values = array("$string");
$result = $this->selectValues($string);
return $result;
}
}
?>
models/dataBase.php
<?php
class dataBase {
private $host;
private $user;
private $pass;
private $dBase;
private $queryFields = NULL;
private $queryValues = NULL;
private $queryIquality = NULL;
private $sql;
private $query;
public $table;
public $fields; //array
public $values; //array
private $connection;
public function __construct() {
$this->connection();
}
private function connection() {
$this->host = 'localhost';
$this->user = 'root';
$this->pass = '12345';
$this->dBase = 'collection';
mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error());
mysql_select_db($this->dBase);
}
//$type == 1 -> Insert
//$type == 2 -> Update
//$type == 3 -> Delete
private function buildQuery($type) {
$cont = count($this->fields);
for ($i = 0; $i < $cont; $i++) {
//Se tiver mais de 1
if ($i < $cont - 1) {
if ($type == 1) {
$this->queryFields .= $this->fields[$i] . ", ";
$this->queryValues .= "'" . $this->values[$i] . "', ";
} elseif ($type == 2) {
} elseif ($type == 3) {
//$this->queryFields .= $this->fields[$i] . "=" . "'" .
// $this->values[$i] . "' AND ";
}
//Se tiver apenas 1
} else {
if ($type == 1) {
$this->queryFields .= $this->fields[$i];
$this->queryValues .= "'" . $this->values[$i] . "'";
} elseif ($type == 2) {
} elseif ($type == 3) {
$this->queryIquality .= $this->fields[$i] . "=" . "'" .
$this->values[$i] . "'";
} elseif ($type == 4) {
$this->queryIquality .= $this->fields[$i] . "=" . "'" .
$this->values[$i] . "'";
}
}
}
}
public function insertValues() {
$this->buildQuery(1);
$this->sql = "INSERT INTO " . $this->table . " (" . $this->queryFields .
") VALUES (" . $this->queryValues . ")";
$this->query = mysql_query($this->sql) or die(mysql_error());
$this->result = mysql_affected_rows();
return $this->result;
}
public function updateValues() {
}
public function deleteValues() {
$this->buildQuery(3);
$this->sql = "DELETE FROM " . $this->table . " WHERE " .
$this->queryFields;
$this->query = mysql_query($this->sql);
$this->result = mysql_affected_rows();
return $this->result;
}
public function selectValues($string) {
$this->buildQuery(4);
$this->sql = "SELECT * FROM " . $this->table . " WHERE " .
$this->queryIquality . " ORDER BY " . $string . " ASC";
$this->query = mysql_query($this->sql);
$this->result = mysql_affected_rows();
return $this->result;
}
}
?>
No caso, eu teria um formulário para pesquisar as músicas, deletar, atualizar e cadastrar... Na verdade, o sistema é um pouco mais que isso, mas se me ajudarem nesta parte acredito que consigo fazer o resto por conta própria...
Carregando comentários...