php - refresh a SQL query to display changes -
i have following query:
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 ? " , `m`.`status`='a'" : ''; $query = "select count(distinct `c`.`conversation_id`) `".tbl_mailbox_conversation."` `c` left join `".tbl_mailbox_message."` `m` on (`c`.`conversation_id` = `m`.`conversation_id`) (`initiator_id`=$profile_id or `interlocutor_id`=$profile_id) , (`bm_deleted` in(0,".self::interlocutor_flag.") , `initiator_id`=$profile_id or `bm_deleted` in(0,".self::initiator_flag.") , `interlocutor_id`=$profile_id) , (`bm_read` in(0,".self::interlocutor_flag.") , `initiator_id`=$profile_id or `bm_read` in(0,".self::initiator_flag.") , `interlocutor_id`=$profile_id) $filter_cond , `m`.`recipient_id`=$profile_id "; self::$newmessagecountcache[$profile_id] = sk_mysql::query($query)->fetch_cell(); return self::$newmessagecountcache[$profile_id];
this return number new mailbox messages, have found ajax code checking if there change.
code:
var previousvalue = null; function checkforchange() { $.ajax({ url: '', ... success: function(data) { if (data != previousvalue) { // have changed! //call function update div previousvalue = data; } } }); } setinterval("checkforchange();", 1000);
but need figure out how update query without refreshing entire page? figured maybe ajax can totally new ajax , don't have no idea start.
update: ok wrote php script queries not sure how ajax script use "emails" var.
here script.
<?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 " . table_prefix . "profile_online 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 filter condition $filter_cond = sk_config::section('mailbox')->section('spam_filter')->mailbox_message_filter ? " , `m`.`status`='a'" : ''; $query = "select count(distinct `c`.`conversation_id`) `".tbl_mailbox_conversation."` `c` left join `".tbl_mailbox_message."` `m` on (`c`.`conversation_id` = `m`.`conversation_id`) (`initiator_id`=$profile_id or `interlocutor_id`=$profile_id) , (`bm_deleted` in(0,".self::interlocutor_flag.") , `initiator_id`=$profile_id or `bm_deleted` in(0,".self::initiator_flag.") , `interlocutor_id`=$profile_id) , (`bm_read` in(0,".self::interlocutor_flag.") , `initiator_id`=$profile_id or `bm_read` in(0,".self::initiator_flag.") , `interlocutor_id`=$profile_id) $filter_cond , `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(); ?>
ajax correct - ajax can send request webserver execute query , receive request. visiting page browser except happens in background , not reloading browsertab/page.
lets file query.php , can access via my-domain.tld/query.php
query.php:
//first make file executeable ajax-requests no regular visit via browser: if($_server['http_x_requested_with'] != "xmlhttprequest") { die(); //user tried enter query.php browser or similar... } //call function query stored or put query here , save result //its important echo want returned in ajax-request. echo self::$newmessagecountcache[$profile_id]; die(); //best die after last echo make sure there no outputs!
now in template or atleast html code is:
function checkforchange() { $.ajax({ url: 'my-domain.tld/query.php', //see, here url file on server data: {}, //you need if want mpass variables script. on php server-side data available via $_post array! success: function(data) { //data contains echo'ed content query.php $(".mailbox_messages").html(data); //the best make container updated class or id access. content can overridden .html() function. echo contents in query.php want displayed in element , override content .html(data); } }); }
now need call checkforchange() when special happens button click example:
<input type="button" id="refresh-mailbox" value="refresh mailbox" /> <script type="text/javascript"> $("#refresh-mailbox").on("click", function() { checkforchange(); //execute function on button click , done! }); </script>
i hope helps. :)
Comments
Post a Comment