radi8 Posted April 21, 2009 Share Posted April 21, 2009 This may sound like a strange request, but I want to load a page and do a single page refresh after the page loads to trigger an event. I hope that this explanation is clear enough. I can provide additional details if you need them. The reason this request is as follows: When my page is called, I am retrieving some 'GET' values and will then poll the database as soon as the page loads, but this may take about 10 seconds to complete. During this process, the page is still in the process of loading from the previous page and appears to not do anything. I have tried reading the database when the user clicks on the Process option What I need to do is load the page, and then do a refresh, or perform a timed event of some sort, that will trigger a get event to load the data from the database. During the load process, I will display a message to the user stating that the data is loading. Does anyone have a method to do a SINGLE page refresh 5 seconds after the page is initially loaded? Or, any other technique that you can suggest is also welcome. I am attaching the php code I am using for reference. Please excuse my poor coding techniques... Thanks in advance for any help you can provide. <?php /* Chart the sales for the selected item 1. Select starting date (converto to julian) 2. select branch (secondary) 3. plot the chart */ ob_start(); include ('dbconn.php'); session_start(); header("Cache-control: private"); // IE 6 Fix. $dodebug=0; if($_GET['lo']==1)sessDestroy();//$_SESSION['Access_Granted'] = 0; //sessDestroy();; if(strlen($_GET['litm']) < 13)$litm=""; // set to error message eventually else $litm=str_replace("\\","",$_GET['litm']); $branch=$_GET['branch']; $valid=false; if(strlen($litm)==0 || strlen($branch)==0)$valid=false; else{ $valid=true; // build working arrays $arr = array_fill(1,12,0); $arr1 = array_fill(1,12,0); $arr2 = array_fill(1,12,0); $months = array(); $year = date('Y'); $year1 = $year-1; $year2 = $year-2; $Year_ttl=0; $Year1_ttl=0; $Year2_ttl=0; $startjul=100365+(1000*($year2-2001)); $endjul = 100365 +(1000*($year1-2000)); $months[1]='Jan'; $months[2]='Feb'; $months[3]='Mar'; $months[4]='Apr'; $months[5]='May'; $months[6]='June'; $months[7]='July'; $months[8]='Aug'; $months[9]='Sept'; $months[10]='Oct'; $months[11]='Nov'; $months[12]='Dec'; $maxval=0; $aitm=""; // DATABASE CONNECTION AND RETRIEVAL $link = mssql_connect($mssql_hostname,$mssql_username,$mssql_password) or DIE("Database failed to respond (Index)."); mssql_select_db($mssql_database)or DIE("Database unavailable"); // define the SQL $sql = "select sdlitm, sdaitm, sduorg,"; $sql.=" cast(month(dateadd(day,(sdtrdj-((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 100)*1000)-1),cast(cast((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 2000) as nvarchar(4)) + '-1-1 12:00:00.00' as datetime))) as int) as 'mon',"; $sql.=" cast(year (dateadd(day,(sdtrdj-((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 100)*1000)-1),cast(cast((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 2000) as nvarchar(4)) + '-1-1 12:00:00.00' as datetime))) as int) as 'yr'"; $sql.=" from dbo.table1"; //$sql.=" where sdtrdj>".$startjul." and sdtrdj <=".$endjul." and sdlitm = '".$litm."' and sdmcu like'%".$branch."' and sddcto in('SO','ST')"; $sql.=" where sdtrdj>".$startjul." and sdlitm = '".$litm."' and sdmcu like'%".$branch."' and sddcto in('SO','ST')"; //$sql.=" order by yr, mon"; $sql.=" union"; $sql .= " select sdlitm, sdaitm, sduorg,"; $sql.=" cast(month(dateadd(day,(sdtrdj-((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 100)*1000)-1),cast(cast((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 2000) as nvarchar(4)) + '-1-1 12:00:00.00' as datetime))) as int) as 'mon',"; $sql.=" cast(year (dateadd(day,(sdtrdj-((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 100)*1000)-1),cast(cast((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 2000) as nvarchar(4)) + '-1-1 12:00:00.00' as datetime))) as int) as 'yr'"; $sql.=" from dbo.table2"; //$sql.=" where sdtrdj>".$startjul." and sdtrdj <=".$endjul." and sdlitm = '".$litm."' and sdmcu like'%".$branch."' and sddcto in('SO','ST')"; $sql.=" where sdtrdj>".$startjul." and sdlitm = '".$litm."' and sdmcu like'%".$branch."' and sddcto in('SO','ST')"; $sql.=" order by yr, mon"; $rs = mssql_query($sql); if (mssql_num_rows($rs) > 0){ $valid=true; for($i = 0;$i < mssql_num_rows($rs);$i++){ $rss = mssql_fetch_row($rs); $aitm=$rss[1]; if($rss[4]==$year){ $arr[$rss[3]]+=$rss[2]; $Year_ttl+=$rss[2]; if($arr[$rss[3]]>$maxval)$maxval=$arr[$rss[3]]; } else if($rss[4]==$year1){ $arr1[$rss[3]]+=$rss[2]; $Year1_ttl+=$rss[2]; if($arr1[$rss[3]]>$maxval)$maxval=$arr1[$rss[3]]; } else if($rss[4]==$year2){ $arr2[$rss[3]]+=$rss[2]; $Year2_ttl+=$rss[2]; if($arr2[$rss[3]]>$maxval)$maxval=$arr2[$rss[3]]; } } } // close the link and flush the dataset mssql_free_result($rs); mssql_close($link); } if($valid){ $_SESSION['arr'] =$arr; $_SESSION['arr1'] =$arr1; $_SESSION['arr2'] =$arr2; $_SESSION['year'] =$year; $_SESSION['year1'] =$year1; $_SESSION['year2'] =$year2; $_SESSION['months'] =$months; $_SESSION['maxval'] =$maxval; $_SESSION['litm'] =$litm; //create the grid data strings $rowHead ="<table width=\"100\" border=\"1px\" align=\"center\"><caption>Item Grid data for $litm, based on Transaction Date</caption>"; $rowMonth ="<tr><th scope=\"col\"> </th>"; $rowDetail ="<tr><th scope=\"row\">".$year."</th>"; $row1Detail ="<tr><th scope=\"row\">".$year1."</th>"; $row2Detail ="<tr><th scope=\"row\">".$year2."</th>"; for($i=1;$i<=12;$i++){ $rowMonth .="<th scope=\"col\">".$months[$i]."</th>"; $rowDetail .="<td>".number_format($arr[$i], 0, '.', ',')."</td>"; $row1Detail .="<td>".number_format($arr1[$i], 0, '.', ',')."</td>"; $row2Detail .="<td>".number_format($arr2[$i], 0, '.', ',')."</td>"; } $rowMonth .="<th scope=\"col\">Total</tr>"; $rowDetail .="<td>".number_format($Year_ttl, 0, '.', ',')."</td></tr>"; $row1Detail .="<td>".number_format($Year1_ttl, 0, '.', ',')."</td></tr>"; $row2Detail .="<td>".number_format($Year2_ttl, 0, '.', ',')."</td></tr>"; $rowFooter ="</table>"; // END DATABASE SECTION AND NSI SPECIFIC SECTION } if($dodebug > 0){ echo "<pre>"; if($dodebug==1){ echo "--> Form data (POST) <-- <br>"; print_r ($_POST); } if($dodebug==2){ echo "--> Form data (SESSION) <-- <br>"; print_r ($_SESSION); } if($dodebug==3){ echo "--> Form data (SERVER) <-- <br>"; print_r ($_SERVER); } if($dodebug==4){ echo "--> PHP Info <-- <br>"; phpinfo(); } echo "</pre>"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Intranet</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <table width="100%" border="0" cellspacing="0" cellpadding="0"> </center> <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#f2f1df"> <tr> <td><div align="center"> <table width="80%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <br> <h2 align="center"><strong><font size="+1" face="Georgia, Times New Roman, Times, serif"> Manufacturing Price/Cost Analysis Item Charting Utility</font></strong></h2> <hr> <form action= <?php echo $_SESSION['http'].$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; ?>> <p> <h2 align="center"><strong><font size="+1" face="Georgia, Times New Roman, Times, serif"> <?php if($valid) print "2 year Analysis of '".$aitm."' (".$litm.") for Branch: ".$branch; ?> </font></strong></h2> <DIV STYLE='background-color: #FAFAFA; '> <DIV STYLE='font-family: tahoma; color: #808080; font-size: 12px; padding: 15px; padding-top: 5px; padding-bottom: 5px' ALIGN=justify> <B> <?php if($valid) echo $litm."'s sales history chart"; else echo " "; ?> </B> <BR> <DIV STYLE='border-top: 1px dotted #CCCCCC; height: 16px;'></DIV> <?php if($valid){?> When ready to view all bucketed sales (SO/ST) of the item from <?php echo "1/1/".$year2 ?> to present, please click on the following link: <A HREF='javascript:render(<?php echo "\"".$litm."\",".$branch ?>);' CLASS=InnerFunctionLink>CHART IT!</A>. <?php } else echo "No valid item passed to form, press the back button and try again"; ?> <BR><BR> <CENTER> <DIV ID=Rendered STYLE='width: 675px; height: 475px;' ALIGN=Left> <DIV ID=Render_1> <TABLE NOBORDER CELLPADDING=0 CELLSPACING=0 STYLE='margin: 5px;'> <TR VALIGN=Bottom> <TD WIDTH=20><IMG SRC='images/chart_bar.png' WIDTH=16 HEIGHT=16></TD> <TD><?php if($valid) echo "To view chart, click on the 'CHART IT!' link above.";?></TD> </TR> </TABLE> </DIV> <DIV ID=Render_2 STYLE='background-color: #DBFFCF; padding: 4px; display: none;'> <TABLE NOBORDER CELLPADDING=0 CELLSPACING=0> <TR VALING=Middle> <TD><IMG SRC='images/lightning.png' WIDTH=16 HEIGHT=16></TD> <TD> The server is processing your request...</TD> </TR> </TABLE> </DIV> <DIV ID=Render_3 STYLE='display: none;' ALIGN='center'> </DIV> <DIV ID=Render_4 STYLE='background-color: #FFCCCC; padding: 4px; display: none;'> <TABLE NOBORDER CELLPADDING=0 CELLSPACING=0> <TR VALING=Middle> <TD><IMG SRC='images/exclamation.png' WIDTH=16 HEIGHT=16></TD> <TD> The request is taking too much time, remote peer not responding. Try again later.</TD> </TR> </TABLE> </DIV> <DIV ID=Render_5 STYLE='padding: 4px; display: none;' ALIGN='center'> <TABLE NOBORDER CELLPADDING=0 CELLSPACING=0 ALIGN=center bgcolor="ffcccc"> <TR><TD> <?php print $rowHead; print $rowMonth; print $rowDetail; print $row1Detail; print $row2Detail; print $rowFooter; print "<br><strong><font size=+1>Use the Back Button to return to the item Cost Page</font></strong>"; ?> </TD></TR> </TABLE> </DIV> </DIV> </CENTER> </DIV> </DIV> </form> </td> </tr> </table> </td> </tr> </table> </body> <SCRIPT> /* Some global vars definition */ var LoadTries = 0; var MaxLoadTries = 40; var Preloader = new Image(); var URL; function render(litm, branch) { RenderHost = "localhost:8080"; URL = "http://"+RenderHost+"/"+litm+".png"; Preloader.src = URL; document.getElementById("Render_1").style.display = "none"; document.getElementById("Render_3").style.display = "none"; document.getElementById("Render_4").style.display = "none"; document.getElementById("Render_5").style.display = "none"; document.getElementById("Render_2").style.display = "inline"; LoadTries = 0; setTimeout ("CheckLoadingStatus()", 500); } function CheckLoadingStatus() { if ( !Preloader.complete ) { LoadTries++; if ( LoadTries >= MaxLoadTries ) { document.getElementById("Render_2").style.display = "none"; document.getElementById("Render_4").style.display = "inline"; } else setTimeout ("CheckLoadingStatus()", 500); } else { document.getElementById("Render_2").style.display = "none"; document.getElementById("Render_3").innerHTML="<IMG SRC='"+URL+"' WIDTH=650 HEIGHT=350>"; document.getElementById("Render_3").style.display = "inline"; document.getElementById("Render_5").style.display = "inline"; } } </script> <?php ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 21, 2009 Share Posted April 21, 2009 If your query is taking 10 seconds. You need to think about restructuring it. what exactly are you trying to accomplish here cast(month(dateadd(day,(sdtrdj-((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 100)*1000)-1),cast(cast((cast(substring(cast(sdtrdj as nvarchar(6)),2,2) as decimal) + 2000) as nvarchar(4)) + '-1-1 12:00:00.00' as datetime))) as int) as 'mon', it could probably be done differently or some of it handled with php Quote Link to comment Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 and as far as a delayed page refresh... you'd wanna look into meta refresh... Quote Link to comment Share on other sites More sharing options...
radi8 Posted April 21, 2009 Author Share Posted April 21, 2009 The date in the database is in the form of a IBM julian date, i.e. today's date is 04/21/2009, in julian this would be 109111. This SQL command will return the month and/or year(I know it is long winded, but it works and was supplied by another forum). Also, this SQL is NOT the reason why the reply takes 10S, it is because I am returning many, MANY, thousands of rows. It takes time to poll and return a dataset of that size (and yes, it is all necessary). The original request was NOT to critique the SQL (I should have left it out) but rather to find a method of loading the page and then automatically loading the dataset after the page has been completely loaded. otherwise, the user is left at the original page waiting for the new page to load. Using the Meta refresh is not viable because I only want it to refresh 1 time, and i cannot find a way to turn off the <meta http-equiv="refresh" content="5"> after the first time. Quote Link to comment Share on other sites More sharing options...
taith Posted April 21, 2009 Share Posted April 21, 2009 move your refresh into php if(!isset($firstload)){ echo '<meta http-equiv="refresh" content="5">'; $firstload=true; } Quote Link to comment Share on other sites More sharing options...
radi8 Posted April 22, 2009 Author Share Posted April 22, 2009 This worked with a slight modification... I changed the $firstload variable to a $_SESSION['firstload'] variable and preset it on the calling page (adds some added functionality that I was able to use). The final code segment is as follows: (calling app) <?php if($_SESSION['itemCostState']==1 && strlen($litm)==13){ unset($_SESSION['firstload']); print "<a href=litmplot.php?litm=".$litm."&branch=".$mcu." target=\"_top\">View Item History</a>"; } ?> Working app: <?php // load the page and refresh after 2 seconds, allows the default page to display before // database fetch is initialized if(!isset($_SESSION['firstload'])){ echo '<meta http-equiv="refresh" content="2">'; $_SESSION['firstload']=true; $valid=99; } else{ // do data fetch after the page refresh... unset($_SESSION['firstload']); if(strlen($_GET['litm']) < 13)$litm=""; else $litm=str_replace("\\","",$_GET['litm']); $branch=$_GET['branch']; ... <do database fetch and data parsing routines>... } ?> Thank you all for helping me get this straightened out. The solution was a lot easier than I initially expected, but it works extremely well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.