2014-12-02 41 views
0

我有以下查询:刷新SQL查询,显示改变

if (!isset($profile_id) || !is_numeric($profile_id)) 
     return false; 

    if (isset(self::$newMessageCountCache[$profile_id])) 
    { 
     return self::$newMessageCountCache[$profile_id];  

    $filter_cond = SK_Config::section('mailbox')->section('spam_filter')->mailbox_message_filter ? " AND `m`.`status`='a'" : ''; 

    $query = "SELECT COUNT(DISTINCT `c`.`conversation_id`) 
     FROM `".TBL_MAILBOX_CONVERSATION."` AS `c` 
     LEFT JOIN `".TBL_MAILBOX_MESSAGE."` AS `m` ON (`c`.`conversation_id` = `m`.`conversation_id`) 
     WHERE (`initiator_id`=$profile_id OR `interlocutor_id`=$profile_id) 
     AND (`bm_deleted` IN(0,".self::INTERLOCUTOR_FLAG.") AND `initiator_id`=$profile_id OR `bm_deleted` IN(0,".self::INITIATOR_FLAG.") AND `interlocutor_id`=$profile_id) 
     AND (`bm_read` IN(0,".self::INTERLOCUTOR_FLAG.") AND `initiator_id`=$profile_id OR `bm_read` IN(0,".self::INITIATOR_FLAG.") AND `interlocutor_id`=$profile_id) 
     $filter_cond AND `m`.`recipient_id`=$profile_id 
    "; 

    self::$newMessageCountCache[$profile_id] = SK_MySQL::query($query)->fetch_cell(); 
    return self::$newMessageCountCache[$profile_id]; 

这将返回的任何新邮箱的邮件了一些,我发现了一个Ajax代码检查是否有变化。

代码:

var previousValue = null; 

function checkForChange() { 
    $.ajax({ 
     url: '', 
     ... 
     success: function(data) { 
      if (data != previousValue) { // Something have changed! 
       //Call function to update div 
       previousValue = data; 
      } 
     } 
    }); 
} 

setInterval("checkForChange();", 1000); 

但我真的需要弄清楚如何在不刷新整个页面更新查询?我想可能是ajax可以提供帮助,但对于ajax我是全新的,我不知道从哪里开始。


更新:好了,所以我写了一个PHP脚本的查询,但不知道如何让AJAX脚本中使用我的“电子邮件”变种。

这里是脚本。

<?php 
if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") { 
     die(); 
} 

include("..\internals\config.php"); 

$host  = DB_HOST; 
$user  = DB_USER; 
$password = DB_PASS; 
$dbname  = DB_NAME; 
$prefix  = DB_TBL_PREFIX; 


$cxn = mysql_pconnect ($host, $user, $password); 

mysql_select_db($dbname, $cxn); 


function get_user_id() 
    { 
     $userid = NULL; 

     if (!empty($_COOKIE['PHPSESSID'])) 
     { 
      $result = $cxn->execute(" 
       SELECT profile_id 
       FROM " . TABLE_PREFIX . "profile_online 
       WHERE hash = '" . $cxn->escape_string($_COOKIE['PHPSESSID']) . "' 
      "); 

      if ($row = $cxn->fetch_array($result)) 
      { 
       $userid = $row[0]; 
      } 
     } 

     return $userid; 
    } 

$profile_id = get_user_id(); 

public static function newMessages($profile_id) 
    { 
     if (!isset($profile_id) || !is_numeric($profile_id)) 
      return false; 

     if (isset(self::$newMessageCountCache[$profile_id])) 
     { 
      return self::$newMessageCountCache[$profile_id]; 
     } 

     // check config for filter condition 
     $filter_cond = SK_Config::section('mailbox')->section('spam_filter')->mailbox_message_filter ? " AND `m`.`status`='a'" : ''; 

     $query = "SELECT COUNT(DISTINCT `c`.`conversation_id`) 
      FROM `".TBL_MAILBOX_CONVERSATION."` AS `c` 
      LEFT JOIN `".TBL_MAILBOX_MESSAGE."` AS `m` ON (`c`.`conversation_id` = `m`.`conversation_id`) 
      WHERE (`initiator_id`=$profile_id OR `interlocutor_id`=$profile_id) 
      AND (`bm_deleted` IN(0,".self::INTERLOCUTOR_FLAG.") AND `initiator_id`=$profile_id OR `bm_deleted` IN(0,".self::INITIATOR_FLAG.") AND `interlocutor_id`=$profile_id) 
      AND (`bm_read` IN(0,".self::INTERLOCUTOR_FLAG.") AND `initiator_id`=$profile_id OR `bm_read` IN(0,".self::INITIATOR_FLAG.") AND `interlocutor_id`=$profile_id) 
      $filter_cond AND `m`.`recipient_id`=$profile_id 
     "; 

     self::$newMessageCountCache[$profile_id] = SK_MySQL::query($query)->fetch_cell(); 
     return self::$newMessageCountCache[$profile_id]; 
    } 

mysql_close($cxn); 

$emails = newMessages(); 

?> 

回答

1

Ajax是正确的 - 使用Ajax,您可以向Web服务器发送请求,它将执行您的查询并接收请求。 就像使用浏览器访问一个页面,除了它发生在后台而不是重新加载browsertab /页面。

比方说这是你的文件query.php并且你可以通过我的-使用domain.tld访问/ query.php

query.php:

//First make this file only executeable for AJAX-Requests but no regular visit via browser: 
if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") { 
     die(); //User tried to enter query.php with a browser or similar... 
} 

//call function where query is stored or put your query here and save result 

//Its important to echo what you want to be returned in the ajax-request. 
echo self::$newMessageCountCache[$profile_id]; 
die(); //Best is die after last echo to make sure there are no extra outputs! 

现在在你的模板或至少其中你的HTML代码:

function checkForChange() { 
    $.ajax({ 

     url: 'my-domain.tld/query.php', //See, here is the URL to your file on server 

     data: {}, //You need this only if you want to mpass any variables to your script. On PHP Server-side this data are available via $_POST array! 

     success: function(data) { //data contains all echo'ed content from query.php 

      $(".mailbox_messages").html(data); //The best is make your container to be updated to a class or ID to access. Its content can be overridden with .html() function. Simply echo all contents in query.php that you want to be displayed in your element and override the content with .html(data); 
     } 
    }); 
} 

现在你只需要调用checkForChange()时,一些特别的东西偏偏喜欢点击一个按钮,例如:

<input type="button" id="refresh-mailbox" value="Refresh my Mailbox" /> 

<script type="text/javascript"> 
    $("#refresh-mailbox").on("click", function() { 
     checkForChange(); //Execute your function on button click and done! 
    }); 
</script> 

我希望这有助于。 :)

+0

也许你可以检查我的php代码,并告诉我,如果这将与ajax一起工作,并且它可以拉动变量。 – NewbieLootz 2014-12-06 10:51:45