/    Sign up×
Community /Pin to ProfileBookmark

Need Help reading and display data from a txt file – PHP

Hello folks, I’m building a website for my dad’s company, so far doing good but now I’ve encountered some problems, so they are working with a software that saves certain data into a text file, its a service company that repairs electronics, so the data is : [B]worksheet code, customer code, device name, device serial number, manufacturer, price offer, gross price in total, repair status.[/B] And the software save that date into a text file in a format like this one:

[U][B]Example: [/B][/U]

654;13285;SDR-H80;E9IA10966;Panasonic;15000;14975;Complete

as u can see the software saves the data in that exact order as i mentioned, seperating them with a ;

( 654 is the worksheet code, 13285 is the customer code, SDR-H80 is the device name etc…. )

So what i need is a php code that will retrieve the right data and displaying it’s status from a form action.
So here’s my code:

<div id=”login” class=”boxed”>
<h3 class=”title”>Information about devices</h3>
<p class=”content”>You can find the worksheet ID and the customer code on your worksheet.</p>

<div class=”content”>

<form id=”form1″ method=”post” action=”process.php”>

<fieldset>
<label for=”inputtext1″>Worksheetcode:</label>
<input type=”text” name=”ID”>
<label for=”inputtext2″>Customer code:</label>
<input type=”text” name=”code”>
<input type=”submit” name=”Submit” value=”Submit”>
<input type=”reset” name=”reset” value=”Reset”>
</fieldset>
</form>
</div>

So as you can see it’s an HTML form, that refers to a .php, i need a php code that will search and display all the data related to a certain device from the textfile by giving the ” customer code ” and the ” worksheet code ” ?

Thank you guys in advance! ?

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 05.2016 — fgetcsv() will be your friend for reading the data in, being sure to use the optional parameter to tell it to use ';' as the field separator (instead of the default comma). So you'll use that in a loop (should be an example in that manual page), and it will give you an array of data for each row that you can output as desired within the loop.
Copy linkTweet thisAlerts:
@ginerjmMay 05.2016 — The first step for your entire process should be to write an input process to save all the csv files to a true database. Create a form to input the data, save it, and re-display it so that you have good data. From then on your work will be much simpler using a true data storage mechanism.

