为什么我的php页面不起作用?

时间:2011-10-28 17:24:14

标签: php

我的.php文件的当前状态:http://rich2233.comoj.com/file2.php

我有两个问题:首先,当页面加载时,我的所有时间字段都被禁用(参见上面的网站)。其次,当我点击提交时,我收到以下错误:在第8行为foreach()提供了无效参数。

任何人都可以帮我解决这个问题吗?

 My .txt looks like this:
 8:00 am|Rich Jones
 9:00 am|Available
 ......
 5:00 pm|Available

代码:

  <?php

if (isset($_POST['submit'])) { 
    $find_name = $_POST['name'];
    $find_time = $_POST['time'];
    $filename = getcwd() . "test.txt";
    $lines = file( $filename , FILE_IGNORE_NEW_LINES );
foreach($lines as $key => $line)
{
        list($time, $name) = explode('|', $line);
        if($time == $find_time && $name == "Available")
        {
        $lines[$key] = $time."|".$find_name;
        file_put_contents( $filename , $lines );
        break;
     }
     }

  }
// Read the file into an array
$users = file("test.txt");
$hours_taken = array();

// Begin Table
echo "<table align = 'center' border='2' width='50%' cellspacing='0' 
    cellpadding='0'>";
echo "<caption> Sign/Up Sheet </caption>";
echo "<tr><td><b>Time</b></td><td><b>Name</b></td></tr>";

// Cycle through the array
foreach ($users as $user) {

    // Parse the line
    list($time, $name) = explode('|', $user);
array_push($hours_taken, $time);

    // Remove newline 
    $name = trim($name);

    // Output the data in a two column table
    echo "<tr><td>".$time."</td><td>".$name."</td></tr>";

}
echo "</table>";

// Assuming same order of rows in users.txt
$hours = array('8:00 am', '9:00 am', '10:00 am','11:00 am', '12:00 pm', '1:00 pm', 
    '2:00 pm', '3:00 pm', '4:00 pm', '5:00 pm'); 

$i = 1;
echo '<form method="post" action="">';
echo 'Name:<input type ="text" id="name" name="name" size="20" maxlength="40" />';
echo '<select name="time"><option selected>-- Select time --</option>';
foreach ($hours as $hour) {
if (in_array($hour, $hours_taken)) {
    echo '<option disabled=disabled>'. $hour .'</option>';
}
else {
    echo '<option value='. $i .'>'. $hour .'</option>';
}
$i++;
}
echo '<input type="submit" id="submit" name="submit" value="Sign Up!" />';
echo '</form>';



  ?>

3 个答案:

答案 0 :(得分:1)

你走了:

请确保test.txt是可读写的......

<?php


if (isset($_POST['submit']))
{ 
    $find_name = $_POST['name'];
    $find_time = urldecode($_POST['time']);
    $lines_handle = fopen("test.txt", "r");
    while (($buffer = fgets($lines_handle, 4096)) !== false)
    {
        list($time, $name) = explode('|', $buffer);
        $time = trim($time);
        $name = trim($name);
        if ($time == $find_time && $name == "Available")
        {
            $lines[] = $time."|".$find_name;
        }
        else
        {
            $lines[] = $time."|".$name;
        }
    }
    $lines_handle = fopen("test.txt", "w");
    foreach ($lines as $line)
    {
        fwrite($lines_handle, $line . "\n");    
    }
}
// Read the file into an array
$users_handle = fopen("test.txt", "r");
//$users = file("test.txt");
$hours_taken = array();

// Begin Table
echo "<table align = 'center' border='2' width='50%' cellspacing='0' 
    cellpadding='0'>";
echo "<caption> Sign/Up Sheet </caption>";
echo "<tr><td><b>Time</b></td><td><b>Name</b></td></tr>";

// Cycle through the array
while (($buffer = fgets($users_handle, 4096)) !== false)
{
    // Parse the line
    list($time, $name) = explode('|', $buffer);
    if (trim($name) != "Available")
    {
        array_push($hours_taken, $time);    
    }

    // Remove newline 
    $name = trim($name);

    // Output the data in a two column table
    echo "<tr><td>".$time."</td><td>".$name."</td></tr>";

}
echo "</table>";

// Assuming same order of rows in users.txt
$hours = array('8:00 am', '9:00 am', '10:00 am','11:00 am', '12:00 pm', '1:00 pm', 
    '2:00 pm', '3:00 pm', '4:00 pm', '5:00 pm'); 

$i = 1;
echo '<form method="post" action="">';
echo 'Name:<input type ="text" id="name" name="name" size="20" maxlength="40" />';
echo '<select name="time"><option selected>-- Select time --</option>';
foreach ($hours as $hour) {
if (in_array($hour, $hours_taken)) {
    echo '<option disabled=disabled>'. $hour .'</option>';
}
else {
    echo '<option value='. urlencode($hour) .'>'. $hour .'</option>';
}
$i++;
}
echo '<input type="submit" id="submit" name="submit" value="Sign Up!" />';
echo '</form>';



  ?>

答案 1 :(得分:0)

并且您缺少目录分隔符:

public_htmltest.txt

$filename = getcwd() . "test.txt";

尝试这种方式:

$filename = getcwd() . DIRECTORY_SEPARATOR . "test.txt";

答案 2 :(得分:0)

  

:为第8行的foreach()提供了无效参数。

此行导致您的错误:

$lines = file( $filename , FILE_IGNORE_NEW_LINES );

您的$lines变量为null或不是数组。