CREATE TABLE IF NOT EXISTS `gostos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idVideo` int(11) NOT NULL,
`data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`idUser` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `naogostos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idVideo` int(11) NOT NULL,
`data` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`idUser` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `videos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(100) NOT NULL,
`titulo` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `videos` (`id`, `url`, `titulo`) VALUES
(1, 'https://www.youtube.com/embed/Yq4KA0mUnC8', 'Dream On - Postmodern Jukebox ft. Morgan James (Aerosmith Cover)'),
(2, 'https://www.youtube.com/embed/1_ekta_LzS0', 'Hallelujah by Jeff Buckley (Morgan James cover)'),
(3, 'https://www.youtube.com/embed/TvnYmWpD_T8', 'Prince- Purple Rain');
LISTAR VIDEOS.
listarVideos.php
<?php
$ligar=mysqli_connect("localhost", "root", "usbw", "youtube");
$sql="select * from videos";
$query=mysqli_query($ligar, $sql);
$fetch=mysqli_fetch_assoc($query);
?>
<!DOCTYPE html>
<html lang="pt">
<!-- http://localhost:8080/aula-15-16/listarVideos.php -->
<head>
<meta charset="UTF-8">
<title>Videos</title>
</head>
<body>
<?php do {?>
<div>
<div>
<h2><a href="videosFiltar.php?id=<?php echo $fetch['id'];?>"><?php echo $fetch['titulo'];?></a></h2>
</div>
<div><iframe width="560" height="315" src="<?php echo $fetch['url'];?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>
</div>
<hr>
<?php } while($fetch=mysqli_fetch_assoc($query));?>
</body>
</html>
VIDEO FILTRAR
videosFiltrar.php
<?php
if(isset($_GET['id'])){
$ligar=mysqli_connect("localhost", "root", "usbw", "youtube");
$id=$_GET['id'];
$sql="select * from videos where id=$id";
$query=mysqli_query($ligar, $sql);
$fetch=mysqli_fetch_assoc($query);
$sql="select count(id) total from gostos where idVideo=$id";
$queryGostos=mysqli_query($ligar, $sql);
$fetchGostos=mysqli_fetch_assoc($queryGostos);
$sql="select count(id) totalNaoGostos from naogostos where idVideo=$id";
$queryNaoGostos=mysqli_query($ligar, $sql);
$fetchNaoGostos=mysqli_fetch_assoc($queryNaoGostos);
}else{
header("Location:listarVideos.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vídeo</title>
<script>
function gostar(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contaGostos").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gostar.php?id=" + id, true);
xmlhttp.send();
}
function naoGostar(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contaNaoGostos").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "naoGostar.php?id=" + id, true);
xmlhttp.send();
}
</script>
</head>
<body>
<div>
<div>
<h2>
<a href="videosFiltar.php?id=<?php echo $fetch['id'];?>">
<?php echo $fetch['titulo'];?>
</a>
</h2>
</div>
<div><iframe width="560" height="315" src="<?php echo $fetch['url'];?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>
</div>
<div><span><button type="button" id="idVideo" value="<?php echo $fetch['id'];?>" onclick="gostar(this.value)">GOSTAR</button></span> | <span id="contaGostos"><?php echo $fetchGostos['total'];?></span>
<span><button type="button" id="idVideo" value="<?php echo $fetch['id'];?>" onclick="naoGostar(this.value)">NÃO GOSTAR</button></span> | <span id="contaNaoGostos"><?php echo $fetchNaoGostos['totalNaoGostos'];?></span>
</div>
</body>
</html>
GOSTAR
gostar.php
<?php
if(isset($_GET['id'])){
$idUser=1;
$ligar=mysqli_connect("localhost", "root", "usbw", "youtube");
$id=$_GET['id'];
// perguntar se o idUser já votou no IdVideo
$sql="select * from gostos where idVideo=$id and idUser=$idUser";
$queryTeste=mysqli_query($ligar, $sql);
$totalTeste=mysqli_num_rows($queryTeste);
if($totalTeste==0){
$sql="insert into gostos (idVideo, idUser) values ($id, $idUser)";
mysqli_query($ligar, $sql);
// testar se nao gostei
$sql="select * from naogostos where idVideo=$id and idUser=$idUser";
$queryTesteNaoGostos=mysqli_query($ligar, $sql);
$totalTesteNaoGostos=mysqli_num_rows($queryTesteNaoGostos);
if($totalTesteNaoGostos>0){
// apagar
$sql="delete from naogostos where idVideo=$id and idUser =$idUser limit 1";
mysqli_query($ligar, $sql);
}
}else{
$sql="delete from gostos where idVideo=$id and idUser =$idUser limit 1";
mysqli_query($ligar, $sql);
}
$sql="select count(id) total from gostos where idVideo=$id";
$query=mysqli_query($ligar, $sql);
$fetch=mysqli_fetch_assoc($query);
echo $fetch['total'];
}else{
header("Location:listarVideos.php");
}
?>
NÃO GOSTAR
naogostar.php
<?php
if(isset($_GET['id'])){
$idUser=1;
$ligar=mysqli_connect("localhost", "root", "usbw", "youtube");
$id=$_GET['id'];
// perguntar se o idUser já votou no IdVideo
$sql="select * from naogostos where idVideo=$id and idUser=$idUser";
$queryTeste=mysqli_query($ligar, $sql);
$totalTeste=mysqli_num_rows($queryTeste);
if($totalTeste==0){
$sql="insert into naogostos (idVideo, idUser) values ($id, $idUser)";
mysqli_query($ligar, $sql);
// testar se nao gostei
$sql="select * from gostos where idVideo=$id and idUser=$idUser";
$queryTesteGostos=mysqli_query($ligar, $sql);
$totalTesteGostos=mysqli_num_rows($queryTesteGostos);
if($totalTesteGostos>0){
// apagar
$sql="delete from gostos where idVideo=$id and idUser =$idUser limit 1";
mysqli_query($ligar, $sql);
}
}else{
$sql="delete from naogostos where idVideo=$id and idUser =$idUser limit 1";
mysqli_query($ligar, $sql);
}
$sql="select count(id) total from naogostos where idVideo=$id";
$query=mysqli_query($ligar, $sql);
$fetch=mysqli_fetch_assoc($query);
echo $fetch['total'];
}else{
header("Location:listarVideos.php");
}
?>
Sem comentários:
Enviar um comentário