Here my first question in SO:
I have a simple log.json where are wrote some messages like:
{"name":"Bob","message":"Hey there"}{"name":"Alice","message":"Hi Sir"}
So, what I would is to read the content of log.json and put it in a simple html table,
I did like this:
<?php
session_start();
if ($_SESSION['me'] == "") { // Just in case you got a session
header("location:index.php");
}
$logFile = "log.json";
$data = file_get_contents($logFile);
$json_data = json_decode($data);
?>
<html>
<head>
<title>Message History</title>
</head>
<body>
<table border="1px">
<tr>
<td>
User
</td>
<td>
Messages
</td>
</tr>
<?php
foreach ($json_data as $row) {
?>
<tr>
<td>
<?= $row->name; ?>
</td>
<td>
<?= $row->message; ?>
</td>
<?php
} // close my loop
?>
</tr>
</table>
<br />
<input type="button" value="Back Home" onclick="location.href='home.php';">
</body>
</html>
Of course doesn't work.. any idea or advise? Thank you guys!
Edit: Here is how i write json in log.json:
$logFile = "log.json";
$file = json_encode(array('name'=>$user_name, 'message'=>$user_message));
file_put_contents($logFile, $file, FILE_APPEND | LOCK_EX);