My PHP script connects to multiple Raspberry PIs that all have a CSV file of this format:
2018-03-22 12:43:21,NM_Test.h264,-2
I then take the multiple CSV files and display them as a HTML table:
<?php
require_once 'Net/SSH2.php';
require_once 'phpseclib1.0.10/Crypt/RSA.php';
//when you include/require like this it puts the content into the variable
//when that content is a PHP array, it puts it in the variable. This needs to be included each time the script is ran
$config = require 'config.php';
$log = 'logfile.txt';
if(is_array($config)){
foreach($config as $cred){
$ssh = new Net_SSH2($cred['ip'], $cred['port']); //i think this is port?
$key = new Crypt_RSA();
$key->loadKey($cred['key']);
if (!$ssh->login('pi', $key)){
//logging with file_put_contants, Append mode, exclusive lock is more race condition safe then an open file handle.
file_put_contents($log, "[".date('Y-m-d H:i:s')."]Login Failed for {$cred['ip']}\n", FILE_APPEND|LOCK_EX);
continue;
//or you can echo it, but you don't want to kill the whole thing if one fails /maybe?
}
//echo or save to file etc.
$output = $ssh->exec('tail -1 /var/log/playlog.csv');
};
echo"<script language='javascript'>
define('data', '$output');
function circle(errorStatus) { return $('<span>').css('color', errorStatus == '0' ? "#7d3" : "red").text('⚫');
const columnNames = ['Status', 'Date/Time', 'Video', 'Error Status'];
const head = $('<thead>').append($("<tr>").append(columnNames.map(h =>
$('<th>').text(h))));
}
const body = $("<tbody>").append(data.split("\n").map(row => {
const cdata = row.split(",");
const columns = [circle(cdata[2]), ...cdata].map(c => $('<td>').html(c));
return $("<tr>").append(columns);
}));
const table = $("<table>").addClass("table table-
striped").append(head).append(body);
$('#container').append(table);
</script>
";
?>
However, when I run the script I get the following syntax error:
PHP Parse error: syntax error, unexpected 'const' (T_CONST), expecting ',' or ';'
On this line:
const columnNames = ['Status', 'Date/Time', 'Video', 'Error Status'];
How can I fix this?
` followed by `
– Kai Apr 07 '18 at 16:13