I've been playing around with this. I think that it would be a good idea to have the bash script write/overwrite a flag file when it's finished making the new chunks and to use that, rather than the main data.txt as the file for the live page to look at to tell it when things have updated. That way we should be able to avoid a situation where the data.txt file has been updated but the chunks haven't all been updated yet.
A fine point. For now I have it working off of the data.txt file, like so:
PHP Code:
<?php
$refresh = isset($_GET['refresh']);
$datapage = 'data.txt'; //can include SERVER path to the data file if different than this page's
$chunksize = 10; //number of lines in each chunk file
//Each chunk file created by bash should have a file name of $chunkprefixSOMENUMBER$chunkext
//with SOMENUMBER starting at 0 and going sequentially from there
$chunkprefix = 'tablefile'; //can include WEB path to the chunk files if different than this page's
$chunkext = '.htm';
$filetime = filemtime($datapage);
if(!$refresh){
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
#datatable th, #datatable td {
border: 1px solid orange;
}
#datahead th {
color: red;
padding: 0.25em
}
#datahead1 {
width: 6em;
}
#dataArea td {
padding: 0.5em;
}
.pages {
display: block;
float: left;
margin: 0.5em 0.5em 0 0;
background-color: lightyellow;
border: 1px solid black;
padding: 0.5em;
width: 3.5em;
text-align: center;
text-decoration: none;
}
.pages.activepage, .pages:hover {
border-color: red;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$.ajaxSetup({ cache: false });
function pagefunction(e, override){
if(!$(this).hasClass('activepage') || override === 'override'){
$('#dataArea').load(this.href);
$pages.removeClass('activepage').filter(this).addClass('activepage');
}
e.preventDefault();
}
var $pages = $('.pages').click(pagefunction), filetime = <?php echo $filetime; ?>, $wrap = $pages.parent();
$('#tablecontainer').height($('#tablecontainer').height());
$('#update').text('<?php echo date ("F jS, Y g:ia T", $filetime); ?>');
$pages.eq(0).addClass('activepage');
setInterval(function(){
$.ajax({
url: '<?php echo $_SERVER['PHP_SELF']; ?>',
data: {refresh: 'true'},
success: function(data){
data = data.split('^');
if(filetime == data[0]){
return;
}
filetime = data[0];
$('#update').text(data[2]);
var $active = $('.pages.activepage'), numpages = $pages.length, i = -1, activeindex = $pages.index($active);
if(numpages != data[1]){
$wrap.empty();
while(++i < data[1]){
$wrap.append('<a class="pages" href="<?php echo $chunkprefix; ?>' + i + '<?php echo $chunkext; ?>">Page ' + (i + 1) + '</a>');
}
$pages = $('.pages').click(pagefunction);
activeindex = $pages.length > activeindex? activeindex : 0;
}
$pages.eq(activeindex).addClass('activepage').trigger('click', ['override']);
}
});
}, 9 * 1000);
});
</script>
</head>
<body>
Last Updated: <span id="update"></span>
<div id="tablecontainer">
<table id="datatable">
<thead>
<tr id="datahead"><th id="datahead1">Column 1</th><th id="datahead2">Column 2</th><th id="datahead3">Column 3</th><th id="datahead4">Column 4</th><th id="datahead5">Column 5</th><th id="datahead6">Column 6</th></tr>
</thead>
<tbody id="dataArea">
<?php
}
$file = file($datapage);
$files = array_chunk($file, $chunksize);
$pages = count($files);
$page = -1;
if(!$refresh){
include $chunkprefix . '0' . $chunkext;
?>
</tbody>
</table>
</div>
<div>
<?php
while(++$page < $pages){
?>
<a class="pages" href="<?php echo "$chunkprefix$page$chunkext"; ?>"><?php echo "Page " . ($page + 1); ?></a>
<?php
}
?>
</div>
</body>
</html>
<?php
} else {
echo $filetime . '^' . $pages . '^' . date ("F jS, Y g:ia T", $filetime);
}
?>
This assumes chunk files like:
HTML Code:
<tr><td>201212099</td><td>080956</td><td>00</td><td>0973517500</td><td>00</td><td>04813,1,49,G,071212,K</td></tr>
<tr><td>20121112</td><td>161318</td><td>00</td><td>01675032799</td><td>00</td><td>11971,1,45,G,121112,K</td></tr>
<tr><td>20121113</td><td>092155</td><td>00</td><td>0974522910</td><td>00</td><td>04900,1,78,T,121112,K</td></tr>
<tr><td>20121113</td><td>092711</td><td>00</td><td>0974522910</td><td>00</td><td>04900,1,61,G,131112,K</td></tr>
<tr><td>20121113</td><td>140032</td><td>00</td><td>01675032799</td><td>00</td><td>11971,1,21,G,131112,K</td></tr>
<tr><td>20121113</td><td>140148</td><td>00</td><td>01675032799</td><td>00</td><td>11971,1,47,T,131112,K</td></tr>
<tr><td>20121113</td><td>145753</td><td>00</td><td>01666723117</td><td>00</td><td>04828,1,47,G,121112,K</td></tr>
<tr><td>20121113</td><td>145846</td><td>00</td><td>01666723117</td><td>00</td><td>04828,1,41,G,131112,K</td></tr>
<tr><td>20121113</td><td>150631</td><td>00</td><td>01675032799</td><td>00</td><td>11971,1,09,T,131112,K</td></tr>
<tr><td>20121113</td><td>192056</td><td>00</td><td>0974958709</td><td>00</td><td>04900,1,43,G,131112,K</td></tr>
If you have any problem outputting chunks like that, let me know. However, it would be best to use the above format.
Also - The bash need not delete the old chunk files, though it can. All it has to do is overwrite them. Any extra ones (like if there end up being less chunks after an update) will be ignored by this script.
Added Later:
I played around with this a bit more. This time with a flag file. It really is the way to go because it can hold the new number of chunk files - all the live page needs to know in order to update, which saves a lot of time when refreshing. I have it worked out, along with a PHP version of what the bash would need to do. If you're interested, let me know.
Bookmarks