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

时间:2014-05-12 16:28:35

标签: php html

我已经制作了一个php文件,其中我通过阅读“attendance.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>

而“attendance.txt”包含:

Romil Divyansha Shikher

1 个答案:

答案 0 :(得分:0)

您可以读取一次文件并创建一个名称数组,并以该数组为基础使复选框为true或false

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

您还可以使用file('/path/to/attendance.txt')函数返回数组,该数组对应于文件中的一行 File Function Documentation
现在在您的HTML中,您可以使用in_array

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