Results 1 to 5 of 5

Thread: select data from comma separated file

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default select data from comma separated file

    hi there
    do you know if it is possible to select data from a comma separated file?
    i need only name and message from this and show it in plain html or css
    number$sex$state$name$surname$zip$street$city$e-mail$ip$message$

    the csv file contains many lines

    thanks in advance!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's possible, but working with text is always harder than working with a database.

    What is the exact format of the file?

    The function explode() may be most useful.
    Explode first by line break, then by comma.
    Or use file() which automatically splits each line into an entry in an array then explode by comma.

    If you don't have clear indications of where each entry starts/ends (line breaks) in a DIFFERENT format than the commas separating each entry, it will be more difficult, though not impossible as long as it is consistently marked throughout the data.

    I recommend that you don't use CSVs-- figure out a way to extract that data then insert it into a database. That will be much faster to search through as well. It's possible to use CSVs, but it will be slow and always hard to work with. Using a database will give you the ability to search quickly, cross-reference, modify, etc., without writing custom functions.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you could try XML files as well, if you don't want to use a database (and have file write permissions). Because XML uses user-defined tags, you can save strings, variables, arrays, multi-dimensional arrays (and more) with a minimum of effort. Look into php's simpleXML functions.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are many ways to save data in text files, and XML is a good one, but the problem is that nothing compares to the speed and ease of use of a database.
    It's not so much that it's "hard" to get the data, but that a database is efficient and parsing a chunk of text (a while text file-- XML, CSV, etc) takes a long time.
    This is only an issue once you have a lot of data.
    For something simple like user accounts where you are just storing email, name, username, password and location, XML would be easy and run fast. If you are storing thousands of pieces of data, however, it will get very slow very quickly.

    One way to keep the speed up is to store things in separate files, one for each "item", whatever an item may be. So in the example above, you'd need a separate file for each user or that too would eventually become slow.

    The real question is about how you want to (quickly) access the information and then whether it makes sense to use a simpler method or to just setup a database which will work in all cases.


    By the way, CSVs are a great format for a very specific purpose: storing data for archives/backups. But then to use that info again it's better to convert to a database.
    And if it matters, MySQL and probably other databases have features for importing CSV data (or at least PHPMyAdmin and other software have this built in-- I guess it's not technically part of the database software).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    ^yes, parsing xml (or any text file) is significantly slower. Searching it (like you would search a database) would become ridiculously time-consuming as the file gets larger.

    But, if you don't want/can't have a database (for whatever reason), I'd recommend xml for flexibility and ease of use. You'll spend much less time coding to get things into/out of a "writable" format.

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
  •