最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Real time page update - Stack Overflow

programmeradmin1浏览0评论

I need help, This is what am actually trying to do. I have two files. "index.php" and "realtime.php" the index.php displays news from a particular country base on the url parameter "country_code", and the realtime.php updates the news list every 2 seconds. what I want is for realtime.php to get the current url parameter of index.php so that it can only update news from that particular country base on the url parameter. I really need this help. Thank you again. script for index.php

<script type="text/javascript">
$(document).ready(function () {
    $.arte({'ajax_url': '../realtime.php?lastid='+$('.postitem:first').attr('id'), 'on_success': update_field, 'time': 1000}).start();
    function update_field(data)
    {
        $("#realtimeupdate").html(data);
    }
});

</script>

and script for realtime.php

<?php

include"customDB.php";
$lastid = $_REQUEST['lastid'];
$query = 'SELECT count(*) as newpost FROM wp_posts WHERE country_code = "XXXXX" AND   post_id > "'.$lastid.'"';
$result = mysql_query($query);
$rec = mysql_fetch_object($result);
if($rec->newpost){ ?>
<a href="" id="newpostlink">(<?php echo $rec->newpost; ?>) new posts</a>
<script type="text/javascript">document.title = '(<?php echo $rec->newpost; ?>) new posts'</script>
<?php } ?>

I want the value of country_code ="XXXXXX" in raltime.php to be the url parameter value of index.php Thanks

I need help, This is what am actually trying to do. I have two files. "index.php" and "realtime.php" the index.php displays news from a particular country base on the url parameter "country_code", and the realtime.php updates the news list every 2 seconds. what I want is for realtime.php to get the current url parameter of index.php so that it can only update news from that particular country base on the url parameter. I really need this help. Thank you again. script for index.php

<script type="text/javascript">
$(document).ready(function () {
    $.arte({'ajax_url': '../realtime.php?lastid='+$('.postitem:first').attr('id'), 'on_success': update_field, 'time': 1000}).start();
    function update_field(data)
    {
        $("#realtimeupdate").html(data);
    }
});

</script>

and script for realtime.php

<?php

include"customDB.php";
$lastid = $_REQUEST['lastid'];
$query = 'SELECT count(*) as newpost FROM wp_posts WHERE country_code = "XXXXX" AND   post_id > "'.$lastid.'"';
$result = mysql_query($query);
$rec = mysql_fetch_object($result);
if($rec->newpost){ ?>
<a href="" id="newpostlink">(<?php echo $rec->newpost; ?>) new posts</a>
<script type="text/javascript">document.title = '(<?php echo $rec->newpost; ?>) new posts'</script>
<?php } ?>

I want the value of country_code ="XXXXXX" in raltime.php to be the url parameter value of index.php Thanks

Share Improve this question asked Dec 20, 2011 at 7:26 David AddoteyeDavid Addoteye 1,6411 gold badge20 silver badges32 bronze badges 3
  • Why don't you remove realtime.php from the app and instead push the updates to the connected web clients when more news bee available? There are a whole host of realtime web technologies that make this easy to do as well as removing the potentially very heavy resource load from your servers. – leggetter Commented Dec 20, 2011 at 16:04
  • please can you explain a bit better of what you are saying – David Addoteye Commented Dec 22, 2011 at 13:41
  • It's possible to push updates from a web server to a web client instead of using inefficient polling techniques. If you poll realtime.php every 2 seconds and have a reasonable number of connected clients you will be putting your web server under a lot of load. I suggest reading up on Push Technologies and WebSockets. You can see a really simple demo of push notifications using PHP here. – leggetter Commented Dec 22, 2011 at 19:27
Add a ment  | 

2 Answers 2

Reset to default 4

Accessing query string in Javascript

First, you'll need to get the value of "country_code" that has been passed as a query string parameter. You can use Javascript like this to do so:

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

(taken from this SO question)

Now, urlParams will look something like:

urlParams = {
    country_code: 'US'
}

So, you can change your AJAX call to add this parameter:

$.arte({'ajax_url': '../realtime.php?country_code='+urlParams.country_code+'lastid='+$('.postitem:first').attr('id'), 'on_success': update_field, 'time': 1000}).start();

And now it will be available via PHP under $_GET['country_code'].

Using the variable in PHP

Now you can access the variable and use it in your query.

$country_code = mysql_real_escape_string($_GET['country_code']);
$query = 'SELECT count(*) as newpost FROM wp_posts WHERE country_code = "' . $country_code . '" AND   post_id > "'.$lastid.'"';

Note that I've written this code to conform to your existing coding style. It's not very clean, so you might consider cleaning it up by using helpers like $.param() and MySQLi prepared statements.

At page generation you can echo country code from $_GET['country_code'] to the script that calls realtime.php.

Or, alternatively, on index.php you can get your country parameter from window.location property and then pass it as a parameter to realtime.php.

发布评论

评论列表(0)

  1. 暂无评论