They may be a problem in PHP if you are not using the sum by Javascript, but if you are in front-end then I will explain what may be occurring.
Supposing the code actually is this (since what was posted makes no sense):
<?php
echo 'document.title = "(' . $total_notifi . ') " + title;';
So the updates now occur on front-end by increment, the way the code here can update the title by accessing the title already coming from php (back-end)
Something like:
document.title += "(1)";
If the problem is really in javascript, then you should convert to int
using parseInt
for example and also do the necessary treatments, something like:
javascript:
//Valor que virá do ajax ou js que você fez e será somado
function atualiza(soma) {
var re = /^\(\d+\)/g;
var res = document.title.match(re);
//Pega o total no titulo
var atual = res && res[0] ? parseInt(res[0].replace(/\(|\)/g, "")) : 0;
//Remove o total e os `()` do titulo
var titulo = document.title.replace(re, "");
document.title = "(" + (atual + parseInt(soma)) + ") " + titulo;
}
setInterval(atualiza, 1000, 1); //Adiciona +1 (terceiro parâmetro)
html:
<title>(<?php echo $total; ?>) Home</title>
However as I have already mentioned, the problem may be in php.
Note that the (<?php echo $total; ?>)
is optional and if the update comes from ajax, as it is on Facebook or Twitter, ie the script here can fit any title without having to set in a variable, examples of pages that the (1)
will be added:
home:
<title>Home</title>
Blog:
<title>Blog</title>
And Ajax (Jquery example) would be something like:
$.ajax("update.php").done(function(data) {
atualiza(data);
});
After running it on any page the result will be something like:
home:
(2) Home
Blog:
(2) Blog
That is how I made the title is updated independent of the original content.
I think you should run the code change only if the
$total_notifi
for>
that the current$total_notifi
. In the process of checking how many notifications it has, return the value only if it is higher. If it is equal, do not run anything.– celsomtrindade
Good morning! There is no way to know how your code works and where is the fault, because you only posted a piece, the update is ajax? Your code here
'.$total_notifi.
the way it’s mixed with javascript and php doesn’t make sense, it can’t be played, read this: http://answall.com/help/mcve. and only after reading edit the question.– Guilherme Nascimento
Every time you seek the
title
you are not removing the amount of notifications. previous.– Pedro Camara Junior
@Guilhermenascimento in fact it only brings the value, in case, 1, 2 or 3, logic is a SELECT I do in the bank and brings me this value!
– Leonardo
@Leonardocarmo I understand, but the problem is not this, the problem is how you put the CODE here in the question, it is not possible to understand if the problem is PHP or JS, if the solution can be php, whether it updates with ajax or not, things like that. You’re saying that it brings from a select, ok is php + mysqli api, but it doesn’t say how the sum is induced. That is, the code has no sense that could be answered, so I provided an answer that tries to make a PARSE of the existing title and by JS itself it updates: http://answall.com/a/108449/3635 - setInterval is only to simulate ;)
– Guilherme Nascimento