0

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?

Kai
  • 93
  • 2
  • 8
  • 2
    MATCH up your single and double quotes !!!! Thats your issue – RiggsFolly Apr 07 '18 at 14:38
  • Just change `? "#7d3" : "red").text('⚫');` to this `? '#7d3' : 'red').text('⚫');` to remove the accidental use of double quotes inside a double quoted string – RiggsFolly Apr 07 '18 at 14:40
  • @kai. Use \ before all double quotes within echo statement. else use single quotes only within script tag code. – Chaitanya Ghule Apr 07 '18 at 14:43
  • Hi guys, thanks for the feedback, I'm now using single quotes for tags within my script but now I'm getting `Syntax error, unexpected end of file` error after I end my PHP script I simply have two HTML tags `

    ` followed by `

    – Kai Apr 07 '18 at 16:13

0 Answers0