簡體   English   中英

為什么我的php頁面無法正常工作?

[英]Why on earth won't my php page work?

我的.php文件的當前狀態: http : //rich2233.comoj.com/file2.php

我有兩個問題:首先,頁面加載時,我的所有時間字段都被禁用(請參見上面的網站)。 其次,當我點擊Submit時,出現以下錯誤:在第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>';



  ?>

干得好:

只要確保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>';



  ?>

並且您缺少目錄分隔符:

public_htmltest.txt

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

嘗試這種方式:

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

:在第8行中為foreach()提供了無效的參數。

此行導致您的錯誤:

$lines = file( $filename , FILE_IGNORE_NEW_LINES );

您的$lines變量為null或不是數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM