1

I was using a text file as a storage/database for some few datas for my website. I successfully outputed it in my web page by displaying each contents on its specified holder or element but somehow I want to customize it using another way to cater my needs.

I have this code for displaying the contents from a text file and place them according to there use.

<?php
$handle = @fopen("mydb.txt", "r");

while (!feof($handle)) // Loop til end of file.
    {
    $buffer = fgets($handle, 4096);
    // Read a line.
    list($a,$b,$c)=explode("|",$buffer);
    //Separate string by the means of |
}
$aa = $a;
$bb = $b;
$cc = $c;
?>


<div id="tabs-1">
    <form id="dbform" action="processor.php" method="GET" >
        <input type="hidden" name="login" value="emailsave" />
        <div id="error"></div>
        <table cellpadding="3" cellspacing="3" align="center" valign="top">
            <tr>
                <td valign="top">
                    <label>Senders email:</label>
                </td>
                <td>
                    <input class="dainput" type="text" name="mailsenders" value="<?php echo $aa;?>" />
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label>Subject:</label>
                </td>
                <td>
                    <input class="dainput" type="text" name="mailsubject" value="<?php echo $bb;?>" />
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <label>Message:</label>
                </td>
                <td>
                    <textarea style="height: 150px;" class="dainput" type="text" name="mailmsg" ><?php echo $cc;?></textarea>
                </td>
            </tr>
        </table>
        <button class="dabutton">Update</button>
    </form>
</div>

edit:

but now I want to use new line (\n) as a separator instead of "|" is there anyway to achieve that?

so the text file content should be like this

your-email@mail.com
Subscriber
hello

instead of this

your-email@mail.com | Subscriber | hello

Im open in any suggestions, recommendations and ideas, please guide me through. Thanks!

  • 2
    I know that I'm not answering your question, but I'm wondering. Is there any reason that you couldn't just use a real database or a pseudo-database, such as SQLite (http://php.net/manual/en/book.sqlite.php)? – wecsam Jul 27 '13 at 16:05
  • Not an exact answer to your question, but shows what is need to seek through a text file: [Getting one line in a huge file with PHP](http://stackoverflow.com/q/2794056/911182) Check out the first two answers. – Herbert Jul 27 '13 at 16:14
  • @wecsam: thats far heavy for me, I prefer using text file because I just think that im just handling a little data so mysql is out a bit in my head and regarding sqlite, I just dont know how to make it through sqlite and I dont have to enough time to switch to sqlite. – Julibear Police Jul 27 '13 at 16:15
  • 1
    I can understand your concerns as creating a database and learning the SQL commands to access it can be daunting and error prone. On the other hand, once you learn it, it can be quite a time saver. – Herbert Jul 27 '13 at 16:31
  • precisely, however, my whole concept stands into this one (text file database concept) so since im starting it then I just want to finish this as it is, besides, this gonna be a big learning for me and will help a lot for future reference! – Julibear Police Jul 27 '13 at 16:47
  • please see my updated question as im doing some other alternative method to cater my needs somehow. Thanks – Julibear Police Jul 27 '13 at 17:24

1 Answers1

0

Use fgets

$handle = @fopen(FILE)
$lines = array();
while (false != ($line = fgets($handle)) {
    $lines[] = $line;
}

That will read the entire file and put each line into $lines. If you have just 3 lines, call fgets 3 times.

ep0
  • 710
  • 5
  • 13
  • what do you mean "If you have just 3 lines, call fgets 3 times." I dont get this easily could you be more precise about that please? as I see your answers near hit my desired output. – Julibear Police Jul 27 '13 at 17:39
  • i have 7 lines in my textfile – Julibear Police Jul 27 '13 at 17:44
  • As per your example, you had three separate entries. If you have an indefinite number of lines, use `while`. – ep0 Jul 27 '13 at 17:48
  • so what the final code will be? I have 7 lines or atleast make it in 3 lines. This is my try, I replace the `$lines[] which inside the while condition with` `$lines[0] = $line;$lines[1] = $line;$lines[2] = $line;` as I think it represent the number of line within the array. – Julibear Police Jul 27 '13 at 18:03
  • I don't get the `7 lines` part. If you can have between 3 and 7 lines, then use `fgets` 7 times. For first 3 times, just read the line and then (from 4 to 7) check whether it returned `false` (that means end of file). To answer your question better, you should provide some data model. – ep0 Jul 27 '13 at 19:43