Re: Looping through output



On Jun 1, 1:58 am, Gordon <gordon.mc...@xxxxxxxxxxxx> wrote:
On May 31, 7:28 pm, Lan Mind <LANMIN...@xxxxxxxxx> wrote:



So I'm outputting XML and I'm trying to output XML continuable in a
loop while a condition is true. Here is what I have:

$originalquestion= mysql_query("SELECT question, answer, id from
answers WHERE id='{$_SESSION['userid']}'");
$row = mysql_fetch_array($originalquestion);

while ($row['id']<=500)
{
   header("Content-type: text/xml");
   echo '<person>';
   echo "<result>" . $row['id'] . "</result>";
   echo "</person>";

}

So I'm outputting XML and I'm trying to output XML continuable in a
loop while a condition is true. Here is what I have:

CODE
$originalquestion= mysql_query("SELECT question, answer, id from
answers WHERE id='{$_SESSION['userid']}'");
$row = mysql_fetch_array($originalquestion);

while ($row['id']<=500)
{
header("Content-type: text/xml");
echo '<person>';
echo "<result>" . $row['id'] . "</result>";
echo "</person>";

}

When this script is run I'm getting a "junk after document element"
error. I believe since a new header is being started after the XML
root element's ending tag it's generating this error. I've never tried
looping through output like this and I don't know if it's possible. I
tried using PHP's flush() function in combination with the
time_nanosleep() function to send the XML output then start a new
output but it didn't work. Is it possible for PHP to keep outputting
DB query results?

I really can't tell for sure what you're trying to do, but from what
I've been able to gather from reading the other replies it sounds like
you're trying to do some kind of AJAX updating based on a database
field changing. In this case a better approach would probably be to
use AJAX to poll the server every few seconds (but not too often as it
will put load on the server and client machines) to get the current
value. In this case all your PHP script would need to do is spit out
an XML or JSON response to a query and not need to worry at all about
how the database might be changing during the execution of the script.

Hello Gordan,

Yes I am updating the client's browser with AJAX. I didn't know how a
server would act with so many requests but now I see I need to be
careful about it. Does anybody know of good articles detailing HTTP
requests and such? I'll use javascript to send a request every few
seconds. Thank you.
.



Relevant Pages

  • Re: How to check URL existence ?
    ... The advantage of XmlHttpRequest is that you can send such requests without causing the page to refresh, which you can't if you just type a URL in the location bar. ... There is a lot of hype lately about AJAX, which means Asynchronous JavaScript and XML. ... Note also that XmlHttpRequest is not the only object allowing you to send "invisible" requests to a server. ...
    (comp.lang.javascript)
  • Re: xmlHTTP Request returns 403 in FireFox when Post used
    ... same code that works on the old server is not working on the dedicated ... function createXMLHttpRequest() { ... With the code as is above, the requests works fine in IE. ... parameters runs correctly outside of AJAX. ...
    (comp.lang.javascript)
  • Re: Is foreach the fastest way to do this?
    ... when you have hundreds of requests coming in within a short period ... rather than worrying about which loop construct to use. ... Rather he would be running multiple servers with a heavy-duty database such as SQL Server, ...
    (comp.lang.php)
  • Re: Immediately display changes to an HTML element
    ... the server using Ajax ... ... but in reality quite a lot of what is called AJAX is ... the possibility that responses from the server could follow a different ... sequence from the requests made for them. ...
    (comp.lang.javascript)
  • Re: AJAX Mash-up Sites?
    ... living on another server. ... how a client can request external page content. ... Now you can make Ajax requests to both foo.com and bar.foo.com. ...
    (comp.lang.javascript)