Jump to content

Ciclo email


Go to solution Solved by M6,

Recommended Posts

Posted

bom dia

gostaria me dessem um dica de como fazer isto:

tenho uma admin onde os users tem acesso a varias coisas inclusive mensagens enviadas entre users

o que queria fazer era: criar um ciclo que em cada vez que chegasse uma nova msg e o user tivesse logado aparecesse uma notificação a informar que tinha uma nova msg por ler.

eu tenho assim

 

<SCRIPT>$(document).ready(function(){
var temp= setInterval(update,6000);
var Stacks = { 
stack_bar_top: { "dir1": "down", "dir2": "right", "push": "top", "spacing1": 0, "spacing2": 0 }, } 

function update (){ 
clearInterval(temp); 
var noteStack = "stack_bar_top"; 
var noteStyle= "warning"; var width = "290px"; 
	$.ajax({ 
		url:"entrada.php?page=60", 
		type:"post" }).done(function (resposta){ 
					var r="tem novas mensagens por ler"; 
					if (resposta !=0){ 
						new PNotify({ title: 'Mensagens', text: r, type: noteStyle, stack: Stacks[noteStack], width: "100%", delay: 3000 }); //var temp =setInterval(update,6000); } }) } })
</SCRIPT>

a page=60 é um php

<?php require_once '../connect.php';connect(); 
$comercial= mysql_query("SELECT * FROM comercial where email='".$_SESSION["user"]."' "); 
$comer= mysql_fetch_array($comercial); 
$msg= mysql_query ("SELECT * FROM msg_entrada where comercial_recetor=".$comer["id_comercial"]." and respondia =0"); $conta= mysql_num_rows($msg); echo $conta; ?>
  • Solution
Posted

Tipicamente isso não se faz com ciclos mas sim com chamadas assincronas AJAX.

Ou seja, tens uma chamada AJAX na página que, a cada X tempo, vai ao servidor verificar se existem mensagens novas.

O jQuery é muito útil para situações como esta. Eis um exemplo:

(function contagem() {
  $.ajax({
    url: 'contar_mensagens_novas.php', 
    success: function(data) {
      alert(data);
    },
    complete: function() {
      // corre daqui a 5 segundos
      setTimeout(worker, 5000);
    }
  });
})();

A "contagar_mensagens_novas.php" será equivalente ao teu "page=60".

  • Vote 1
10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted (edited)
52 minutos atrás, M6 disse:

Tipicamente isso não se faz com ciclos mas sim com chamadas assincronas AJAX.

Ou seja, tens uma chamada AJAX na página que, a cada X tempo, vai ao servidor verificar se existem mensagens novas.

O jQuery é muito útil para situações como esta. Eis um exemplo:


(function contagem() {
  $.ajax({
    url: 'contar_mensagens_novas.php', 
    success: function(data) {
      alert(data);
    },
    complete: function() {
      // corre daqui a 5 segundos
      setTimeout(worker, 5000);
    }
  });
})();

A "contagar_mensagens_novas.php" será equivalente ao teu "page=60".

worker não devia ser a função contagem?

Edited by nmoa
Posted

Sim, precisamente.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted

Isso é apenas uma simplificação do teu código.

Podes invocar o que quiseres quando necessitares.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.