searls03 Posted July 17, 2011 Share Posted July 17, 2011 is there a way with php or jquery to pull info from a database as soon as it is updated? make sense? Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/ Share on other sites More sharing options...
Psycho Posted July 17, 2011 Share Posted July 17, 2011 I think you need to provide more information about what you are wanting. On the face of it, what you are asking doesn't make any sense. If I INSERT something into the database why would I need to pull that information out since I have all the info that was used to create it. What I *think* you may be wanting to do is to have a user's page automatically updated with new information when records are inserted by another user or process. The answer to that is yes. And, it would be accomplished via AJAX, which you can do with jquery, other JavaScript libraries or roll your own. The trick is for the AJAX call to pass a timestamp of the last time data was fetched so you can pull just the information needed. EDIT: Here is a tutorial using jquery to build a shoutbox. It should point you int he right direction. http://yensdesign.com/2009/01/create-a-shoutbox-using-php-and-ajax-jquery/ Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243793 Share on other sites More sharing options...
searls03 Posted July 17, 2011 Author Share Posted July 17, 2011 yes so basically I want a div or something to refresh when a database is updated by someone else.......this is for a chat so as soon as the user logs out it will show them as offline. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243795 Share on other sites More sharing options...
teynon Posted July 17, 2011 Share Posted July 17, 2011 Chat's on a webserver are going to be refresh based. The only way around something like that is a program like java opening a constant connection to a server and constantly monitoring the database for updates. (Way more intensive on the server, and you'll need root access.) For a website to work, you'll need to refresh the page or make the page request the ajax page every now and then. I have made this system before. If you want the rest of the code PM me. However, here is the ajax refresh code: function checkMessages() { // AJAX updater. var urlvars='?uid='+escape('<?php echo $_SESSION['key'];?>')+"&time="+updateTime(); var xmlHttp2; try { // Firefox, Opera 8.0+, Safari xmlHttp2=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp2.onreadystatechange=function() { if(xmlHttp2.readyState==4) { var prefix = xmlHttp2.responseText.substr(0, 2); if (prefix=="re") { response=xmlHttp2.responseText.substr(2); document.getElementById('chatMessages').innerHTML=response; } } else if (xmlHttp2.readyState==2) { //document.getElementById('chatMessages').innerHTML=document.getElementById('chatMessages').innerHTML+'Text message is being sent now.'; } } xmlHttp2.open("GET","getMessages.php"+urlvars,true); xmlHttp2.send(null); scrollToBottom('liveChatBox'); } setInterval("checkMessages()", 5000); Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243799 Share on other sites More sharing options...
Psycho Posted July 17, 2011 Share Posted July 17, 2011 The only way around something like that is a program like java opening a constant connection to a server and constantly monitoring the database for updates. "Java" is NOT "JavaScript", they are two totally different technologies. "JavaScript" is all that is needed here. There are plenty of tutorials out there on building a chat box using AJAX (I even linked to one). and I'm pretty sure plenty of ready-made code. No need rebuilding the wheel when one already exists. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243880 Share on other sites More sharing options...
teynon Posted July 17, 2011 Share Posted July 17, 2011 I know the difference between Java and Javascript. I program in both. Like I already said, web chats are refresh based. AJAX still relies on basically refreshing the results page (requesting it again and again). Where as the java application I was talking about would be a socket / constant connection where actions could be initiated by either side. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243882 Share on other sites More sharing options...
ignace Posted July 17, 2011 Share Posted July 17, 2011 Or use OPA (http://www.opalang.org) to build your own Chat. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243893 Share on other sites More sharing options...
searls03 Posted July 17, 2011 Author Share Posted July 17, 2011 so basically what I want is like gmails chat status.........when they logoff different icon from logged on? i don't know whether this is database or what.......but does anyone know how o do this with a database or something else? Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243917 Share on other sites More sharing options...
Psycho Posted July 18, 2011 Share Posted July 18, 2011 Yes, this can be done. I already provided a link for you to a tutorial to build pretty much exactly what you are looking for. What more information, specifically, are you wanting? Like I already said, web chats are refresh based. AJAX still relies on basically refreshing the results page (requesting it again and again). Where as the java application I was talking about would be a socket / constant connection where actions could be initiated by either side. I can't see why anyone would seriously consider using Java to implement a chat box using Java. No one in their right mind would ever install a Java app for a website just to use such a feature. The only exception would be in a corporate environment where the company wants strict control of the application. And, I'm pretty sure that''s not the case here. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243975 Share on other sites More sharing options...
searls03 Posted July 18, 2011 Author Share Posted July 18, 2011 i didn't know exactly where it is in the link that I need to use............I good at making some php......not reading it and other languages. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1243976 Share on other sites More sharing options...
teynon Posted July 18, 2011 Share Posted July 18, 2011 @mjdamato: Actually, Yahoo uses chat boxes in Java because a lot of their games used Java. So do a lot of dating web sites. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244062 Share on other sites More sharing options...
searls03 Posted July 18, 2011 Author Share Posted July 18, 2011 could you two stop arguing and help me figure out which piece of code that mjdamato gave me is what I need? Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244140 Share on other sites More sharing options...
teynon Posted July 18, 2011 Share Posted July 18, 2011 searls03, he posted a tutorial. If you follow the tutorial you will have / understand what you need. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244143 Share on other sites More sharing options...
searls03 Posted July 18, 2011 Author Share Posted July 18, 2011 I did follow the tutorial, but there is php and ajax both in there and I don't want to use all of the code, only what is relevant to what I need............like I said, i am not good at reading code and seeing what works together........I have to make it to know what it does. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244145 Share on other sites More sharing options...
teynon Posted July 18, 2011 Share Posted July 18, 2011 I have to make it to know what it does. That's why you have to follow the tutorial... You have to finish the tutorial to understand how it all works together. Your chat question will use AJAX. So that tutorial will help and you DO need to look at the ajax code. As far as determining whether a user is offline or online, you could refresh the list of online peeps and update the div ID using the same concepts in that tutorial, in the code I posted (and sent to you in PM), and that everyone else has already said. If you want specific help with the code problem, I would post the code you are having specific problems with. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244148 Share on other sites More sharing options...
Psycho Posted July 18, 2011 Share Posted July 18, 2011 I did follow the tutorial, but there is php and ajax both in there and I don't want to use all of the code, only what is relevant to what I need... If you really did read that tutorial you would know that you need both the PHP code and the AJAX code. In reality, AJAX is not a language but a description of using multiple web technologies to provide a more interactive experience - in this case a shoutbox. With just raw HTML (even if it is created with PHP), the user would have to refresh their page to see if there were any new messages; either manually or with a button. What we want is a way to automatically display new messages without refreshing the page. AJAX requires both a client-side technology and a server-side technology. In the tutorial, the AJAX implementation used is a combination of client-side JavaScript (using the jquery framework) and server-side PHP. Basically, the javascript code will have a timed event that will make a request to the server and check if there are new messages. That request goes to the PHP script just as if the user had typed it into their browser. But, in this case, this all happens without any user interaction and is completely transparent to the user. The PHP script will check if there are new records and return the result back to the javascript code that made the request. The javascript code will then take the data returned and dynamically update the relevant data in the HTML page without any page refreshes. Quote Link to comment https://forums.phpfreaks.com/topic/242201-pull-immediately-from-database/#findComment-1244166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.