updating a line of text on a php web server
I am coding a php web server and have come across the need to update a line of text. I was wondering if there was anyway to for the output of your server to be printed only once and have a number in each line change every second, 5 seconds, 10 seconds, etc. I do not want my server to print my output every time a new message is sent, I just want the numbers to change as if it was a "ticker". Here is my code:
#!/usr/bin/php5 -q <?php function giveMeAnArray($delimiter, $string) { return explode($delimiter, $string); } $read_write = "read"; if ($read_write == "read") { //Initialize the socket set_time_limit(0); $address = '127.0.0.1'; $port = 10119; $counter = 0; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed. Reason: " . socket_strerror($sock) . "<BR>"; } if (($ret = socket_bind($sock, $address, $port)) < 0) { echo "socket_bind() failed. Reason: " . socket_strerror($ret) . "<BR>"; } //Start listening on the socket if (($ret = socket_listen($sock, 5)) < 0) { echo "socket_listen() failed. Reason: " . socket_strerror($ret) . "<BR>"; } //do { //Accept an incoming connection if(($client = socket_accept($sock)) < 0) { echo "socket_accept() failed. Reason: " . socket_strerror($ret) . "<BR>"; break; } do { //Read whatever was just sent, 1024 bytes' worth. Make this however long you need. if( false == ($global_string = socket_read($client, 2048))) { echo "socket_read() failed. Reason: " . socket_strerror($ret) . "<BR>"; break ; }; if (!$global_string = trim($global_string)) { continue; } else { print ("Serving Client!<BR>"); } //Strip whitespace $global_string = ereg_replace("[ \t\n\r]", "", $global_string).chr(0); $delim = ','; $array = giveMeAnArray($delim, $global_string); //print("Coil ID number: $array[0]\n"); //print("Coil Width: $array[1]\n"); //print("Footage Count: $array[2]\n"); //print("Coil Length: $array[3]\n"); // for($i=0; $i < count($array); $i++) { // sleep(1); // echo "\r"; // echo "update: $i%"; // } $id = $array[0]; $Width = $array[1]; $footagecount = $array[2]; $Length = $array[3]; print("Coil ID number: $id<BR>"); print("Coil Width: $Width<BR>"); print("Footage Count: $footagecount<BR>"); print("Coil Length: $Length<BR>"); $counter++; } while (true); //Close the connection; Don't leave things like this open...!! // socket_close($client); // } while (true); //print("Coil ID number: $id<BR>"); // print("Coil Width: $Width<BR>"); //print("Footage Count: $footagecount<BR>"); //print("Coil Length: $Length<BR>"); //Close the socket itself // socket_close($sock); header( "Location: http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] ); } //ends if ("read") |
Quote:
Then javascript springs to mind. I think the function you want is called `setTimeout'--and use 1000 (ms) and a function as the argument; the function would then go and mess with the DOM (or use foo.innerHTML (where foo is a <span>)). hth --Jonas |
Thank you for your reply. I am new to coding in linux languages and am not familiar with the suggestiuon that you made. Would it be possible for you to explain in a bit more detail and perhaps give me some examples that would pertain to my code or even use my code?
|
Quote:
I assume that you're generating HTML output for viewing in a pretty regular browser, and that you want the page to be dynamic on the client-side. There's a scripting language very useful for that, called JavaScript--w3 has a tutorial (http://www.w3schools.com/js/default.asp). The language is cross-platform, and most newer browsers that supports it supports the newest standards. Some people (me) may choose to turn off javascript, though (it's most often used for popping up ads or creating lame effects). I'm suggesting to use that for updating the page contents. The obvious way is to use `setTimeout', which calls a function of your choice after n milisecs. Makes a bit more sense? |
Am I going to have to convert all of my code to javascript or will I only have to implement the settimeout function? I am still a little unsure exactly what you mean. Would it be possible for you to take a snippet of my code and illustrate what you mean or how I would implemnt this setTimeout function?
|
You won't have to implement the setTimeout function--it's a part of the JavaScript std. lib.
What you have to do is put each part you want to update in <span>-tags, with appropriate IDs. The javascript code will be something like this: Code:
function update() { In that case, it's probably easier just to use a META refresh tag. So, in order to help you the best way: exactly what is it you want the page to do? Can you point to a page that does something like it? |
I am sorry for not being so clear as to what I want it to do. You have a copy of my code from the previous post. Right now.....my code prints out as follows:
#!/usr/bin/php5 -q Serving Client! Global string before being parsed: 1323667,35.700,101,18 Global string after being parsed: Coil ID number: 1323667 Coil Width: 35.700 Footage Count: 101 Coil Length: 18 Serving Client! Global string before being parsed: 1323667,35.700,29,23 Global string after being parsed: Coil ID number: 1323667 Coil Width: 35.700 Footage Count: 29 Coil Length: 23 socket_read() failed. Reason: Incorrect function I am going to eliminate lines 3 and 4 eventually...they are just there for debugging purposes. Now i am not able to view this output until I manually shut the client down. I want the the page to display........output be printed(only once however)......then just have the numbers changing as each new string of numbers is being passed in. Basically I want it to have the effect of a "ticker" where the number labels are printed only once and the numbers are dynamically changing. I hope to be able to watch as the numbers change instead of having the page continually refresh. Take a look at this website: http://www.toptips.com/debtclock.html I dont need them to change this fast but in the area of once every second or two. |
So what you want is for your page (on the client) to get data from the server at regular intervals, and update only the data on the page using the fresh data from the server without reloading the whole page. True?
If so, then you need to establish a communication between client and server. The server can run PHP without any problem, and on the client, you'll indeed have to rely on Javascript. Here's a page to help you get started with Javascript: http://www.onlinetools.org/articles/...ivejavascript/ And here is a page dealing with the specific technology you'll have to use: http://www.w3schools.com/dom/dom_http.asp Yves. |
So, just to get everything perfectly straight:
You want the HTML output to change whenever the PHP script recieves more input over the network? btw, when posting code, please used the [ code ] tags; when posting PHP, use [ PHP ] instead. |
I just want the the number of the html output to change each time the server recieves a new message:
Coil ID number: 1323667 Coil Width: 35.700 Footage Count: 29 Coil Length: 23 I want the output to look just like this with only the numbers changing. Just like the on the website that I sent you which has: Debt: (a dynamically changing number) |
All times are GMT -5. The time now is 06:11 PM. |