<?php
$fname = "countlog.txt";
if(file_exists($fname){ // ตรวจสอบว่ามีไฟล์อยู่หรือไม่
// มีไฟล์
$cntfile = fopen($fname,"r"); // เปิดไฟล์
$count = fgets($cntfile,1000); // นำค่าออกมาใช้
}else{
// ไม่มี
$count = 0;
}
fclose($cntfile); // ปิดไฟล์
$count=$count+1; // บวกเพิ่ม
echo "<div class=\"counter\">counter : ".$count."</div>"; // แสดงผลลัพธ์
$cntfile = fopen("countlog.txt","w"); // เปิดไฟล์echo "<div class=\"counter\">counter : ".$count."</div>"; // แสดงผลลัพธ์
fwrite($cntfile, $count); // บันทึกเลขใหม่ทับลงในไฟล์
fclose($cntfile); // ปิดไฟล์
?>
Description
โปรแกรม counter ง่ายๆข้างต้นนี้ใช้ textfile ในการเก็บค่าตัวแปร และนับเพิ่มขึ้นเรื่อยๆ ท่านสามารถดัดแปลงไปใช้เป็น นับค่าลดลงเรื่อยๆ เช่น เรียกโปรแกรมให้ทำงานได้เท่าที่กำหนดไว้ได้เช่นกันครับ
0 ความคิดเห็น