Jump to content

PHP Query Data - Auto Refresh


Recommended Posts

Hi.

I am trying to get a PHP Query to refresh every 10 seconds. I have scoured the internet for days and could not find anything of much use. Plenty of Ajax going on (whatever that is) but the scripts were immense. I only want to refresh 7 lines of PHP Query Code.

 

Can you please tell me if this is possible?

 

Thanks in Advance.

Link to comment
Share on other sites

Post the code you want to be refreshed...the simplest way that comes to mind is using meta refresh or javascrit, so once the page is loaded it will refresh every 10 seconds.

 

That would be my advice, echo out a meta tag or javascript to do this.

Link to comment
Share on other sites

<?php
$result1 = mysql_query("SELECT * FROM ajaxim_users", $openDB);
$num_rows1 = mysql_num_rows($result1);
?>
<?php
$result2 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 1", $openDB);
$num_rows2 = mysql_num_rows($result2);
?>
<?php
$result3 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 0", $openDB);
$num_rows3 = mysql_num_rows($result3);
?>
<?php
$result4 = mysql_query("SELECT * FROM ajaxim_users WHERE banned = 1", $openDB);
$num_rows4 = mysql_num_rows($result4);
?>
<?php
$result5 = mysql_query("SELECT * FROM ajaxim_chats", $openDB);
$num_rows5 = mysql_num_rows($result5);
?>
<strong>Members:</strong><br /><?echo"$num_rows1";?><hr />
<strong>Online Admins:</strong><br /><?echo"$num_rows2";?><hr />
<strong>Online Users:</strong><br /><?echo"$num_rows3";?><hr />
<strong>Banned Users:</strong><br /><?echo"$num_rows4";?><hr />
<strong>Chatrooms:</strong><br /><?echo"$num_rows5";?><hr />

Link to comment
Share on other sites

You are going to need to use AJAX.  Its basically JavaScript with some XML and PHP thrown in.  Its pretty simple when you get the hang of it, but untill then I would do some practive first.  Try W3Schools.  Do you need to send post data or GET data?  If not, I can write one really easy for you.

Link to comment
Share on other sites

I don't think so, no. this is not a form. It simply runs a query on the database tables and counts the rows. The displays its results. I am not trying to pass it from page to page or anything.

 

You could write one for me? That would be great! I have heard of AJAX, but never in my life used it. The software I am designing this MOD for is Ajax, but that's all I know.

 

I would be very grateful if you were to help me with this code. I am completely at sea if it is not PHP/CSS.

Link to comment
Share on other sites

Ya, JavaScript, when you look into it, is VERY similar to PHP.  But here is your example:

<?php
$result1 = mysql_query("SELECT * FROM ajaxim_users", $openDB);
$num_rows1 = mysql_num_rows($result1);

$result2 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 1", $openDB);
$num_rows2 = mysql_num_rows($result2);

$result3 = mysql_query("SELECT * FROM ajaxim_users WHERE is_online = 1 AND admin = 0", $openDB);
$num_rows3 = mysql_num_rows($result3);

$result4 = mysql_query("SELECT * FROM ajaxim_users WHERE banned = 1", $openDB);
$num_rows4 = mysql_num_rows($result4);

$result5 = mysql_query("SELECT * FROM ajaxim_chats", $openDB);
$num_rows5 = mysql_num_rows($result5);
if ($_GET[ajax]){
echo $num_rows1."~".
$num_rows2."~".
$num_rows3."~".
$num_rows4."~".
$num_rows5;
}else{
?>
<script language="Javascript">
//The time is the 10000.  It is in miliseconds
setTimeout("glbup()",10000);
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            ajax_glbup(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
function ajax_glbup(str){
str=str.Split("~");
document.getElementById("mb").innerHTML=str[0];
document.getElementById("oa").innerHTML=str[1];
document.getElementById("ou").innerHTML=str[2];
document.getElementById("bu").innerHTML=str[3];
document.getElementById("cr").innerHTML=str[4];
}
function glbup(){
//document.getElementById("mb").innerHTML
//This is where everything is executed.  Insert URL here:
xmlhttpPost("{INSERT URL HERE OF THIS SCRIPT}?ajax=1");
setTimeout("glbup()",10000);
}
</script>
<strong>Members:</strong><br /><div id="mb"><?echo"$num_rows1";?></div><hr />
<strong>Online Admins:</strong><br /><div id="oa"><?echo"$num_rows2";?></div><hr />
<strong>Online Users:</strong><br /><div id="ou"><?echo"$num_rows3";?></div><hr />
<strong>Banned Users:</strong><br /><div id="bu"><?echo"$num_rows4";?></div><hr />
<strong>Chatrooms:</strong><br /><div id="cr"><?echo"$num_rows5";?></div><hr />

<form name="f1">
  <p>word: <input name="word" onchange="jsupdate('word','{INSERT URL HERE}')" type="text">  
  <div id="result"></div>
</form>
</body>
</html>
<? } ?>

 

There is a rough draft.  I have to leave, but try that, and play with it.  It should work, if not I'm sure somebody else on the forum can help if im not back by then.

Link to comment
Share on other sites

Hey thank man. I will have a mess with this. I really should learn AJAX, I notice it coming up a lot more on the net nowadays.

Thanks for your help. I really appreciate it ;)

 

His code works, but seriously. If you are doing AJAX, look into jQuery it takes alot of the guessing work out of it and is secure/consistent.

 

And it makes it really easy to use.

Link to comment
Share on other sites

  • 3 years later...

hi everyone !! I'm new with forum and with php...

 

if u think on PHP code structure , to reload/refresh $variables in a page ,

u simple have to get the $variable from mysql before u write that in the HTML page and after the isset ,

but put the mysql_connect on top of the page.

 

now :

 

session start

connect to mysql

make an isset instruction

query mysql and get a variable $var

and than write the $var.

 

using this scheme theres no problem.

exemple :

 

befor this page i have the login.php page with the login form

in that page i create the $_SESSION[] array

 

 

<?PHP

session_start();

if( $_SESSION['user'] == NULL){ // I create user in login.php page befor redirect 

Header("Location: login.page url "); // if no session from login page redirect to login page

}else{

	mysql_connect('sql.site.com', 'user', 'password') or die(mysql_error()); // connection to database
	mysql_select_db('database');

	}

} // end if SESSION

 //isset case for a POST form

        if(isset($_POST['submitnew'])){           

			$new = addslashes($_POST['newtext']);    // put value of var from POST  in a variable $new
   					
			mysql_query("UPDATE `table` SET `textsql`= '$new'  WHERE id='1' "); // update new values in the database


       }// end if isset


                                $query = mysql_query("SELECT * FROM `table` WHERE id='1' "); // obtain a value from database (in this case must be after isset)
	                $fetch = mysql_fetch_array($query);

	                $text = $fetch['textsql']; // now the value from database is in a variable called $text


?>


<body>


		        <form id='change text' method='post'>
                    
                                <-- now we can write the new value and not the obsolete one without any cache problem -->
                                <-- in this case I write the $text variable in the textarea with PHP echo -->

                                <textarea rows="20" cols="20" name="newtext"> <?PHP echo $text; ?> </textarea>  
                    
                                <input type='submit' name='submitnew' value='Update'/>
                     
                                </form>

</body>

 

this work for me !! for text and images !!

Many thanks

--

Marco

 

EDIT: please use code tags & no need to reply to this old of a thread.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.