0

I am trying to search a line in a text file and then print the following three lines. For example, if the text file has

1413X
Peter
858-909-9999
123 Apple road

then my PHP file would take in an ID ("1413X") through a form, compare it to lines in the text file - essentially a mock database - and then echo the following three lines. Currently, it is echoing only the phone number (with the second half of the numbers wrong??). Thanks for your help.

<?php
    include 'SearchAddrForm.html';

    $file = fopen("addrbook.txt", "a+");
    $status = false;
    $data = '';


    if (isset($_POST['UserID']))
    {
        $iD = $_POST['UserID'];
        $contact = "";

        rewind($file);

        while(!feof($file))
        {
            if (fgets($file) == $iD)
            {
                $contact = fgets($file);
                $contact += fgets($file);
                $contact += fgets($file);
                break;
            }
        }

        echo $contact;
    }

    fclose($file);
?>
1
  • 2
    php string concatenation operator is . (dot) not +. Commented Feb 8, 2015 at 11:35

2 Answers 2

1

It is better to set some flag that you found id and some counter to count lines after it to achieve your aim.

<?php
include 'SearchAddrForm.html';

// $file = fopen("addrbook.txt", "a+");
$file = fopen("addrbook.txt", "r");

$status = false;
$data = '';


if (isset($_POST['UserID']))
{
    $iD = $_POST['UserID'];
    $contact = "";

    rewind($file);

    $found = false;
    $count = 1;
    while (($line = fgets($file)) !== FALSE)
    {
        if ($count == 3) // you read lines you needed after you found id
            break;

        if ($found == true)
        {
             $contact .= $line;
             $count++
        }

        if (trim($line) == $iD)
        {
            $found = true;
            $contact = $line;
        }
    }

    echo $contact;
}

fclose($file);
?>

This kind of example how you can achieve this. And as you see in comment you should use $contact .= value, not $contact += value. Also instead of reading you can take the whole file in array line by line using function file. And why are opening file for writing?

Sign up to request clarification or add additional context in comments.

1 Comment

Ah! The problem was the trim()! Thank you, this code would probably work as well.
1

What I did:

<?php

//input (string)
$file = "before\n1413X\nPeter\n858-909-9999\n123 Apple road\nafter";

//sorry for the name, couldn't find better
//we give 2 strings to the function: the text we search ($search) and the file ($string)
function returnNextThreeLines($search, $string) {

    //didn't do any check to see if the variables are not empty, strings, etc

    //turns the string into an array which contains each lines
    $array = explode("\n", $string);

    foreach ($array as $key => $value) {
        //if the text of the line is the one we search
        //and if the array contains 3 or more lines after the actual one
        if($value == $search AND count($array) >= $key + 3) {
            //we return an array containing the next 3 lines
            return [
                $array[$key + 1],
                $array[$key + 2],
                $array[$key + 3]
            ];
        }
    }

}

//we call the function and show its result
var_dump(returnNextThreeLines('1413X', $file));

2 Comments

The main reason my code was not working was because I did not trim() the line of the file when attempting to match it with the iD. Regardless, thank you for your help. I will definitely use this implementation from now on, as I did not know that you can transform the file into an array.
@PeterKuebler Happy to help. I used a string for input, but you can just replace this using file_get_contents which will return a string of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.