LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-13-2005, 07:59 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
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")
 
Old 06-15-2005, 06:55 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
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.
Just to get it completely straight: you want to generate html output dynamically (by php), and then you want the page to be `live' without using php for that?

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
 
Old 06-15-2005, 07:07 AM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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?
 
Old 06-15-2005, 07:23 AM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
Would it be possible for you to explain in a bit more detail
Sure.

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?
 
Old 06-15-2005, 07:42 AM   #5
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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?
 
Old 06-15-2005, 08:56 AM   #6
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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() {
   foo.innerHTML = "" + ((1 * foo.innerHTML) + 1)
   bar.innerHTML = ...
   baz ...
}
But I've just come to realise something: what you probably want is for the HTML to monitor some data which you fetch through PHP (say, the content of column x, row y in table z).

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?
 
Old 06-15-2005, 09:19 AM   #7
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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.
 
Old 06-15-2005, 09:37 AM   #8
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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.
 
Old 06-15-2005, 09:38 AM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
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.
 
Old 06-15-2005, 09:43 AM   #10
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
PHP configuration with apache web server vikrambhimbar Linux - Software 4 12-17-2005 04:04 AM
drawing line in php on web page pranith Programming 6 06-21-2005 04:14 AM
setting up a php web server mrobertson Programming 4 06-02-2005 01:29 PM
Receiving Emails through web server? (PHP) gothgeek84 Programming 12 01-16-2005 03:02 PM


All times are GMT -5. The time now is 02:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration