Trying to improve some of my code, I have one page that runs a large query on a database and then displays some results in a table. Depending on the options the user selects the query can sometimes take a while (~20 seconds).
I'd like a message to appear while the query is being ran, then to be replaced by the table when the query has finished.
I got some code of another site which claims to do something similar:
<?php
/ connect to database, and make query ... /
$db = mysql_connect('host', '1234564', '87*(&&%&NSCNBNZT£%')
or die("Failure to connect with database");
mysql_select_db('mydata');
?>
<HTML>
<HEAD>
<script Language = "Javascript">
function changeContent(divName, sContent){
var divlayer;
if (document.getElementById) {
// Standards Compliant
divlayer = document.getElementById(divName);
divlayer.innerHTML = sContent;
}
else if(document.all){
// IE 4/5 ...
divlayer = document.all[divName];
divlayer.innerHTML = sContent;
}
else if(document.layers){
// Netscape Navigator 4.x
divlayer = document.layers[divName];
divlayer.document.open();
divlayer.document.write(sContent);
divlayer.document.close();
}
else {
// other browsers
}
}
</script>
<HEAD>
<BODY>
<!---
Progress Bar - uses image and text
--->
<p>Loading - Please Wait </p><br>Progress:
<img id="imgStatus" src="images/blue2.gif" height="10" width="0" align="left"><div id="pctdone"> </div>
<br>
<?
/ start query /
$query = ("(SELECT * FROM user WHERE $userid = 1");
$result = @($query);
/ Prepare for Progress Bar /
ob_flush();
$tblwidth = 600; # width of progress bar image
$last_width = -1;
$numrows = @mysql_numrows($result);
$irow = 0;
/ start processing the mysql $array with something like /
while ($array = @mysql_fetch_array($result)) {
//Update Progress Bar
$irow++;
$leftwidth = number_format(($irow * $tblwidth) / $numrows, 0);
if ($leftwidth != $last_width)
{
$last_width = $leftwidth;
echo("<script>document.images.item('imgStatus').width=$leftwidth ;</script>");
$sContent = "<font face=\"Tahoma\" color=\"0000FF\" size=\"-2\">Loading $irow ...</font>";
echo("<script>changeContent('pctdone', '$sContent');</script>");
ob_flush();
}
/ ***************** /
} # End of array processing ....
/ ****************
Update Progress Bar with Done message
**************** /
echo("<script>document.images.item('imgStatus').width=$tblwidth; </script>");
$sContent = "<font face=\"Tahoma\" color=\"0000FF\" size=\"-2\">Done!</font>";
echo("<script>changeContent('pctdone', '$sContent');</script>");
ob_flush();
?>
When I run the code I get a progress bar, but a couple of the ob_flush() funcitons cause errors.
Anyone know why this is happening or can suggest a better way of doing this. Any help would be greatly appreciated.