0

In my JavaScript Long-polling script, I call msgsrv.php to echo the num_rows not on the screen yet. Here's the JavaScript which is functioning:

<script type="text/javascript" charset="utf-8">
function addmsg(type, msg){
    /* Simple helper to add a div.
    type is the name of a CSS class (old/new/error).
    msg is the contents of the div */
    $("#notiWrap.notiZero").html(
        "<div id='notiZero "+ type +"'>"+ msg +"</div>"
    );
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
    $.ajax({
        type: "GET",
        url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

        async: true, /* If set to non-async, browser shows page as "Loading.."*/
        cache: false,
        timeout:50000, /* Timeout in ms */

        success: function(data){ /* called when request to barge.php completes */
            addmsg("notiMsg", data); /* Add response to a .msg div (with the "new" class)*/
            setTimeout(
                waitForMsg, /* Request next message */
                1000 /* ..after 1 seconds */
                
            );
        },
    });
};
$(document).ready(function(){
    waitForMsg(); /* Start the inital request */
    
});
</script>

However, msgsrv.php knows the actual latest row in the database, but it doesn't know the id of the latest row that was loaded on the page.

$num_rows works; $loaded_rows fails. $loaded_rows fails, because when the JS calls msgsrv.php, it resets to the database's actual num_rows.

I need the PHP to read what is the $loaded_rows on the page instead of in the database, because I already know how to query the latest row.

Here is my msgsrv.php:

<?php
/* Send a string after a random number of seconds (2-10) */
sleep(2);

include('/home/alimix/public_html/include/conn.php');

/* Variables */
$nr = mysql_query("SELECT * FROM txtr");
while (mysql_fetch_array($nr))
{$nowrecent =  mysql_num_rows($nr);}

$mr = mysql_query("SELECT * FROM txtr ");
for ($i=0; $i < 1; $i++)
{$mostrecent = "$nowrecent";}
/* $mostrecent shouldn't equal $nowrecent if new rows aren't loaded*/
    $minus = $nowrecent - $mostrecent;

        if ($minus > 0) {
            //echo $nowrecent;
            echo $minus;
            //echo $mostrecent;
        }
        elseif($minus < 1) {
            echo "nr".$nowrecent."";
            echo " ";
            echo "mr".$mostrecent."";
        }
?>
Martin54
  • 1,349
  • 2
  • 13
  • 34
Tim Banon
  • 95
  • 1
  • 8

1 Answers1

0

Instead of feeding this:

    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",

Feed this:

    url: "http://mediahood.net/widgets/txtr/inc/msgsrv.php",
    data: {
      lastID: mylastID
    },

Every time you get a row, set mylastID to your new latest ID (and make sure that mylastID is in the scope of the AJAX call). That's it, you can then recover the value using $_GET['lastID']!

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
  • iono how long its gonna take me to figure this out even tho u might be giving me the right answer. thx anyway. – Tim Banon May 13 '13 at 05:09