You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
912 B
40 lines
912 B
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tts;
|
|
|
|
final class json {
|
|
|
|
public static function do_json_headers(int $status_code = 200) {
|
|
if (!headers_sent()) {
|
|
header($_SERVER['SERVER_PROTOCOL'] . " " . $status_code);
|
|
header('Content-Type: application/json; charset=utf-8', true, intval($status_code));
|
|
}
|
|
}
|
|
|
|
public static function output(array $data, string $misc = '') {
|
|
$number_of_rows = \bs_tts\common::get_count($data);
|
|
|
|
self::do_json_headers();
|
|
|
|
$dt = new \DateTime();
|
|
$ds = $dt->format('Y-m-d h:i:s A');
|
|
|
|
echo "{ \r\n \"records_total\": {$number_of_rows}, \r\n";
|
|
echo "\"date_stamp\": \"{$ds}\", \r\n";
|
|
|
|
if (! empty($misc) ) {
|
|
echo $misc;
|
|
}
|
|
|
|
if ($number_of_rows > 0) {
|
|
echo '"data":[';
|
|
echo json_encode($data);
|
|
echo ']}'; // end JSON object
|
|
} else {
|
|
echo '"data":[]}'; // No users!!!
|
|
}
|
|
}
|
|
|
|
} |