Browse Source

Ajax 用の呼出プログラム作成

bc-ajax
みてるぞ 4 months ago
parent
commit
2cf8b64f1f
1 changed files with 66 additions and 0 deletions
  1. +66
    -0
      broadcast/services/fetch_data.php

+ 66
- 0
broadcast/services/fetch_data.php View File

@@ -0,0 +1,66 @@
<?php

const LOG_PATH = '../log.txt';

header ('Content-Type: application/json; charset=UTF-8');

$log_data = [];

$page = (int) (filter_input (INPUT_POST, 'p') ?? 1);
$length = (int) (filter_input (INPUT_POST, 'max') ?? 20);
$asc = (int) (filter_input (INPUT_POST, 'asc') ?? 0) !== 0;
$keyword = trim (filter_input (INPUT_POST, 'q') ?? '');

$date_start = filter_input (INPUT_POST, 'start');
$date_end = filter_input (INPUT_POST, 'end');

$f = fopen (LOG_PATH, 'r');

if ($f !== false)
{
while (($dt = fgetcsv ($f, 0, "\t")) !== false)
{
$chat_info = json_decode ($dt[1]);

$log_data[] = ['dateTime' => $dt[0],
'chatIcon' => $chat_info -> author -> imageUrl,
'chatName' => htmlspecialchars ($chat_info -> author -> name),
'chatMessage' => htmlspecialchars ($chat_info -> message),
'answer' => htmlspecialchars ($dt[2])];
}
}

fclose ($f);

unset ($f);

if ($keyword !== '')
{
$log_data = array_filter ($log_data, fn ($row) => (
strpos (($row['chatName'] . "\n" . $row['chatMessage'] . "\n"
. $row['answer']),
$keyword)
!== false));
}

if ($date_start)
{
$log_data = array_filter ($log_data, fn ($row) => (
substr ($row['dateTime'], 0, 10) >= $date_start));
}

if ($date_end)
{
$log_data = array_filter ($log_data, fn ($row) => (
substr ($row['dateTime'], 0, 10) <= $date_end));
}

$pages_max = (int) ((count ($log_data) - 1) / $length) + 1;

if (!($asc))
$log_data = array_reverse ($log_data);

$log_data = array_slice ($log_data, ($page - 1) * $length, $length, true);

echo json_encode ($log_data);


Loading…
Cancel
Save