I am a beginner PHP coder, so if my code looks way crazy, apologies up front.
I have been tasked with (trying to) write a program that will loop through a list of databases (and servers too but I will figure that part out later), send each database the same query, then display the results for few seconds, then the next query ect... I have this close in that I can display all of the results the way I want, but it is all one after the other on the same page and you would need user interaction to scroll down to see each db result. I am trying to do this in a way that once started, will just loop through the databases one at a time and display it without user interaction (It will be on a TV for users to refer to throughout the day). Another problem is that I am stuck using 5.3 update24 I think (we have other programs that use it and wont run on anything newer (that's another project for another day).
I tried to use session_start()
with session_write_close()
or session_destroy()
or Sesson_reset()
at the end of the loop but this just re-initializes the same results, as opposed to for example draw the table with results of database1, wait 3 seconds, clear the screen, then display the next database results in the loop.
I'm close to giving up since I could find no one else that has done this or at least no one that asked the same question that I could find. My code is below, the query was anonymized..
<html>
<head>
<title>stuff older than 3 days</title>
<link rel="stylesheet" type="text/css" href="style.php"/>
</head>
<body>
<?php
/*
* Comments here
*/
$serverName = "192.168.0.90";
$databaseName = "";
$dbUsername = "xxdbuserxx";
$dbUserpass = "xxdbpassxx";
$db_Name_Array = parse_ini_file("dbname.ini");
$db_Srv_Array = parse_ini_file("srvname.ini");
$k=0;
$countq=0;
//Loop through each database listed in the ini file
foreach($db_Name_Array as $section=>$values){
//$databaseName=$section
$connectionInfo = array("UID" => $dbUsername, "PWD" => $dbUserpass, "Database" => $section);
$link = sqlsrv_connect($serverName, $connectionInfo);
//read the array and print
$sql = "SELECT Field1, Field2, Field3, Field4, Field5, Field6 from Table;
//stmt1 is solely for retrieving the number of rows, unfortunately it cant be retrieved from the normal stmt query.
$stmt1 = sqlsrv_query( $link, $sql, array(), array('Scrollable' => 'buffered'));
//display the number of results in the case that there are too many to see on the screen
$countq=sqlsrv_num_rows($stmt1);
// now do the same query but for displaying them to the screen
$stmt = sqlsrv_query( $link, $sql);
echo "<h2>$values ----- ( # of sessions older then 3 days: $countq )</h2>";
echo"<table class='table1'>";
//set the Header
echo"<tr class='qtop1'><td>field1</td><td>field2</td><td>Field3</td><td>Field4</td><td>Field5</td><td>Field6</td></tr>
";
//loop through the results changing color of every other row to make it easier to read
while ($row = sqlsrv_fetch_array($stmt)){
if ( isset($k) and $k==0){
echo"<tr class='d0'><td>{$row['Field1']}</td><td>{$row['Field2']}</td><td>{$row['Field3']}</td><td>{$row['Field4']}</td><td>{$row['Field5']}</td><td>{$row['Field6']}</td></tr>
";
$k=1;
}
else{
echo"<tr class='d1'><td>{$row['Field1']}</td><td>{$row['Field2']}</td><td>{$row['Field3']}</td><td>{$row['Field4']}</td><td>{$row['Field5']}</td><td>{$row['Field6']}</td></tr>
";
$k=0;
}
}
echo"</table>";
if( $stmt === false ){
die( print_r( sqlsrv_errors(), true));
}
}
?>
</body>
</html>
Place the following code within the <head>
section of your page.
<meta http-equiv="refresh" content="3">
This will tell the browser to refresh/reload every 3 seconds.
Leave the session_start() on the page but remove all of the other session_*() functions.
EDIT
What you are wanting is pagination with auto-refresh. Looking at your code you would need to make the following changes:
<?php
session_start();
//--- keep track of where we are
if( !isset($_SESSION['PageCount']) )
$_SESSION['PageCount'] = 0;
/*
* Comments here
*/
$serverName = "192.168.0.90";
$databaseName = "";
$dbUsername = "xxdbuserxx";
$dbUserpass = "xxdbpassxx";
$db_Name_Array = parse_ini_file("dbname.ini");
$db_Srv_Array = parse_ini_file("srvname.ini");
$k=0;
$countq=0;
$itemsPerPage = 2; //--- how many entries to display Must be more than 1
$currentPage = $_SESSION['PageCount'];
//--- loop through number of databases that need to be displayed
for($x=0; $x<$itemsPerPage; $x++ ) {
//--- get the info about the db and server from the respective arrays
$dbName = $db_Name_Array[$currentPage];
$dbSrv = $db_Srv_Array[$currentPage];
//--- make the connection to the database using $dbName and $dbSrv info
//--- get the data and format it
//--- increment the current page number
$currentPage++;
//--- set to zero to loop to the first entry
if( $currentPage >= count( $db_Name_Array) ) {
$currentPage = 0;
}
}
//--- save the current page number to the session var
$_SESSION['PageCount'] = $currentPage;
?>
The above will list 2 or more entries ($itemsPerPage).
The $_SESSION['PageCount'] keeps track of which items to display.
As the code stands, the last item on the list will be the first item on the list on the next refresh.