If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Feb 2016
    Member #

    Encapsulating and reading into an array from text file

    please Iím trying to make 2 classes, to encapsulate these text files, Customer.txt and Orders.txt. The customer one I need to read each line into a separate instance of an array for each line of text, how can I do this?
    Also, do I have to do something about the commaís in the text files?

    I know I need to use fopen, just not sure how to go about doing it.

    This is what Iíve tried so far.


    class readOrders{

    $array_of_lines = fopen("orders.txt", 'r') or die("Failed to create file");
    foreach($array_of_lines as $line) {
    $pieces_of_line = explode(',', $line);


    class readCustomers{
    $array_of_more_lines = fopen("customers.txt", 'r') or die("Failed to create file");
    foreach($array_of_more_lines as $line) {
    $pieces_of_line = explode(',', $line);


    Here are my text files


    1,4,0133360903,Building Java Programs,Computer Science
    2,6,0321836995,Mathematics All Around,Mathematics
    3,24,0321825721,Mathematics for Elementary Teachers with Activity Manual,Mathematics
    4,10,0133011208,Business Math,Business

    2,Leonie,Köhler,,University of Stuttgart,Theodor-Heuss-Straße 34,Stuttgart,,Germany,70174,+49 0711 2842222
    3,Bj√łrn,Hansen,,University of Oslo,Ullev√•lsveien 14,Oslo,,Norway,0171,+47 22 44 22 22
    4,François,Tremblay,,McGill University,1498 rue Bélanger,Montréal,QC,Canada,H2G 1A7,+1 (514) 721-4711

    ::: i really need an assistance.



  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Cottage Grove, Minnesota
    Member #
    723 times
    Is there a reason why you are not using MySQLi?

    For plain text file, use this ...

    $array_of_lines = file("orders.txt");
    foreach($array_of_lines as $line) {
    $pieces_of_line = explode(',', $line);

    Using commas for separators is OK unless someone's name or address has commas in it.
    A lot of times, pipes | are used as separators because they are uncommon to be used in text.
    That of course depends on where the file comes from (example an Excel file converted to CSV).

    MySQLi would do so much more, such as sorting, searching, joining, add, edit, delete, etc.

    Or maybe what you're doing this a homework assignment?

    Last edited by mlseim; Feb 19th, 2016 at 05:30 AM.

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 04:15 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: