Skip to content

Commit

Permalink
Add/Remove from Favorite list
Browse files Browse the repository at this point in the history
  • Loading branch information
billzyj committed Nov 19, 2021
1 parent 22a6e03 commit 6d757e2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions addtofavorites.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
require_once("files/main.php");
$vidId=$_POST['favoriteButton'];
$vidId=(int)$_GET['Id'];
$checkquery = $con -> prepare("SELECT * from favorites where userName='$loggedInUserName' and videoId = '$vidId'");
$checkquery -> execute();
if($checkquery->rowCount()==0){
$query = $con -> prepare("INSERT into favorites (videoId, userName) VALUES ('$vidId', '$loggedInUserName')");
$query->execute();
echo "Video has been added to favorites";
header("location:watch.php?Id=$vidId&add=success");
}
else{
echo"Video is already in favorites";
Expand Down
2 changes: 1 addition & 1 deletion favorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<?php
if($loggedInUserName!=""){
$mediaGrid= new MediaGrid($con);
echo $mediaGrid->create(null, "My Favorites", $loggedInUserName);
echo $mediaGrid->create('Favorite', "", "","", $loggedInUserName);
}
?>
</div>
11 changes: 11 additions & 0 deletions files/Classes/MediaGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public function createMySharedContentBlock($type, $sortBy, $loggedInUserName, $h
return $this->createBlock("Shared ".$type."s", $gridItems);
}

public function createMyFavoriteContentBlock($type, $sortBy, $loggedInUserName, $hide = 'not hidden')
{
$gridItems = $this->getFavorite($type, $sortBy, $loggedInUserName);
return $this->createBlock("My Favorite ".$type."s", $gridItems);
}

public function create($page, $category, $keywords, $sortBy, $loggedInUserName, $videoId = 1, $hide = 'not hidden')
{
if ($page == 'Home') {
Expand All @@ -97,6 +103,11 @@ public function create($page, $category, $keywords, $sortBy, $loggedInUserName,
if ($page == 'MyList') {

}

if ($page == 'Favorite') {
return $this->createMyFavoriteContentBlock('video', $sortBy, $loggedInUserName)
.$this->createMyFavoriteContentBlock('image', $sortBy, $loggedInUserName);
}
}

public function getSuggestions($videoId)
Expand Down
13 changes: 13 additions & 0 deletions removeFromFavorite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
require_once("files/main.php");
$vidId = (int)$_GET['Id'];
$checkquery = $con->prepare("SELECT * from favorites where userName='$loggedInUserName' and videoId = '$vidId'");
$checkquery->execute();
if ($checkquery->rowCount() > 0) {
$query = $con->prepare("DELETE FROM favorites WHERE userName='$loggedInUserName' and videoId = '$vidId'");
$query->execute();
header("location:watch.php?Id=$vidId&delete=success");
} else {
echo "Video is already not in favorites";
}
?>
28 changes: 25 additions & 3 deletions watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,32 @@
echo "<button type='submit' value='$vidId' name='playlistButton'>Add to Playlist</button>
</form>";

echo "<form action='addtofavorites.php' method='POST' >
<button type='submit' value='$vidId' name='favoriteButton'>Favorite</button>
</form>";
$checkquery = $con -> prepare("SELECT * from favorites where userName='$loggedInUserName' and videoId = '$vidId'");
$checkquery -> execute();
if($checkquery->rowCount()==0){
echo "<form action='addtofavorites.php' method='GET' >
<button type='submit' value='$vidId' name='Id'>Add to Favorite</button>
</form>";

}
else{
echo "<form action='removeFromFavorite.php' method='GET' >
<button type='submit' value='$vidId' name='Id'>Remove from Favorite</button>
</form>";

}

if (isset($_GET["add"]) && $_GET["add"] == 'success') {
echo "<div class='badge'>
<p style = 'color:red'>Successfully added to Favorite!</p>
</div>";
}

if (isset($_GET["delete"]) && $_GET["delete"] == 'success') {
echo "<div class='badge'>
<p style = 'color:red'>Successfully removed From Favorite!</p>
</div>";
}
?>
</div>

Expand Down

0 comments on commit 6d757e2

Please sign in to comment.