0

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);

                        ?>
ikiK
  • 6,328
  • 4
  • 20
  • 40
  • Just add the number into session like `$_SESSION['number'] = x` if session is not defined, when user refresh page test if session is already defined, if right, don't update it, when user will close it's browser, the session will clean and number will added on the next session – Adrien Leloir Apr 30 '20 at 14:16

1 Answers1

0

Maybe it will be ok

<?php
session_start();

if(!isset($_SESSION['news-number'])){
    $news_number = 0;

    // Do something
    echo "news number updated";
    $news_number = 45;
    // Do something

    $_SESSION['news-number'] = $news_number;
}

echo $_SESSION['news-number'];

?>

You can make the same thing using cookies, if user session is defined (or something like this), don't update the cookie, else define it with its new value

EDIT :

<?php
session_start();

if(!isset($_SESSION['news-number'])){
    $result = mysqli_query($con, "SELECT na_id from NALOG;");
    $news_number = mysqli_num_rows($result);

    $_SESSION['news-number'] = $news_number;
}

echo "<script> var value=" . $_SESSION['news-number'] . "</script>";

?>

This is just an example of logic, don't implement this, then echo var in script it's not the best way Pass variable from php to js script

Adrien Leloir
  • 513
  • 4
  • 8
  • I don't understand this, been trying for hours or I don't know how to implement it in code I provided... I tried numerous things with sessions. There is a short code i provided, can you make an example out of that? – ikiK Apr 30 '20 at 16:25
  • I appreciate your help and time, but this wont work ether for me no matter what i do with this method. It keeps resitting after refresh. – ikiK Apr 30 '20 at 17:59