1

Please help me on this as I am a newbie on PHP. I am reading from a file and loading the content in an array. Now I want to display only one record but the result is giving a blank page. When I display the whole array it works fine but I put the if statement to get a specific record, it doesn't work. Please see below code.

<?php
    $strreturn = "H123458"; 
    $customerRec = array(); 
    $destination_url ="./users/patient.txt";
    $myfile = fopen($destination_url, "r") or die("Unable to open file!");
    $c=0;
    while(!feof($myfile)) {             
        $customerRec[$c++] = fgets($myfile);        
    }
    fclose($myfile);

    for($i=0;$i<count($customerRec);$i=$i+6){
        if($customerRec[$i] == $strreturn){ 
            for($t=$i;$t<6+$i;$t++){
                echo "$customerRec[$t]"."<br/>";                
            }                           
        }           
    }   
?>

--patient.txt----

H123456
Hemede
1234567896541
2018-09-01
2018-09-30
CPT805
H123457
Mario
9876543214569
2018-09-02
2018-09-29
CPT280
H123458
Michael
3698521478965
2018-09-03
2018-09-28
PML209
H123521
Laurant
7532159852365
2018-09-04
2018-09-24
FRS965
H123954
Theos
9658741258632
2018-09-05
2018-09-19
OTH77
H.Petro
  • 11
  • 3
  • Assuming the array is populated correctly (which you claim it is), then your code looks fine - demo: https://eval.in/1056343 . I think it's outputting what you wanted it to (although you didn't say precisely what the correct output should be) but it certainly doesn't output nothing. A blank page could be indicative of your PHP script crashing - maybe switch on error reporting and/or check your logs to see what's happening. There's nothing in the code above which indicates a problem. – ADyson Sep 11 '18 at 13:11
  • 2
    The first part of this seems like an overly complicated version of `$customerRec = file('./users/patient.txt');`. See http://php.net/manual/function.file.php – Phil Sep 11 '18 at 13:11
  • 1
    Why are you adding +6 on `for($i=0;$i –  Sep 11 '18 at 13:13
  • 1
    @D.Schaller because that loop is skipping from one ID line (i.e. the lines starting with an H) to the next, not looking for each individual line. – ADyson Sep 11 '18 at 13:14
  • 1
    A blank PHP page is typically the result of undisplayed errors. See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Sep 11 '18 at 13:14
  • @ADyson Well... this seems more than legit. Sry for my braindead-moment :x –  Sep 11 '18 at 13:15
  • 2
    Your text has white space; use trim in your condition and it should work: `if(trim($customerRec[$i]) == $strreturn)` – lovelace Sep 11 '18 at 13:19
  • You may also be interested in PHP's `array_chunk()` and `array_filter()` functions. If your record structure is consistently grouped into 6 lines, you could make this much simpler – Phil Sep 11 '18 at 13:21

3 Answers3

1

Just replace your line if($customerRec[$i] == $strreturn){ with below line

if(trim($customerRec[$i]) == $strreturn){ 

Hope this helps

Krishna Joshi
  • 315
  • 1
  • 7
0

This is a very specific question but here goes with a solution that might give you some optimisation hints and it never hurts to explore the PHP manual

// Load the file contents into an array, trimming away any whitespace
$customerRec = array_map('trim', file(__DIR__ . '/users/patient.txt'));

// Split the array into groups of 6
$groups = array_chunk($customRec, 6);

// Find the groups with the first record equal to "H123458"
$search = "H123458";
$found = array_filter($groups, function($group) use ($search) {
    return reset($group) === $search;
});

// Loop over the found groups and display them
foreach ($found as $group) {
    echo implode('<br/>', $group);
}

References:

Phil
  • 157,677
  • 23
  • 242
  • 245
0

Your code is OK. But the array elements probably have an extra new-line character at the end.

That is why the comparison in the if-statement does not work. Try this line before the if-statement:

echo urlencode($customerRec[$i])."<br/>";

You will probably see something like this: H123458%0A

A solution for this would be to use a trim function around the fgets.

Mark1
  • 166
  • 1
  • 5