3

I need advice about the title above. I have the following piece of code :

PHP :

$sql = "SELECT * FROM i_case";
$qry = pg_query($connection, $sql);
$res = pg_num_rows($qry);   

$sql_1 = "SELECT * FROM i_case";
$qry_1 = pg_query($connection, $sql_1);
$res_1 = pg_num_rows($qry_1);

do {
    echo "new record on i_case";
} while($res !=  $res_1 and !(usleep(10000)));

JQUERY :

setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "file.php",
        success : function(response){
            alert(response);
        }
    });
},1000);

It's not work that I expecting.

I don't know where is the matter from the code?

4
  • what exception? i dont understand? Commented Jan 27, 2012 at 7:45
  • response is -> new record on i_case Commented Jan 27, 2012 at 7:47
  • but there is no new record on table... no error! i put error: function(){ alert("ERROR"); } Commented Jan 27, 2012 at 7:48
  • if xdazz answered worked for you, then accept his answer.. Commented Jan 27, 2012 at 8:53

4 Answers 4

6

Your loop even won't run once because $res is same with $res_1 in almost all cases. If the loop runs, it's a horrible infinite loop.

What you should do is like below.

The javascript:

var old_count = 0;

setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "file.php",
        success : function(data){
            if (data > old_count) {
                alert('new record on i_case');
                old_count = data;
            }
        }
    });
},1000);

And the PHP code:

$sql = "SELECT count(*) as count FROM i_case";
$qry = pg_query($connection, $sql);
$row = pg_fetch_assoc($qry);
echo $row['count'];
Sign up to request clarification or add additional context in comments.

2 Comments

but, when i refresh the page, last notification is shown again and again, any advice?
@jin you can load the initial record count first not just set to 0.
5

A little tweak to xdazz answer. To avoid alert for first time when you load the page.

JS Code:

var old_count = 0;
var i = 0;
setInterval(function(){    
$.ajax({
    type : "POST",
    url : "notify.php",
    success : function(data){
        if (data > old_count) { if (i == 0){old_count = data;} 
            else{
            alert('New Enquiry!');
            old_count = data;}
        } i=1;
    }
});
},1000);

PHP Code:

include("config.php");   
$sql = "SELECT count(*) as count FROM Enquiry";
$qry = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($qry);
echo $row['count']; 

P.S. My first Stackoverflow answer.

1 Comment

Can you explain the the code { if (i == 0){old_count = data;} else{ alert('New Enquiry!'); old_count = data;} } i=1;
2

I think your whole approach will not work. I suggest, PHP to only generate the count and JS to check if it has changed.

$sql = "SELECT * FROM i_case";
$qry = pg_query($connection, $sql);
$res = pg_num_rows($qry);   
echo $res;

and in JS, you could do somesthing like this:

var count_cases = -1;
setInterval(function(){    
    $.ajax({
        type : "POST",
        url : "file.php",
        success : function(response){
            if (count_cases != -1 && count_cases != response) alert('new record on i_case');
            count_cases = response;
        }
    });
},1000);

4 Comments

so it call count_cases from -1, what if cases count already 50 record?
-1 is just an init value, to prevent the alert in the first run of the function. After the 1st run, count_cases is initiated with -let's say- 50 and the function will trigger the alert, as soon as the PHP returns a number != 50 (likely > 50?)
btw: A full SELECT to just count the rows is very inefficient. Have a look on the COUNT statement in xdazz's answer
okay, i've tried to add new record, but notification is not appearing... why?
1

Have you checked the you're connected to db?

i mean $connection variable returns true?, because i didn't found any code in php that connects to database.

If you could tell the what response you're getting from the server end, then i can help you bit more.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.