使用PHP通过从TXT文件中读取名称来标记存在和不存在


marking present and absent using php by reading names from a txt file

我制作了一个php文件,其中我通过阅读"attend .txt"文件中给出的名称来标记出席和缺席。我该怎么做呢?它给了我错误的答案。我在标签

中应用了php
<html>

<head>
<style>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
th,td
{
padding:5px;
}
</style>
</head>
<body >

<div id="container" style="width:1250px">
<div id="header" style="background-color:#FFA500;">
<h1 style="margin-bottom:0;text-align:center;">WEBKIOSK ATTENDANCE</h1></div>
<div id="menu" style="background-color:#FFD700;height:500px;width:250px;float:left;">
<br>
<b>Teacher's name</b><br><br>
<b>class timing</b><br><br>
<b>Lecture/tutorial</b>
</div>
<div id="content" style="background-color:#CCCCFF;height:500px;width:1000px;float:left;text-align:right;">
<img src="logo.jpg"width="145" height="175">
<table align=center style="width:300px;">
<tr>
  <th style="color:#FF0000;"><i>S.NO</i></th>
  <th style="color:#FF0000;"><i>ENROLLMENT NO.</i></th>
  <th style="color:#FF0000;"><i>NAME</i></th>       
  <th style="color:#FF0000;"><i>PRESENT/ABSENT</i></th>
  </tr>
<tr>
<tr>
  <td>1.</td>
  <td>11102213</td>
  <td>Divyansha</td>        
  <td><form>
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!");if(strcmp("Divyansha",fgets($myfile))) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!"); if((strcmp("Divyansha",fgets($myfile))==-1)) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
</form></td>
  </tr>
<tr>
  <td>2.</td>
  <td>11102310</td>
  <td>Romil</td>        
  <td> <form>
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!");if(strcmp("Romil",fgets($myfile))) echo'checked="checked"';?> type="radio" name="attendance2" value="present">
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!");if((strcmp("Romil",fgets($myfile))==-1)) echo'checked="checked"';?> type="radio" name="attendance2" value="present">
</form></td>
</tr>
<tr>
  <td>3.</td>
  <td>11103566</td>
  <td>Shikher</td>      
  <td><form>
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!");if(strcmp("Shikher",fgets($myfile))) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!"); if((strcmp("Shikher",fgets($myfile))==-1)) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
</form></td>
  </tr>
<tr>
  <td>4.</td>
  <td>11102210</td>
  <td>Dhruv</td>        
  <td><form>
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!");if(strcmp("Dhruv",fgets($myfile))) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
<input <?php $myfile = fopen("attendance.txt", "r") or die("Unable to open file!"); if((strcmp("Dhruv",fgets($myfile))==-1)) echo'checked="checked"';?> type="radio" name="attendance1" value="present">
</form></td>
  </tr>

</table></div>
<div id="footer" style="background-color:#FFA500;clear:both;text-align:center;">
Jaypee Instiute Of Information Technlogy</div>
</div>
</body>
</html>

而"attend .txt"包含:

RomilDivyanshaShikher

你可以读一次文件,然后创建一个名称数组,并基于该数组使复选框为真或假

$names = array();
$handle = @fopen("/path/to/attendance.txt", "r");
if ($handle) {
    while (!feof($handle)) {
       $names[] = fgets($handle);
    }
    fclose($handle);
}

您也可以使用file('/path/to/attendance.txt')函数,它将返回与文件文件函数文档
中的一行对应的数组。在html中可以使用in_array

<input <?php if(in_array('Divyansha',$names)) echo'checked="checked"'; ?> type="radio" name="attendance1" value="present">