• 0

Read from TXT File and Echo a Result


Question

Understandably I haven't programmed in a very long time, actually years.

I need a code that will allow me to fetch a certain string from a txt file on my server. I will probably need some pipe's to seperate different item codes apart.

Basically I need;

A code that will understand that there is going to be a text file with about 20 different number codes in it

A code that will be able to loopback a "No code found" if no string is returned

I'm creating a track a parcel ID feature on my website, and every item will have a special code, and a logistics officer will be adding details to the text file about where the item is.

The structure at the moment is;

			<form>
Tracking #:<input type="text" name="trackingid" /> <input type="submit" value="Track!" />
</form>

It would be ideal if underneath this, it will return the result.

The text file will look like

383281171 brisbane arrival <br>shipped out to home address; 9381256289 departed mail center <br>shipped to home;

I know it's not a great approach, I should be using SQL, etc, but this is the only option for now.

Can someone help?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Here's an example if you host the files on your server (and is accessible through it)

<?php

$lines = file("http://www.mywebsite.net/textfile.txt");

foreach($lines as $line)
{
    echo($line);
}
?> 

(from php.net)

  • Like 2
Link to comment
Share on other sites

  • 0

<?php
$search = $_GET['trackingid'];
$lines = file('tracking.txt');
foreach($lines as $line)
{
  if(strpos($line, $search) !== false)
    echo $line;
}
?>

Got it, thanks for your help ;)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.