<?php 




class DaoNoticias extends conexao {

public function Atualizar(noticias $noticias) {

try {

if ($noticias->getnti_id() != '') {

$sql = " UPDATE noticias SET
nti_titulo = ?,
nti_descricao = ?,
nti_imagem = ?,
nti_categoria = ?
WHERE nti_id = ? ";

$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $noticias->getnti_titulo());
$stmt->bindValue(2, $noticias->getnti_descricao());
$stmt->bindValue(3, $noticias->getnti_imagem());
$stmt->bindValue(4, $noticias->getnti_categoria());
$stmt->bindValue(5, $noticias->getnti_id());


} else {

$sql = " INSERT INTO noticias (nti_titulo,nti_descricao,nti_imagem,nti_categoria)
VALUES (?,?,?,?)";


$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $noticias->getnti_titulo());
$stmt->bindValue(2, $noticias->getnti_descricao());
$stmt->bindValue(3, $noticias->getnti_imagem());
$stmt->bindValue(4, $noticias->getnti_categoria());


}
if ($stmt->execute()){
return true;
} else
return false;

} catch (Exception $e) {
print "Erro ao tentar atualizar: ".$e;
}

}

public function PesquisarNoticias($codigo){
$sql = " SELECT * FROM noticias WHERE nti_id = ? ";
$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $codigo);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}


public function PesquisarNoticiasCategoria($codigo){
$sql = " SELECT * FROM noticias WHERE nti_categoria = ? ";
$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $codigo);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}


public function PesquisarBusca($codigo){

$sql = " SELECT * FROM noticias WHERE nti_descricao LIKE '%$codigo%' ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

public function ContarProdutos(){
$sql = "SELECT COUNT(pro_id) AS NumberOfProduto FROM produtos; ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
	foreach($result as $row){
		return $row['NumberOfProduto'];
	}
}


public function ContarNoticias(){
$sql = "SELECT COUNT(nti_id) AS NumberOfNoticias FROM noticias; ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
	foreach($result as $row){
		return $row['NumberOfNoticias'];
	}
}


public function ListarNoticias(){
$sql = " SELECT * FROM noticias ORDER BY nti_id DESC ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

public function ListarNoticiasSaude(){
$sql = " SELECT * FROM noticias ORDER BY nti_id WHERE nti_categoria='Saúde' ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

public function ListarNoticias5(){
$sql = " SELECT * FROM noticias ORDER BY nti_id LIMIT 5 ";
$stmt = self::conectar()->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}


public function deletar($nti_id){		
$sql = " DELETE FROM servicos WHERE nti_id = ? ";
$stmt = self::conectar()->prepare($sql);
$stmt->bindValue(1, $nti_id); 
$stmt->execute();
}

 
}
