Jump to content

notifications system not working


shan2batman

Recommended Posts

Hi, guys i’m trying to create a notification alert for private message section in my project. The problem is everytime when the sender is sending message the notification bar shows new msg’s in sender profile and not in receivers profile. I don’t know where i’m going wrong so can any of you experts solve it. i’ll post the code here

The private message page code:

The db from where the queries are called from:

CREATE TABLE IF NOT EXISTS `private_message` (

`pm_id` int(255) NOT NULL AUTO_INCREMENT,

 `r_seen` enum('0','1') NOT NULL,
  
`receiver` varchar(255) NOT NULL,

`sender` varchar(255) NOT NULL,

`sent_time` datetime NOT NULL,

`subject` varchar(255) NOT NULL,

`message` text NOT NULL,

`sdelete` enum('0','1') NOT NULL,

`rdelete` enum('0','1') NOT NULL,

`parent` varchar(255) NOT NULL,

`has_replies` enum('0','1') NOT NULL,

`rread` enum('0','1') NOT NULL,

`sread` enum('0','1') NOT NULL,

`receiver_uid` int(255) NOT NULL,

`sender_uid` int(255) NOT NULL,

`os_id` int(255) DEFAULT NULL,

UNIQUE KEY `pm_id_2` (`pm_id`),

KEY `pm_id` (`pm_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=95 ;

The private message page code:

<?php



//error_reporting(0);

include 'includes/header.php';

if (!isset($_SESSION['app'])) {



header("location: index.php?usernotfound?id=017");

}



$updateid="";

$title="";

$data="";

$sess_id=(int)$_SESSION['id'];

$sess_uname = htmlentities($_SESSION['uname']);

$session_uname = stripslashes($sess_uname);

$f_uname0 = htmlentities($_GET['u']);

$f_uname = stripslashes($f_uname0);

$sql = "select * from user where uname=:uname and activated='1' limit 1";

$stmt = $conn->prepare($sql);

$stmt->bindparam(":uname", $f_uname);

$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

$_SESSION['f_pm']=$f_uname;

$f_uid=$row[0]['user_id'];

$fname_s = $row[0]['fname'];

$lname_s = $row[0]['lname'];

$uname_s = $row[0]['uname'];

$pmid= htmlentities($_GET['pmid']);



if(isset($pmid)){

$updatedelete="update private_message set sread='1', r_seen='1' where pm_id=:pmid ";

$stmt_r=$conn->prepare($updatedelete);

//$stmt->bindValue(":originator",$originator);

$stmt_r->bindValue(":pmid",$pmid);

$stmt_r->execute();

}



$is_friend=FALSE;

$ownerblockviewer=FALSE;

$viewerblockowner=FALSE;

$isowner="no";

if($f_uid==(int)$_SESSION['id']&&isset($_SESSION['app'])){

$isowner="yes";

}

if ($f_uname != $session_uname &&$_SESSION['app']==TRUE) {

$freind_check = "select friend_id from friends where friend_one_uid=:session and friend_two_uid=:f_name and accepted='1' or friend_one_uid=:f_name and friend_two_uid=:session and accepted='1' limit 1";

$stmt = $conn->prepare($freind_check);

$stmt->bindparam(":session", $sess_id);

$stmt->bindparam(":f_name", $f_uid);

$stmt->execute();

$isfriend=$stmt->rowCount();

if ($isfriend>0) {



$is_friend = TRUE;

}

$blockcheck0 = "select blocked_id from blockedusers where blocker_uid=:f_name and blockee_uid=:session limit 1";

$stmt1 = $conn->prepare($blockcheck0);

$stmt1->bindValue(":session", $sess_id);

$stmt1->bindValue(":f_name", $f_uid);

$stmt1->execute();

$owner= $stmt1->rowCount();



if ( $stmt1->rowCount()>0) {

$ownerblockviewer = TRUE;

}

$blockcheck2 = "select blocked_id from blockedusers where blocker_uid=:session and blockee_uid=:f_name limit 1";

$stmt2 = $conn->prepare($blockcheck2);

$stmt2->bindparam(":session", $sess_id);

$stmt2->bindparam(":f_name", $f_uid);

$stmt2->execute();

$viewer=$stmt2->rowCount();

if ($stmt2->rowCount() > 0) {

$viewerblockowner = TRUE;

}

}

$pm_ui="";

?>



<div class="container-fluid">

<br><div class="row"> </div><br>

<div class="row">

<div class="col-sm-2 col-md-3 col-xs-4">

<div class="friend_pm">



<?php include 'notification/friend_pm.php'; ?>



</div>

</div>

<div class="col-lg-8 col-xs-8 col-md-9">

<?php

#if visitor to profile is friend and is not the owner, can send private message.

#build ui carry the profile id, visitorname,pm subject and comments.

if($is_friend==TRUE && $isowner=="no"){

$sql1="select * from private_message where(receiver=:u and sender=:sess and rdelete='0') "

. "or "

. "(sender=:u and receiver=:sess and sdelete='0' ) order by sent_time desc ";

$stmt1=$conn->prepare($sql1);

$stmt1->bindValue(":u",$f_uname);

$stmt1->bindValue(":sess",$_SESSION['uname']);

$stmt1->execute();

$fetch=$stmt1->fetchAll(PDO::FETCH_ASSOC);

foreach ( $fetch as $row1)

$pmid=$row1['pm_id'];

$pm_ui='<hr><br><br>'

. '<a class="btn mark-all mark_all_as_read" value="mark_all" originator="'. htmlentities($_SESSION['uname']).'" id="'.$pmid.'">Mark All as read</a><br>'

. '<textarea id="pmtext" class="pm_message input-custom2" placeholder="Send a message"></textarea>'

. '<br>'

. '<button class="btn btn-warning pm_btn" attr="'.$row1['sender'].'" id="'.$f_uname.'" value="'.$pmid.'" type="new_pm" name="'.$session_uname.'">'



. 'Send</button>';

}



?>

<div id="status_ui">



<?php



echo "<br><br><br><br><br><br><div class='jumbotron scroll-custom'>"



. "<div class='pm_post'>";

include "notification/pm_inbox.php";

echo "</div></div>";

?>

<?php

if($is_friend==TRUE&& $isowner=="no"){



echo $pm_ui;

}

?>



</div>

<div class="col-sm-2 hidden-xs hidden-md">



</div>

</div>

</div>

<script type="text/javascript">

var spinner="<img src='img/spinner.gif'>";



$(".pm_btn").click(function(){

var f_uname=$(this).attr('id');

var action=$(this).attr('type');

var s_uname=$(this).attr('name');

var pm_message=$(".pm_message").val();

var os_id=$(this).attr("value");

if(pm_message==""){alert("Please write something to send");

}else{

$.post("parser/pm_system.php",

{"f_uname":f_uname,"action":action,"sess_uname":s_uname,"pm_message":pm_message,"os_id":os_id},

function(data){

$(".pm_message").val("");

// alert(data);

// $(".jumbotron").prepend("<small>Reply from:-"+s_uname+" right now </small><br><p>"+pm_message+"</p>");

//lets the script scroll down to the bottom of the chat class automatically

$(".scroll-custom").scrollTop($(".scroll-custom")[0].scrollHeight);



});}

});

$(".mark_as_read").click(function(){

var action=$(this).attr("value");

var id=$(this).attr("id");

var originator=$(this).attr("originator");

$.post("parser/pm_system.php",

{"action":action,"pmid":id,"originator":originator},

function(data){

alert(data);

});



});



$(".mark-all").click(function(){



var action=$(this).attr("value");

var id=$(this).attr("id");

var originator=$(this).attr("originator");

//alert(action+""+id+""+);

$.post("parser/pm_system.php",

{"action":action,"pmid":id,"originator":originator},

function(data){

alert(data);

});



});

//logic for search box

$(".search").keyup(function(){

var input=$(".search").val();

var length=input.length;

$("#result").html(spinner).show();

var check=$(".checkbox-search").prop('checked');



if(check!=true){

$.post("search.php",({"(un|fn|ln)":input}),function(result){



$("#result").html(result).show();

});

}else if(check==true){

var input=$(".search").val();

$("#result").html(spinner).show();

$.post("search_group.php",({"group_name":input}),function(result){



$("#result").html(result).show();

});

}

});

$(document).click(function(){



$("#result").hide();

});



function load_stuff(){

$(".pm_post").load("notification/pm_inbox.php");

}

//loads friends who have sent messages

function load_names(){

$(".friend_pm").load("notification/friend_pm.php");

}

setInterval(function(){

load_stuff();

load_names();

},3000);

$(document).click(function(){



$("#result").hide();

});

$("button.friend-add-btn").click(function(){

var id=$(this).attr('id');

var name=$(this).attr('name');



$(".spinner-friend-add"+id).html(spinner).show();

$.post(

"parser/friend_sys.php",

({"user": name,"friend_one_uid":id}),

function (data) {



alert(data);

$(".spinner-friend-add"+id).html(spinner).hide();

$(".friend_fade_"+id).fadeOut();

}

);

});

$('[data-toggle="popover"]').popover();

//logic to accept fried request

$(document).on("click", "button.accept_btn" ,function(){

var accept_id=$(this).attr('id');

var sess=$(this).attr('sess');

var name=$(this).attr('name');



$(".show-spinner").html(spinner).show();

$.post("notification/friend_req_accept.php",({"reqid":accept_id,"friend_two_uid":sess,"friend_two":name}),function(data){

$(".show-spinner").hide();

alert(data);

});



});

//logic to reject friend request

$(document).on("click", "button.reject_btn" ,function(){

var accept_id=$(this).attr('id');

$(".show-spinner").html(spinner).show();

$.post("notification/friend_req_reject.php",({"reqid":accept_id}),function(data){

$(".show-spinner").hide();

alert(data);



});



});



//logic for sending friend requests

// $("button.friend_btn").click(function () {

$(document).on("click", "button.friend_btn",function(){

alert("see");

$(".friend").html(spinner).show();

var user_f = $(this).attr('user');



var id = $(this).attr('sess');

var name=$(this).attr('name');

$.post(

"parser/friend_sys.php",

({"user": user_f,"friend_one_uid":id}),

function (data) {

alert(data);

$(".friend").html(spinner).hide();



}

);

//$(".friend").html(spinner).fadeOut(250);

});

//logic to like this person button.



$(document).on("click", "button.like_person_btn",function(){

$(".friend").html(spinner).show();

var user_f = $(this).attr('user');

var userid=$(this).attr('userid');

var id = $(this).attr('sess');

var name=$(this).attr('name');

$.post(

"parser/like_this_person_parser.php",

({"user": user_f,"like_person_one_uid":id,"like_person_two_uid":userid}),

function (data) {

alert(data);

$(".friend").html(spinner).hide();



}

);

//$(".friend").html(spinner).fadeOut(250);

});



//logic for blocking friends



$(document).on("click", "button.block_btn",function(){

$(".show-spinner").html(spinner).show();

var user_f = $(this).attr('user');

$.post(

"parser/block_system.php",

({"user": user_f}),

function (data) {

alert(data);



}

);

$(".show-spinner").html(spinner).fadeOut(250);

});





$(document).on("click", "button.unfreind_btn",function(){

$(".show-spinner").html(spinner).show();

var user_f = $(this).attr('user');

$.post(

"parser/unfriend_sys.php",

{"user": user_f},

function (data) {

alert(data);



}

);

$(".show-spinner").html(spinner).fadeOut(250);

});

//logic for unblocking users whom were blocked



$(document).on("click", "button.unblock_btn",function(){

$(".show-spinner").html(spinner).show();

var user_f = $(this).attr('user');

$.post(

"parser/unblock_sys.php",

({"user": user_f}),

function (data) {

alert("You have unblocked this user");

$("button.unblock_btn").hide();

$("button.block_btn").show();

}

);

$(".show-spinner").html(spinner).fadeOut(250);

});

//to load notifications dynamically i used the set interval method.



function load_notifications(thisview=''){

var theData = {

view: thisview

}

$.ajax({

url: "notification/new_friends.php",

method: "POST",

data:theData,

dataType:"json",

success: function(data){

// alert(data);

$(".drop-menu").html(data.notification);

if(data.unseen_notification>0){

$(".badge1").html(data.unseen_notification);

}else{

//alert(data);

$(".drop-menu").html("No Notifications");

$(".badge1").html(data.unseen_notification);}

}



});

};

function notification(thisview=''){

var theData= {

view: thisview

}

$.ajax({

url:"notification/ajax_notifications.php",

method: "POST",

data:theData,

dataType: "json",

success: function(data){

$(".notify-menu").html(data.notifications);

if(data.unseen_notifications>0){

$(".badge3").html(data.unseen_notifications);

}

}



});

}

function notification_pm(thisview=''){

var theData= {

view: thisview

}

var f_uname=$(".badge_pm").attr("type");

var sender=$(".badge_pm").attr("attr");

var sess=$(".badge_pm").attr("name");

var rcvr =$(".badge_pm").attr("id");



$.ajax({

url:"notification/ajax-pm-notification.php",

method: "POST",

data:{"theData":theData, "f_uname": f_uname},

dataType: "html",

success: function(data){

if(data>0){

// alert(data);



$(".badge_pm").html("New Msg's");



}else{

// alert(data);

$(".badge_pm").html("No Msg's");

}

}

});





}

//clicking on notification icon in header does this

$(document).on("click", ".count_notification", function(){

var id='';

$.post("notification/count_notifications.php", {id:id}, function(data){



});

});

//load_notifications();

$(document).on("click",".count_friend", function(){



load_notifications('yes');

});

//loads every 2 seconds for chat

setInterval(function(){

load_notifications();

notification();

notification_pm();

},5000);

</script>

</body>

</html>

 

the ajax notification page:

<?php

include '../includes/dbconfig.inc.php';

$f_uname= htmlentities($_POST['f_uname']);

$mark="select parent,r_seen,rread,sread,receiver,sender from private_message where (receiver=:sess1 or sender=:sess2) and (r_seen='1' or r_seen='0') and (sread='1' or sread='0')";

$stmt_m=$conn->prepare($mark);

$stmt_m->bindValue(":sess1", $_SESSION['uname']);

$stmt_m->bindValue(":sess2", $_SESSION['uname']);

//$stmt_m->bindValue(":f_uname", $f_uname);

$stmt_m->execute();

$column=$stmt_m->fetch();









if ($column['r_seen']=='0'&&$column['parent']==$_SESSION['uname']) {

echo'1';

// print_r($column);



} elseif ($column['r_seen']=='1'&&$column['parent']==$_SESSION['uname']) {

echo '0';

// echo 'old';

}elseif ($column['r_seen']=='0'&&$column['parent']!=$_SESSION['uname']) {



echo '1';

//echo'new';



}elseif ($column['r_seen']=='1'&&$column['parent']!=$_SESSION['uname'])

{

echo '0';

// echo'new11';

}

else {

echo '0';

//print_r($f_uname);



}

logic for notifications in header:


 

$mark="select rread,sread,receiver,sender from private_message where (receiver=:sess or sender=:sess) and has_replies='1' and rread='1' and sread='0'";

$stmt_m=$conn->prepare($mark);

$stmt_m->bindValue(":sess", $_SESSION['uname']);

//$stmt_m->bindValue(":f_uname", $f_uname);

$stmt_m->execute();

$column=$stmt_m->fetch();







if($column['receiver']==$_SESSION['uname']&&$column['rread']>0&&$column['sread']==0)

{

if ($column['sender']!=$_SESSION['uname'])

{

print_r($column);

$pm_n="<span class='badge_pm label label-pill label-success' type='".$f_uname."'>New Msg's</span><a href='private_message.php?u=".$_SESSION['uname']."'><img src='img/logo/message-white.png' alt='private_message'></a>";

} else {

$pm_n="<span class='badge_pm label label-pill label-success' type='".$f_uname."'></span><a href='private_message.php?u=".$_SESSION['uname']."'><img src='img/logo/message-black.png' alt='private_message_alert'></a>";

}





}elseif ($column['sender']==$_SESSION['uname']&&$column['rread']==1&&$column['sread']==0)

{

if ($column['receiver']!=$_SESSION['uname']) {

$pm_n="<span class='badge_pm label label-pill label-success' type='".$f_uname."'></span><a href='private_message.php?u=".$_SESSION['uname']."'><img src='img/logo/message-black.png' alt='private_message_alert'></a>";

} else {

print_r($f_uname);

$pm_n="<span class='badge_pm label label-pill label-success' type='".$f_uname."'>New Msg's</span><a href='private_message.php?u=".$_SESSION['uname']."'><img src='img/logo/message-white.png' alt='private_message'></a>";

}



}else

{

$pm_n="<span class='badge_pm label label-pill label-success' type='".$f_uname."'></span><a href='private_message.php?u=".$_SESSION['uname']."'><img src='img/logo/message-black.png' alt='private_message_alert'></a>";

}

 

Link to comment
Share on other sites

Why does your query not do the 'parent' selection for you instead of checking the results later?

As for whatever your problem is I find it impossible to follow what you are attempting to do here. 

Turn on error checking in every script your are using here until you have it fully developed and debugged.

Link to comment
Share on other sites

here is the code for private message part where the user can turn off the notification by clicking the mark as read button.

 

   
      if(isset($_POST['action'])&&$_POST['action']=="mark_all"){
       if (!isset($_POST['pmid'])||$_POST['pmid']=="") {
           echo "Id is missing contact shan";
           exit();
           }
       $originator= htmlentities($_POST['originator']);
           $pmid=$_POST['pmid'];
       $marked1="select os_id from private_message where pm_id=:os_id";
       $stmth=$conn->prepare($marked1);
       $stmth->bindValue(":os_id", $pmid);
       $stmth->execute();
       $fetch=$stmth->fetchAll();
       if ($fetch[0][0]=='0') {
               if ($originator==$_SESSION['uname']) {
$updatedelete="update private_message set sread='1', r_seen='1', os_id=:pmid where pm_id=:pmid";    
$stmt=$conn->prepare($updatedelete);
//$stmt->bindValue(":originator",$originator);
$stmt->bindValue(":pmid",$pmid);
$stmt->execute();
       echo "you have marked it as read";
       exit();

}
       elseif ($originator!=$_SESSION['uname']) {
$updatedelete="update private_message set rread='1', os_id=:pmid,  where pm_id=:pmid";    
$stmt=$conn->prepare($updatedelete);
//$stmt->bindValue(":originator",$originator);
$stmt->bindValue(":pmid",$pmid);
$stmt->execute();
       echo "you have marked it as read";
       exit();

}
       }  else {
           
       
       if(!isset($_POST['originator'])&&$_POST['originator']==""){
       echo "Message sender is missing";
       exit();    
       }
      
       #see who is marking it as read.
       if ($originator==$_SESSION['uname']) {
$updatedelete="update private_message set sread='1' ,r_seen='1' where os_id=:pmid";    
$stmt=$conn->prepare($updatedelete);
//$stmt->bindValue(":originator",$originator);
$stmt->bindValue(":pmid",$pmid);
$stmt->execute();
       echo "you have marked it as read";
       exit();

}
       elseif ($originator!=$_SESSION['uname']) {
$updatedelete="update private_message set rread='1' , r_seen='1' where os_id=:pmid";    
$stmt=$conn->prepare($updatedelete);
//$stmt->bindValue(":originator",$originator);
$stmt->bindValue(":pmid",$pmid);
$stmt->execute();
       echo "you have marked it as read ";
       exit();

}
   }
   
       }

 

Link to comment
Share on other sites

A few comments about the design of your "private_mesage" table

  • pm_id int(255) - you cannot store integers with 255 digits (max 10 digits if unsigned = 4294967295). If you add one record every second you will run out in about 130 years. If this is not sufficient, make it BIGINT (20 digits). Make pm_id the primary key (not a key and a unique key).
  • Don't use numeric values in enums. They are designed for things like "size enum('S', 'M', 'L', 'XL', 'XXL')". Just use TINYINT with values of 0 or 1.
  • receiver and sender names should not be in this table, just their uid (which you have). The names should be stored once only in the user table.

Thus the table would be like this:

CREATE TABLE IF NOT EXISTS `private_message` (

`pm_id` int NOT NULL AUTO_INCREMENT,
`r_seen` tinyint NOT NULL,
`sent_time` datetime NOT NULL,
`subject` varchar(255) NOT NULL,
`message` text NOT NULL,
`sdelete` tinyint NOT NULL,
`rdelete` tinyint NOT NULL,
`parent` int NOT NULL,
`has_replies` tinyint NOT NULL,
`rread` tinyint NOT NULL,
`sread` tinyint NOT NULL,
`receiver_uid` int NOT NULL,
`sender_uid` int NOT NULL,
`os_id` int DEFAULT NULL,
PRIMARY KEY (pm_id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.