Reputation: 185
Current state of my .php file: http://rich2233.comoj.com/file2.php
I have two issues: First, when the page loads, all my time fields are disabled (See site above). Second, when I hit submit I get the following error: invalid argument supplied for foreach() in on line 8.
Can anyone help me fix this?
My .txt looks like this:
8:00 am|Rich Jones
9:00 am|Available
......
5:00 pm|Available
Code:
<?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>';
?>
Upvotes: 0
Views: 153
Reputation: 628
Here you go:
Just be sure that test.txt is readable and writable...
<?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>';
?>
Upvotes: 1
Reputation: 100557
: invalid argument supplied for foreach() in on line 8.
This line is causing your error:
$lines = file( $filename , FILE_IGNORE_NEW_LINES );
Your $lines
variable is null or not an array.
Upvotes: 0
Reputation: 628
and you are missing a directory separator:
public_htmltest.txt
$filename = getcwd() . "test.txt";
try this way:
$filename = getcwd() . DIRECTORY_SEPARATOR . "test.txt";
Upvotes: 0