Jump to content

Help! Problem reading data from flatfile db, Notice: Undefined offset


Recommended Posts

This is the first time i use flat database

I am trying to read the file in my flatfile database, but i got this error "Notice: Undefined offset"

 

The Way I store in database

$time_id."|".$day."|" .$time. "|".$name."|".$email."|".$event."

so it will be like

2008-Jan-15_02:10:49PM|2008-01-19|6-7|Peter|Peter@peter.com|Test

 

 

However, when i read the data from database i got this ouput:

 

2008-Jan-15_03:41:56AM 2008-01-18 4-5 df dsfsadf 2008-Jan-15_02:10:49PM 2008-01-19 6-7 Peter Peter@peter.com Test

Notice: Undefined offset: 5 in /home/.joyful/mak/mak.ghaben.com/scheduler/test.php on line 122

 

Notice: Undefined offset: 4 in /home/.joyful/mak/mak.ghaben.com/scheduler/test.php on line 122

 

Notice: Undefined offset: 3 in /home/.joyful/mak/mak.ghaben.com/scheduler/test.php on line 122

 

Notice: Undefined offset: 2 in /home/.joyful/mak/mak.ghaben.com/scheduler/test.php on line 122

 

Notice: Undefined offset: 1 in /home/.joyful/mak/mak.ghaben.com/scheduler/test.php on line 122

 

 

Live action here

http://mak.ghaben.com/scheduler/test.php?action=read

 

Full source code is here

<?php
error_reporting(E_ALL);
//How to write file in flat file

$input_time = date("Y-M-d_h:i:sA");


//$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';

if  ($action == 'submit') {

$time_id = $_POST['time_id'];
$time = $_POST['time'];
$name = $_POST['name'];
$email = $_POST['email'];
$day = $_POST['day'];
$event = $_POST['event'];


$fp = fopen("database.txt","a");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $time_id."|".$day."|" .$time. "|".$name."|".$email."|".$event."\n");

fclose($fp);

}

?>



<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<form method="post" action="?action=submit">
<TABLE Width=100%>
<TR>
   <TD>SCHEDULE ID:<font color="#FF0000"><font size="-1"></font></font></TD>
   <TD><INPUT TYPE="hidden" NAME='time_id' VALUE="<? echo "$input_time"; ?>" size=40><? echo "$input_time"; ?></TD>
</TR>
<TR>
   <TD>In Charge Person:<font color="#FF0000"><font size="-1"></font></font></TD>
   <TD><INPUT  NAME='name' VALUE='' size=40></TD>
</TR>
<TR>
   <TD>In Charge Person Email Address:<font color="#FF0000"><font size="-1"></font></font></TD>
   <TD><INPUT  NAME='email' VALUE='' size=40></TD>
</TR>




<TR>
   <TD>Day:<font color="#FF0000"><font size="-1"></font></font></TD>
<TD><select name="day">

<?
$w = 0;
$x = 28;

while($w < $x) {
$date = mktime(0, 0, 0, date("m"), date("d")+"$w", date("y"));
echo '<option value='. date("Y-m-d", $date) . '>' . date("m/d/y", $date) . '</option>';	
$w++;

}
?> 

</select></TD>
</TR>

<TR>
   <TD>TIME:<font color="#FF0000"><font size="-1"></font></font></TD>
<TD><select name="time">

<?
$i = '0';
$k = '24';
// use it later $am = 'AM';
// use it later $pm = 'PM';

while($i < $k) {
$j = $i + 1;

echo '<option value=' . $i . '-' .$j.'>' . $i . ':00 - ' . $j . ':00 </option> ';
$i++;
}
?> 

</select></TD>
</TR>

<TR>
<TD>EVENT:</TD>
<TD><textarea cols="70" rows="25" name="event"></textarea></TD>
</TR>
<TR>
   <TD></TD><br><br>
   <TD><INPUT TYPE="submit" name="submit" value="Submit Schedule"></TD> 
</TR>
</TABLE>
</form>
</html>


<?

if ($action == 'read') {


$fp = fopen('database.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}

while (!feof($fp)) {
$line = fgets($fp, 1024); //use 2048 if very long lines
list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $line);
echo '
<tr>
<td>'.$field1.'</td>
<td>'.$field2.'</td>
<td>'.$field3.'</td>
<td>'.$field4.'</td>
<td>'.$field5.'</td>
<td>'.$field6.'</td>
</tr>';
$fp++;
}

fclose($fp);



} //end action read


?>

 

EDITED BY thorpe: We have


tags for a reason.

Well, you can test whether $line has anything in it - (it could just be the empty line at the bottom of the file) before you try and operate on it but you're still going to run into problems with multiple line comments in your textarea. They're going to be written to the file with all the line breaks preserved. Also what if someone types a '|' in the textarea? You're better off using some obscure delimiter like chr(254). Then again you're better off using mysql or something :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.