function readRecord($id, $path, $mode) { $retries = 0; $max_retries = 20; $fp = fopen($path, 'r'); do { if ($retries > 0) { usleep(rand(1, 10000)); } $retries += 1; } while (!flock($fp, LOCK_SH) and $retries <= $max_retries); if ($retries == $max_retries) { return false; } $json_string = file_get_contents($path); flock($fp, LOCK_UN); fclose($fp); $fp = fopen($path, $mode); $retries = 0; do { if ($retries > 0) { usleep(rand(1, 10000)); } $retries += 1; } while (!flock($fp, LOCK_EX) and $retries <= $max_retries); if ($retries == $max_retries) { return false; } $json = json_decode($json_string, true); if ($json == null) { $json['data'] = []; } while (count($json['data']) < $id) { $data = genData(); for ($i = 0; $i < count($data); $i++) { $json['data'][] = $data[$i]; } } $returnData = array('angles' => $json['data'][$id - 1], 'id' => $id); $returnJson = json_encode($returnData); echo $returnJson; fwrite($fp, json_encode($json)); flock($fp, LOCK_UN); fclose($fp); return true; }
</tr> </thead> <tbody> <tr> <td class="merge" style="border-right: none;border-bottom: 1px solid black;padding: 4px 5px;"></td> <td colspan="10" class="group lefttext" style="text-weight: bold;text-align:left;border-right: 1px solid black;border-bottom: 1px solid black;padding: 4px 5px;">สาระการเรียนรู้พื้นฐาน</td> </tr> <!-- Begin Basic Subject Data --> <?php echo genData($dataInfoBasic);?> <!-- End Basic Subject Data --> <tr> <td class="merge" style="border-right: none;border-bottom: 1px solid black;padding: 4px 5px;"></td> <td colspan="10" class="group lefttext" style="text-weight: bold;text-align:left;border-right: 1px solid black;border-bottom: 1px solid black;padding: 4px 5px;">สาระการเรียนรู้เพิ่มเติม</td> </tr> <!-- Begin Extra Subject Data --> <?php echo genData($dataInfoExtra);?> <!-- End Extra Subject Data --> </tbody> </table> </div> <div id="footer" style="width: 100%;vertical-align: middle;"> <table cellspacing="0" cellpadding="0" style="width:100%;padding-top:20px;"> <tr> <td style="width: 50%;"> <div id="sum" style="display:block;width: 100%;margin: auto;padding-top: 20px;vertical-align: top;"> <table cellspacing="0" style="border-top: 1px solid black;border-left: 1px solid black;font-size: 0.8em;width: 100%;"> <thead> <tr> <th rowspan="2" style="font-weight: normal;border-right: 1px solid black;border-bottom: 1px solid black;padding: 4px 5px;">สรุปผลการประเมิน</th> <th colspan="2" style="font-weight: normal;border-right: 1px solid black;border-bottom: 1px solid black;padding: 4px 5px;">ผลการเรียนที่ได้</th> </tr>