Advertisement
Guest User

php

a guest
Oct 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.02 KB | None | 0 0
  1. <?php
  2. class  Anuncios{
  3.  
  4.     public  function getMeusAnuncios(){
  5.         global  $con;
  6.         $array = [];
  7.         $sql = $con->prepare("SELECT *,
  8.                                    (select anuncios_imagens.url from anuncios_imagens where anuncios_imagens.id_anuncio = anuncios.id limit 1) as url
  9.                                    FROM anuncios WHERE id_usuario = :id_usuario");
  10.         $sql->bindValue(':id_usuario', $_SESSION['cLogin']);
  11.         $sql->execute();
  12.  
  13.         if ($sql->rowCount() > 0){
  14.             $array = $sql->fetchAll();
  15.         }
  16.         return $array;
  17.     }
  18.  
  19.     public function getAnuncio($id){
  20.         $array = [];
  21.         global $con;
  22.  
  23.         $sql = $con->prepare("SELECT * FROM anuncios WHERE id = :id");
  24.         $sql->bindValue(":id", $id);
  25.         $sql->execute();
  26.  
  27.         if ($sql->rowCount() > 0){
  28.             $array = $sql->fetch();
  29.             $array['fotos'] = [];
  30.  
  31.             $sql = $con->prepare("SELECT id, url FROM anuncios_imagens WHERE  id_anuncio = :id_anuncio");
  32.             $sql->bindValue(":id_anuncio", $id);
  33.             $sql->execute();
  34.  
  35.                 if ($sql->rowCount() > 0){
  36.                     $array['fotos'] = $sql->fetchAll();
  37.                 }
  38.         }
  39.         return $array;
  40.     }
  41.  
  42.     public function addAnuncio($titulo, $categoria, $valor, $descricao, $estado){
  43.         global $con;
  44.  
  45.         $sql = $con->prepare("INSERT INTO anuncios SET titulo = :t, id_categoria = :id_cat, id_usuario = :id_user, descricao = :descr, preco = :valor, estado = :est");
  46.         $sql->bindValue(":t", $titulo);
  47.         $sql->bindValue(":id_cat", $categoria);
  48.         $sql->bindValue(":id_user", $_SESSION['cLogin']);
  49.         $sql->bindValue(":descr", $descricao);
  50.         $sql->bindValue(":valor", $valor);
  51.         $sql->bindValue(":est", $estado);
  52.         $sql->execute();
  53.     }
  54.  
  55.     public function editAnuncio($titulo, $categoria, $valor, $descricao, $estado, $fotos, $id){
  56.         global $con;
  57.  
  58.         $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");
  59.         $sql->bindValue(":t", $titulo);
  60.         $sql->bindValue(":id_cat", $categoria);
  61.         $sql->bindValue(":id_user", $_SESSION['cLogin']);
  62.         $sql->bindValue(":descr", $descricao);
  63.         $sql->bindValue(":valor", $valor);
  64.         $sql->bindValue(":est", $estado);
  65.         $sql->bindValue(":id", $id);
  66.         $sql->execute();
  67.  
  68.         if (count($fotos) > 0){
  69.             for ($q=0; $q < count($fotos['tmp_name']); $q++){
  70.                 $tipo = $fotos['type'][$q];
  71.                 if (in_array($tipo, array('image/jpeg', 'image/png'))){
  72.                     $tmpname = md5(time(), rand(0,999999)).'jpg';
  73.                     move_uploaded_file($fotos['tmp_name'][$q], 'images/anuncios/'.$tmpname);
  74.  
  75.                     list($width_orig, $heigth_orig) = getimagesize('images/anuncios/'.$tmpname);
  76.                     $ratio = $width_orig / $heigth_orig;
  77.  
  78.                     $width = 500;
  79.                     $heigth = 500;
  80.  
  81.                     if ($width/$heigth > $ratio){
  82.                         $width = $heigth * $ratio;
  83.                     } else{
  84.                         $heigth = $width / $ratio;
  85.                     }
  86.  
  87.                     $img = imagecreatetruecolor($width, $heigth);
  88.                     if ($tipo == 'image/jpeg'){
  89.                         $origi = imagecreatefromjpeg('images/anuncios/'.$tmpname);
  90.                     }elseif ($tipo == 'image/png'){
  91.                         $origi = imagecreatefrompng('images/anuncios/'.$tmpname);
  92.                     }
  93.  
  94.                     imagecopyresampled($img, $origi, 0, 0, 0, 0, $width, $heigth, $width_orig, $heigth_orig);
  95.                     imagejpeg($img, 'images/anuncios/'.$tmpname, 80);
  96.  
  97.                     $sql = $con->prepare("INSERT INTO anuncios_imagens SET id_anuncio = :id_anuncio, url = :url");
  98.                     $sql->bindValue(':id_anuncio', $id);
  99.                     $sql->bindValue(':url', $tmpname);
  100.                     $sql->execute();
  101.  
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     public function excluirAnuncio($id){
  108.         global $con;
  109.  
  110.         $sql = $con->prepare("DELETE FROM anuncios_imagens WHERE id_anuncio = :id");
  111.         $sql->bindValue(":id", $id);
  112.         $sql->execute();
  113.  
  114.         $sql = $con->prepare("DELETE FROM anuncios WHERE id = :id");
  115.         $sql->bindValue(":id", $id);
  116.         $sql->execute();
  117.     }
  118.  
  119.     public  function excluirFoto($id){
  120.         global $con;
  121.  
  122.         $id_anuncio = 0;
  123.  
  124.         $sql = $con->prepare("SELECT id_anuncio FROM id_anuncios_imagens WHERE id = :id");
  125.         $sql->bindValue(":id", $id);
  126.         $sql->execute();
  127.  
  128.         if ($sql->rowCount() > 0){
  129.             $row = $sql->fetch();
  130.             $id_anuncio = $row['id_anuncio'];
  131.         }
  132.  
  133.         $sql = $con->prepare("DELETE FROM anuncios_imagens WHERE id = :id");
  134.         $sql->bindValue(":id", $id);
  135.         $sql->execute();
  136.  
  137.         return $id_anuncio;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement