Reputation: 349
I have got 2 pages:
Fun.php
Class.fun.php
In class.fun.php
I got functions, for example, select posts/upvote post/downvote post.
In Fun.php
I'm displaying all posts (Like facebook posts) : Image
When I upvote/downvote
post, it calls function in class.fun.php
and refreshes page, but goes back to top. : Image
But I want page to stay or go back to same post, where I clicked upvote
, so I could scroll forward.
My Upvote function:
public function upvotePost(){
try
{
if(isset($_SESSION['user_session'])){
$user_id = $_SESSION['user_session'];
$stmt = $this->runQuery("SELECT * FROM users WHERE id=:id");
$stmt->execute(array(":id"=>$user_id));
$myRow=$stmt->fetch(PDO::FETCH_ASSOC);
}
$id = $_GET['image_id'];
$user_id = $myRow['id'];
$stmt2 = $this->conn->prepare("SELECT count(*) FROM fun_post_upvotes WHERE image_id=('$id') AND user_id=('$user_id')");
$stmt2->execute();
$result2 = $stmt2->fetchColumn();
if($result2 == 0){
$stmt3 = $this->conn->prepare("INSERT INTO fun_post_upvotes (image_id,user_id) VALUES(:image_id,:user_id)");
$stmt3->bindparam(":image_id", $id);
$stmt3->bindparam(":user_id", $user_id);
$stmt3->execute();
$stmt4 = $this->conn->prepare("SELECT * FROM fun_posts WHERE id=('$id')");
$stmt4->execute();
$result4 = $stmt4->fetchAll();
foreach($result4 as $post){
$newUpvotes = $post['upvotes']+1;
$stmt5 = $this->conn->prepare("UPDATE fun_posts SET upvotes=$newUpvotes WHERE id=('$id')");
$stmt5->execute();
$_SESSION["result"]='You have succesfully liked this post!';
}
}else{
$_SESSION["error"]='You have already liked this post!';
}
$stmt6 = $this->conn->prepare("SELECT count(*) FROM fun_post_downvotes WHERE image_id=('$id') AND user_id=('$user_id')");
$stmt6->execute();
$result6 = $stmt6->fetchColumn();
if($result6 > 0){
$stmt7 = $this->conn->prepare("DELETE FROM fun_post_downvotes WHERE image_id=('$id') AND user_id=('$user_id')");
$stmt7->execute();
$stmt8 = $this->conn->prepare("SELECT * FROM fun_posts WHERE id=('$id')");
$stmt8->execute();
$result8 = $stmt8->fetchAll();
foreach($result8 as $post){
$newDownvotes = $post['downvotes'] - 1;
$stmt9 = $this->conn->prepare("UPDATE fun_posts SET downvotes=$newDownvotes WHERE id=('$id')");
$stmt9->execute();
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Upvotes: 0
Views: 187
Reputation:
Take a look at HTML anchors for a very simple solution, assuming you are using a hyperlink for voting. You would need the container of each post to have it's own unique ID attribute, which you then refer to in your upvote/downvote href attribute, creating an anchor which the browser will jump to. See https://www.w3schools.com/html/html_links.asp For example -
<div id="post_1">
Post content is here
<a href="yoururl.com/posts#post_1">UPVOTE</a>
<a href="yoururl.com/posts#post_1">DOWNVOTE</a>
</div>
<div id="post_2">
Post content is here
<a href="yoururl.com/posts#post_2">UPVOTE</a>
<a href="yoururl.com/posts#post_2">DOWNVOTE</a>
</div>
Alternatively look into using javascript ajax to post the data without reloading the page and therefore no need to scroll at all. See https://www.w3schools.com/xml/ajax_intro.asp for an introduction, or check out a JS library like jQuery.
Upvotes: 1