As for the code you showed - some things to know:

  • - the label element needs an id attribute on the input element to connect them. Where you say for=xxx needs to relate to an xxx on the corresponding input tag.


  • - to 'show' the data (from your future db) you should set value attributes containing a php var. Then you read the data, move it to those php var names and send the form.


  • [code=php]
    ...
    ...
    $query = "select ID, code from tablename where ID='$idvalue' ";
    $query_data = $pdo->query($query);
    if (!$query_data)
    { handle error condition}
    // Now assume we have one row
    $row = $query_data->fetch(PDO::FETCH_ASSOC);
    $id = $row['ID'];
    $code = $row['code'];
    ...
    ...
    ...
    $code=<<<heredocs
    <form id="form1" method="post" action="process.php">
    <fieldset>
    <label for="inputtext1">Worksheetcode:</label>
    <input type="text" name="ID" value='$id'>
    <label for="inputtext2">Customer code:</label>
    <input type="text" name="code" value='$code'>
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" name="reset" value="Reset">
    </fieldset>
    </form>
    heredocs;
    echo $code;
    [/code]


    Hope this helps. PS _ when you choose your database interface method, be sure to use one of the currently supported ones - mysqlI or PDO(!)
    Copy linkTweet thisAlerts:
    @dime1902authorMay 06.2016 — Oh Thank You guys for the quick replies, but it's not a .csv file, it's a .txt file, and the software that they are using cannot be modified so i gotta adjust the website to read that .txt and display it on a new page( process.php) . And sorry for being a noob I've never worked with a database before xD. I'll look into that in the future but right now, i gotta fix this problem as simple as possible. ?
    Copy linkTweet thisAlerts:
    @NogDogMay 06.2016 — Yes, it's not technically a CSV file, but fegetcsv() is smart enough to treat it as one, using ";" as the separator. It does not matter what the file suffix is: just that it's a plain text file and that there is a specific character being used as the field separator.
    Copy linkTweet thisAlerts:
    @dime1902authorMay 06.2016 — Oh, I see, I didn't know that, thank you NogDog ?
    Copy linkTweet thisAlerts:
    @ginerjmMay 06.2016 — Is the data from the txt/csv file going to be used/referenced more than once? Is all you are doing is grabbing the data once and displaying it nice and neat and that's it? Because if this data source is meant to provide you long-lasting "data", then you really need to do something like what I described earlier. If the existing data collection system is feeding you files that contain one or more records of business transactions do you really want to retain them as individual files forever?
    Copy linkTweet thisAlerts:
    @dime1902authorMay 06.2016 — Every day at the end of the day, the software creates a .TXT, which separates the data i need with ; s from left to right, i've put that txt into the folder which contains all the files of the website, and all i want is, when the user puts the worksheet code, and the customer id, in the html form i created then it'll display the right data from that .txt. for example: If some1 types 654 in the worksheet code box and 13285 in the customer id box then it'll display:

    SDR-H80

    E9IA10966

    Panasonic

    15000

    14975

    Complete

    like this but ofc i'd also like to add some text to make it look good like Status: , Manufacturer: ,

    you know, so i can format it later in css, and if the user types something that isn't in the txt then i'd like it to display an error message like: " There is no such data, please check if you typed the data correctly" Also it's in another language but the the first line of the txt contains the things i mentioned before worksheet code, customer code, device name, device serial number, manufacturer, price offer, gross price in total, repair status and the rest is just the data itself like for example 73;11867;;;;0;0;new or the previous example:

    654;13285;SDR-H80;E9IA10966;Panasonic;15000;14975;Complete

    and that txt file will be put into the folder every end of the day manually, so I'd like to hear your thoughts on this one ? I love this forum, i always learn something new ?
    Copy linkTweet thisAlerts:
    @ginerjmMay 06.2016 — I have no idea what you tried to tell us in your last, especially how the input you spoke of is related to the data you then display.

    Again - can you answer the question I posed in my last? What do you do with this txt file after you do what you described above? Toss it?
    Copy linkTweet thisAlerts:
    @dime1902authorMay 06.2016 — Oh yes sorry i forgot, and no i don't want to toss it away ofc , I'd like to make it work multiple times like not just one time, whenever the user types the right data then i want it to display. Sorry mate, I haven't done things like this before so mind me if i don't understand a few things, but you get the idea what i'm trying to do... I don't know honestly how to solve this problem, I just simply want my website to display the right data from that txt using an html form and ofc PHP.
    Copy linkTweet thisAlerts:
    @ginerjmMay 06.2016 — So you are trying to re-invent the wheel. There are thousands of real-life applications that generate data in formats that are better managed by porting them to a database. You have some idea of a person providing a couple of off-the-cuff pieces of data that will magically pull up the associated 'transaction' data from one out of a mass of individual files. I don't see how you make that connection from your previous post but apparently it exists.

    Think about this. Data is being generated by a process. That process sends you a set of data in a format that you rely on. You save it in a folder along with a multitude of other sets, each in their own little file. How long are you going to let all that data sit like that and try to make good use of it?

    You should develop a process that receives a file of data, reads the data a row at a time (I assume here that one row is one business record?), shows it on a nicely formatted screen form and allows you to add some codes or identifying criteria and then SAVE it to a db table. (Again - each record in that table represents one row from the text file.) Now you have a db that can respond rapidly to a request for data when given the necessary id(s). It could even prompt you with the id(s) and a short desc of the record so that you can pick one out easily. You're not going to do that with the original source files.

    Yes - I am stuck on this one point while you insist that all you want is a simple form to show this data off. If you really going to receive a txt file every day and want to store this data and manage it and review it and summarize it and call it up instantly, then you need to re-think the storage mechanism. No need to have the outside source alter its format. You simply have to transform it to a modern, manageable format that will facilitate all of your future processing needs.
    Copy linkTweet thisAlerts:
    @dime1902authorMay 06.2016 — This tutorial is the closest thing what i'm tying to do too bad it isn't in English lol i just need a php code for my case, i don't know anything about database and re-displaying stuff from a database. I just want this to make it work.
    Copy linkTweet thisAlerts:
    @ginerjmMay 06.2016 — ok - so you just HAVE to use the txt files as your database. Fine. And the users have a printed worksheet in front of them that they can use to pull up the data in one of those files byinputting the code that is printed out in front of them. But - you say that they can do this to check the status of the order. Does that mean a specific item could appear in successive txt files with updated info in the later ones? Plus - when a user enters the needed code how do you know which file it is in? Is the workcode part of the text file name? Or do they have to enter the filename and the workcode to get to a particular record inside that text file, assuming that there are multiple records in each text file.

    I am trying to help you here since you aren't ready for big-time coding it appears
    Copy linkTweet thisAlerts:
    @dime1902authorMay 07.2016 — Yes indeed my friend ? I'm not ready for big time coding yet , about the file: there is only one text file , and the text file's name is the same, ALWAYS, it'll be replaced ( override ) manually in the folder at every closing, and yes you are right, one line means one business record, and don't worry about the manually replacing stuff we have reasons for that, there is no need for any super complicated method, just simply I need a full PHP code to display the right data from the .txt not the whole thing, just the data what is needed ( which means that particular line ) and yes you are right they have these two " IDs " the worksheet code and the customer code printed out on paper in front of them to make sure they can see only their device's status. As I said, always the FIRST line contains that info: worksheet code; customer code; device name; device serial number; manufacturer; price offer; gross price in total; repair status

    separated with ; -s , the rest lines are each 1 line / business records, and i want them to be displayed when the user types in the correct customer and worksheet code in that form. Not over complicated, just simple.
    Copy linkTweet thisAlerts:
    @NogDogMay 07.2016 — Because I was bored:
    [code=php]
    <?php

    // create test file
    $testFile = tempnam('/tmp', 'test');
    file_put_contents(
    $testFile,
    "654;13285;SDR-H80;E9IA10966;Panasonic;15000;14975;Complete".PHP_EOL.
    "655;13286;SDR-H85;E9IA10964;Panasonic;15003;14978;Incomplete"
    );
    // make sure there's really something in there:
    echo "<pre>Test Filenn".file_get_contents($testFile)."</pre>";

    // now the actual reading/processing of the file
    $fh = fopen($testFile, 'r');
    while($row = fgetcsv($fh, null, ';')) {
    if(count($row)) {
    echo "<h3>Data row</h3>n<ul>n";
    foreach($row as $field) {
    echo "<li>$field</li>";
    }
    echo "</ul>n";
    }
    }
    fclose($fh);

    // clean up after ourselves:
    unlink($testFile);
    [/code]

    Where do I send my invoice?
    Copy linkTweet thisAlerts:
    @dime1902authorMay 07.2016 — Thanks NogDog, but it didn't work, ? the filename is WEBDATA.TXT, also I've watched the tutorial and I discovered some things how I should create the output, so I made a table on process.php and I want to display the data in the table.

    but still the function doesn't work. I made a test file for that purpose only to test it out, but I cannot make it work xD, I even tried converting to .TXT to UTF-8 encode cuz of the non english content. but still, doesn't work.
    Copy linkTweet thisAlerts:
    @NogDogMay 08.2016 — Thanks NogDog, but it didn't work, ? the filename is WEBDATA.TXT, also I've watched the tutorial and I discovered some things how I should create the output, so I made a table on process.php and I want to display the data in the table.

    but still the function doesn't work. I made a test file for that purpose only to test it out, but I cannot make it work xD, I even tried converting to .TXT to UTF-8 encode cuz of the non english content. but still, doesn't work.[/QUOTE]

    Well, I was not trying to write the full end product for you, just show you the basic mechanism of using fgetcsv() with a semicolon-separated-values file. Yes, you'll have to supply the actual file path, make sure your HTML is configured to output whatever character encoding it is using (and perhaps look into applying htmlentities() to the output).
    ×

    Success!

    Help @dime1902 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...