Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Anuncios{
- public function getMeusAnuncios(){
- global $con;
- $array = [];
- $sql = $con->prepare("SELECT *,
- (select anuncios_imagens.url from anuncios_imagens where anuncios_imagens.id_anuncio = anuncios.id limit 1) as url
- FROM anuncios WHERE id_usuario = :id_usuario");
- $sql->bindValue(':id_usuario', $_SESSION['cLogin']);
- $sql->execute();
- if ($sql->rowCount() > 0){
- $array = $sql->fetchAll();
- }
- return $array;
- }
- public function getAnuncio($id){
- $array = [];
- global $con;
- $sql = $con->prepare("SELECT * FROM anuncios WHERE id = :id");
- $sql->bindValue(":id", $id);
- $sql->execute();
- if ($sql->rowCount() > 0){
- $array = $sql->fetch();
- $array['fotos'] = [];
- $sql = $con->prepare("SELECT id, url FROM anuncios_imagens WHERE id_anuncio = :id_anuncio");
- $sql->bindValue(":id_anuncio", $id);
- $sql->execute();
- if ($sql->rowCount() > 0){
- $array['fotos'] = $sql->fetchAll();
- }
- }
- return $array;
- }
- public function addAnuncio($titulo, $categoria, $valor, $descricao, $estado){
- global $con;
- $sql = $con->prepare("INSERT INTO anuncios SET titulo = :t, id_categoria = :id_cat, id_usuario = :id_user, descricao = :descr, preco = :valor, estado = :est");
- $sql->bindValue(":t", $titulo);
- $sql->bindValue(":id_cat", $categoria);
- $sql->bindValue(":id_user", $_SESSION['cLogin']);
- $sql->bindValue(":descr", $descricao);
- $sql->bindValue(":valor", $valor);
- $sql->bindValue(":est", $estado);
- $sql->execute();
- }
- public function editAnuncio($titulo, $categoria, $valor, $descricao, $estado, $fotos, $id){
- global $con;
- $sql = $con->prepare("UPDATE anuncios SET titulo = :t, id_categoria = :id_cat, id_usuario = :id_user, descricao = :descr, preco = :valor, estado = :est WHERE id = :id");
- $sql->bindValue(":t", $titulo);
- $sql->bindValue(":id_cat", $categoria);
- $sql->bindValue(":id_user", $_SESSION['cLogin']);
- $sql->bindValue(":descr", $descricao);
- $sql->bindValue(":valor", $valor);
- $sql->bindValue(":est", $estado);
- $sql->bindValue(":id", $id);
- $sql->execute();
- if (count($fotos) > 0){
- for ($q=0; $q < count($fotos['tmp_name']); $q++){
- $tipo = $fotos['type'][$q];
- if (in_array($tipo, array('image/jpeg', 'image/png'))){
- $tmpname = md5(time(), rand(0,999999)).'jpg';
- move_uploaded_file($fotos['tmp_name'][$q], 'images/anuncios/'.$tmpname);
- list($width_orig, $heigth_orig) = getimagesize('images/anuncios/'.$tmpname);
- $ratio = $width_orig / $heigth_orig;
- $width = 500;
- $heigth = 500;
- if ($width/$heigth > $ratio){
- $width = $heigth * $ratio;
- } else{
- $heigth = $width / $ratio;
- }
- $img = imagecreatetruecolor($width, $heigth);
- if ($tipo == 'image/jpeg'){
- $origi = imagecreatefromjpeg('images/anuncios/'.$tmpname);
- }elseif ($tipo == 'image/png'){
- $origi = imagecreatefrompng('images/anuncios/'.$tmpname);
- }
- imagecopyresampled($img, $origi, 0, 0, 0, 0, $width, $heigth, $width_orig, $heigth_orig);
- imagejpeg($img, 'images/anuncios/'.$tmpname, 80);
- $sql = $con->prepare("INSERT INTO anuncios_imagens SET id_anuncio = :id_anuncio, url = :url");
- $sql->bindValue(':id_anuncio', $id);
- $sql->bindValue(':url', $tmpname);
- $sql->execute();
- }
- }
- }
- }
- public function excluirAnuncio($id){
- global $con;
- $sql = $con->prepare("DELETE FROM anuncios_imagens WHERE id_anuncio = :id");
- $sql->bindValue(":id", $id);
- $sql->execute();
- $sql = $con->prepare("DELETE FROM anuncios WHERE id = :id");
- $sql->bindValue(":id", $id);
- $sql->execute();
- }
- public function excluirFoto($id){
- global $con;
- $id_anuncio = 0;
- $sql = $con->prepare("SELECT id_anuncio FROM id_anuncios_imagens WHERE id = :id");
- $sql->bindValue(":id", $id);
- $sql->execute();
- if ($sql->rowCount() > 0){
- $row = $sql->fetch();
- $id_anuncio = $row['id_anuncio'];
- }
- $sql = $con->prepare("DELETE FROM anuncios_imagens WHERE id = :id");
- $sql->bindValue(":id", $id);
- $sql->execute();
- return $id_anuncio;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement