5

I am trying to do a address alert script for my local volunteer fire brigade and I'm stuck.

I have a txt delimited file called preplan.txt containing lines like this:

Line1: REF00001 | NAME1 | ALERTADDRESS1 | LINK2DOWNLOADPDFINFOONADDRESS1 | NOTESONADDRESS1

Line2: REF00002 | NAME2 | ALERTADDRESS2 | LINK2DOWNLOADPDFINFOONADDRESS2 | NOTESONADDRESS2

Line3: REF00003 | NAME3 | ALERTADDRESS3 | LINK2DOWNLOADPDFINFOONADDRESS3 | NOTESONADDRESS3

and so on.

I also have a string named $jobadd which is the address for a job...

What I need to do in php is if the job address is the same ($jobadd) as any of the Alert Addresses in the txt file then display relevant name, address, link and notes.. It needs to also ignore whether it is written in capital letters or not. Basically if $jobadd = a address in the txt file display that info...

I can only seem to echo the last line.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
AdamK
  • 53
  • 4
  • 3
    Don't worry about any lack of skill. I'm pretty certain most people here can't fight bushfires very well :-) You'll get the help you need here, that's what it's for. – paxdiablo Mar 31 '12 at 02:02
  • Haha, when I was small kid I thought throwing that red fire extinguishing tube directly into the fire would stop burning. :D – Silviu-Marian Mar 31 '12 at 02:23

3 Answers3

4

First, split the string to lines:

$lines = explode("\n", $data); // You may want "\r\n" or "\r" depending on the data

Then, split and trim those lines, too:

$data = array();

foreach($lines as $line) {
    $data[] = array_map('trim', explode('|', $line));
}

Finally, look for $jobadd in column #3, i.e. index #2, and print the data if found:

foreach($data as $item) {
    if(strtolower($item[2]) === strtolower($jobadd)) {
        // Found it!
        echo "Name: {$item[1]}, link: {$item[3]}, notes: {$item[4]}";
        break;
    }
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Awesome Thank you sooo much! I gave that a crack but I got a white screen. I think it's the way I am calling the txt file: [code] $file = "preplan.txt"; $file = fopen($file, "r") or exit("Unable to open file!"); [/code] I'm guessing thats wrong? Thanks again for taking the time to help me out! – AdamK Mar 31 '12 at 02:21
  • @iambriansreed: I think the OP can figure out how to read a file. – Ry- Mar 31 '12 at 03:01
  • @minitech Read his comment above mine. – iambriansreed Mar 31 '12 at 03:02
  • @iambriansreed: Ouch. I retract my previous statement. Just notify him instead of me, then ;) – Ry- Mar 31 '12 at 03:03
  • @AdamKamenek: See iambriansreed's comment above for how to read the file. – Ry- Mar 31 '12 at 03:04
  • @iambriansreed: Posting an answer that's 90% copied from someone else's answer is very bad form. Your edit was rejected for a reason. -1. – Ry- Mar 31 '12 at 03:06
  • I sent my (second) edit to you which you rejected initially then realized you were wrong and *retracted* the denial. Just trying to help the OP. – iambriansreed Mar 31 '12 at 03:08
  • @iambriansreed: The comment is enough. Really. (And I would reject it anyway, it's really not part of what I intended to say at all.) – Ry- Mar 31 '12 at 03:09
  • Which is why I completed the solution in my answer. – iambriansreed Mar 31 '12 at 03:09
  • @iambriansreed: Let me once again stress that it is not your answer. It's my answer, copied entirely without attribution, even if it may be obvious what you did. *The comment is enough.* Downvoting in return is pretty rude, too. – Ry- Mar 31 '12 at 03:10
  • After looking at your code I am not even sure if it would work. I think your last foreach should be in the second foreach and `$data` should be rewritten on each outside loop. My answer is updated as well. – iambriansreed Mar 31 '12 at 03:16
  • @iambriansreed: Actually, I just forgot a `[]` after `$data`. Thanks, updated now. – Ry- Mar 31 '12 at 03:18
  • ... Your incorrect code is why I down voted by the way. Why did you down vote mine? – iambriansreed Mar 31 '12 at 03:18
  • @iambriansreed: I told you, because you copied it entirely from mine (and note it was also incorrect). I'll remove it now since you've made a sufficient change. – Ry- Mar 31 '12 at 03:19
1

Updated

Stream lined a bit. Just enter the correct file path for $file and you should be good to go.

$data = file_get_contents($file);

$lines = array_filter(explode("\n", str_replace("\r","\n", $data)));

foreach($lines as $line) {

    $linedata = array_map('trim', explode('|', $line));

    if(strtolower($linedata[2]) == strtolower($jobadd)) {
        // Found it!
        echo "Name: {$linedata[1]}, link: {$linedata[3]}, notes: {$linedata[4]}";
        break;
    }
}
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
  • Thanks! That seems to work but its only looking at the last line of the txt file as far as I can tell. If I have the address up the top it doesn't alert. Put the address that is a match at the bottom and it displays. Any ideas? Again I thank you for your time! :) – AdamK Mar 31 '12 at 03:24
  • Double check the text file and the string you are looking for. If it works further down the text file it should by all accounts work at the top. Glad I could get you this far. – iambriansreed Mar 31 '12 at 03:30
0
<?php

    define('JOBADDR','ALERTADDRESS3');

    # get all lines
    $pl = file_get_contents('preplan.txt');
    $pl = explode("\n",$pl); 

    # cleanup
    foreach($pl as $k=>$p){ # goes through all the lines
        if(empty($p) || strpos($p,'|')===false || strtoupper($p)!==$p /* <- this checks if it is written in capital letters, adjust according to your needs */ )
            continue;

        $e = explode('|', $p); # those are the package elements (refid, task name, address, ... )
        if(empty($e) || empty($e[2])) # $e[2] = address, it's a 0-based array
            continue;

        if(JOBADDR !== trim($e[2])) # note we defined JOBADDR at the top
            continue;

        # "continue" skips the current line

        ?>


        <p>REF#<?=$e[0]; ?> </p>
        <p><b>Name: </b> <?=$e[1]; ?></p>
        <p><b>Location:</b> <a href="<?=$e[3]; ?>"><?=$e[2]; ?></a> </p>
        <p><b>Notes: </b> </p>
        <p style="text-indent:15px;"><?=empty($e[4]) ? '-' : nl2br($e[4]); ?></p>

        <hr />


        <?php
    }
Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • Again I thank you for taking the time to answer, I am not sure where I go wrong but I get a white screen with that too! I copied it to it's own php file and also the php I use to read the txt.. Nothing.. I'm really lost, way to much reading today! lol Again thank you! :) – AdamK Mar 31 '12 at 02:47
  • When you save this file, make sure there's a `preplan.txt` file in the same folder as this file, and it contains the three lines you provided in your initial post. If there are still problems, patch at the top of the script (right after ` – Silviu-Marian Mar 31 '12 at 02:53
  • Thanks for the reply, I am stumped, I copied and saved including the error reporting but still a white screen. How do I paste code here? I will post what I have.. – AdamK Mar 31 '12 at 03:01
  • Replace `$pl = file_get_contents('preplan.txt');` with `$pl = file_get_contents('preplan.txt'); var_dump($pl); echo "
    ";` and `$pl = explode("\n",$pl);` with `$pl = explode("\n",$pl); var_dump($pl); echo "
    `; you should get at least two horizontal gray lines and some odd stuff. It will let you know what's inside those vars and how the script reads your `preplan.txt`; if you still get nothing, white screen, it means that there's a problem with your webserver. First var_dump should get the contents of preplan.txt, second one gives the lines.
    – Silviu-Marian Mar 31 '12 at 13:26