I wont to make a notification which notices user of number of new items since his last visit.
I have a code that does exactly that but there is a problem, if user refreshes a page or lives to another page on same domain and comes back the number is set to 0. I need a way to number of new items stays the same until user closes the window or session is expired.
Code currently does the next:
<?php
$result = mysqli_query($con, "SELECT na_id from NALOG;");
$row_cnt = mysqli_num_rows($result);
printf("U bazi su trenutno %d naloga.\n", $row_cnt);
// takes the number of curent DB state
printf("<script type='text/javascript'>
$.cookie('".$_SESSION['username_login']."');
var logstari = $.cookie('".$_SESSION['username_login']."');
// reads old coocke vale and sets is as varibale
$.cookie('".$_SESSION['username_login']."', '%d');
var log = $.cookie('".$_SESSION['username_login']."');
//sets new state count as new cookie and varible
var razlika = log - logstari;
// makes caluclation of diference
toastr.info(razlika + ' novih naloga od tvog zadnjeg posjeta'):
//lets the notification out
</script>", $row_cnt);
?>
I also tried this way:
<?php
$result = mysqli_query($con, "SELECT na_id from NALOG;");
$row_cnt = mysqli_num_rows($result);
printf("U bazi su trenutno %d naloga.\n", $row_cnt);
printf("<script type='text/javascript'>
$.cookie('".$_SESSION['username_login']."');
var logstari = $.cookie('".$_SESSION['username_login']."');
var log = ".'%d', $row_cnt.";
var razlika = log - logstari;
toastr.info(razlika + ' novih naloga od tvog zadnjeg posjeta');
$.cookie('".$_SESSION['username_login']."', '%d');
</script>", $row_cnt);
?>
Where i tried to set var log = ".'%d', $row_cnt.";
new log number strait out of php but how ever I try to escape PHP value and insert it into JS variable it doesn't work and im getting error printf(): Too few arguments
. If somehow I can make that work I was thinking in last line setting an new cookie with new count ONLY after session expires or window closes if even possible.
Not sure how to approach this problem anymore so any help or guidance is appropriated.
EDIT:
My last attainment, still looses the value after refresh.
<?php
$result = mysqli_query($con, "SELECT na_id from NALOG;");
$row_cnt = mysqli_num_rows($result);
printf("U bazi su trenutno %d naloga.\n", $row_cnt);
printf("<script type='text/javascript'>
var logstari = $.cookie('".$_SESSION['username_login']."');
var log = '%d';
var razlika = log - logstari;
toastr.info(razlika + ' novih naloga od tvog zadnjeg posjeta');
</script>", $row_cnt);
printf("<script type='text/javascript'>
$.cookie('".$_SESSION['username_login']."', '%d');
</script>", $row_cnt);
?>