SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Thread: txt based CMS

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    txt based CMS

    I have a simple CMS with content stored in txt files.

    Content TXT file looks like

    Code:
    23.4.2006, 22:41 [|] 1 [|] our clients [|] text texttext
    I read this file using a loop and splitting the array in single piece of data:
    PHP Code:
    $open "filename.txt";
    $filearr file($open);
     foreach(
    $filearr as $row) {
       list(
    $labdat$showit$name$text) = explode(" [|] ",$row);

    And then I can echo the variables $labdat, showit, $name, $text on page.

    Everything is great untill I fwrite the code like that on txt file

    <table cellspacing="1" cellpadding="1" width="200" border="1">
    <tbody>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </tbody>
    </table>

    And file then looks like that
    Code:
    23.4.2006, 22:41 [|] 1 [|] our clients [|] text texttext
    <table cellspacing="1" cellpadding="1" width="200" border="1">
        <tbody>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
    </table>
    Somehow this
    foreach($filearr as $row) {
    list($labdat, $showit, $name, $text) = explode(" [|] ",$row);
    }

    is going mad and not doing what I want him to do.
    There are no array members at all or they are mixwd or something with each others.

    Why is that happening?

  2. #2
    SitePoint Member argos's Avatar
    Join Date
    Apr 2006
    Location
    Estonia
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's doing so imo, bcoz you are using file(); and file command reads lines in array. So it all needs to be on 1 line.

    (If I was correct... )

    argos.

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your data for each "row" extends multiple lines but you're reading the file line by line (into an array). Use file_get_contents(file) to read it into a string and somehow mark each of your records' start or end so you can split it into an array yourself.

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Grand Rapids, MI
    Posts
    1,284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps SQLite would work better here?

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Server don't have a php5, so the SQLite is not ar solution.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As said before, just use another character sequence to denote end-of-record (e.g. " [] ") ...
    Code:
    23.4.2006, 22:41 [|] 1 [|] our clients [|] text texttext
    <table cellspacing="1" cellpadding="1" width="200" border="1">
        <tbody>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
    </table> [] 
    24.4.2006, 22:41 [|] 1 [|] our clients [|] Another record
    <table cellspacing="1" cellpadding="1" width="200" border="1">
        <tbody>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
    </table> []
    Use file_get_contents() to put the filedata into a string variable, then you can explode on " [] " to make an array of records, then explode within that array on " [|] " to get each data item.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it has only one record, not multiple.

    I found a sollution by using a base64_encode and decode function on $text string before inserting data in file and getting them back. Works like a charm

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If only one record then file_get_contents() would have done what you want anyway, so the only explode you need is on " [|] ".
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how to do that file_get_contents returns a string, not an array

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, my mistake.

    $filearr = file_get_contents($open);
    $filearr = array($filearr);
    foreach($filearr as $row) {
    list($labdat, $showit, $name, $teksts) = explode(" [|] ",$row);
    }

  11. #11
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $open "filename.txt";
    $filedata file_get_contents ($open);
    list(
    $labdat$showit$name$teksts) = explode(" [|] ",$filedata); 
    Isn't that all you need? $filedata is a string comprising the contents of the file. Then all you need to do is explode on " [|] " to get the info into the list variables.
    Ian Anderson
    www.siteguru.co.uk

  12. #12
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this on this file
    23.4.2006, 22:41 [|] 1 [|] our clients [|] text texttext
    <table cellspacing="1" cellpadding="1" width="200" border="1">
    <tbody>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </tbody>
    </table> []
    24.4.2006, 22:41 [|] 1 [|] our clients [|] Another record
    <table cellspacing="1" cellpadding="1" width="200" border="1">
    <tbody>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </tbody>
    </table>

  13. #13
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But you said the file contained only one record? Make your mind up! On what you posted this time then then simply use my previous suggestion.
    Ian Anderson
    www.siteguru.co.uk

  14. #14
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one record is
    23.4.2006, 22:41 [|] 1 [|] our clients [|]texttext

  15. #15
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So what is the significance of ...

    <table cellspacing="1" cellpadding="1" width="200" border="1">
    <tbody>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </tbody>
    </table>

    ... Why is it even in the file?
    Ian Anderson
    www.siteguru.co.uk

  16. #16
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is wraped below the 23.4.2006, 22:41 [|] 1 [|] our clients [|]texttext
    and looks like
    23.4.2006, 22:41 [|] 1 [|] our clients [|]texttext
    <table cellspacing="1" cellpadding="1" width="200" border="1">
    <tbody>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </tbody>
    </table>
    function file() reads file content line by line to array and the result is very strange

    file_get_contents() read all file content to string.

    I decided to use base64_encode function to stor it all in one line.

    There is no need for database for such small project.

  17. #17
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tipone
    it has only one record, not multiple.
    Quote Originally Posted by tipone
    try this on this file
    ...

    [|] Another record

    ...

    Clearly you're contradicting yourself.

  18. #18
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok forget about it. I just stripped out all /n and everything works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •