0

i have need of show only the last occurence when the rows are the same serial number.

Those are the lines of my txt file:

ID| NAME | SERIAL
1;  John;  00001;
2;  Mike;  00002;
3;  John;  00001;


  // open file
  $file = fopen("Data.txt","r");

  // loop lines
  while(!feof($file)){
      $line =  fgets($file);
      $explode_line = explode(";",$line);
      $id = $explode_line[0];
      $serial = $explode_line[1];

      if ($serial == $_POST['serial'])) {
          echo $id . ' - ' . $serial;
      }
  }

  fclose($file);

Result must be:
3; John; 00001;

and not:
1; John; 00001;
3; John; 00001;

j0k
  • 22,600
  • 28
  • 79
  • 90
Jimmy
  • 119
  • 1
  • 2
  • 7

4 Answers4

0

The simplest way is to store the data in an array of rows; or, if you need only one row, a single tuple:

$file = fopen("Data.txt","r");

$result = false;

// loop lines
while(!feof($file))
{
  // Use trim here
  $line =  trim(fgets($file));

  $explode_line = explode(";",$line);
  $id = $explode_line[0];
  $serial = $explode_line[1];
  if ($serial == $_POST['serial']))
  {
      $result = array('id' => $id, 'serial' => $serial);
  }
}
fclose($file);

if ($result)
{
  echo $result['id'] . ' - ' . $result['serial'];
}
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • I must show only the last occurrence if there are same serial number. – Jimmy Nov 21 '12 at 11:36
  • And so it does. If there is a match, `$result` gets populated, and it only contains the last results. At the end of the loop, if something was found, `$result` evaluates to true and the output is printed. Try it. – LSerni Nov 21 '12 at 11:39
0

Just overwrite an $myId variable that is defined outside of the loop. This will always contain the last occurrance after the loop completes.

  $file = fopen("Data.txt","r");

  $myId = '';

  while(!feof($file)){
      $line =  fgets($file);
      $explode_line = explode(";",$line);
      $id = $explode_line[0];
      $serial = $explode_line[1];

      if ($serial == $_POST['serial'])) {
          $myId = $id;
      }
  }

  fclose($file);

  if($myId != '')
  {
      echo htmlspecialchars($myId). ' - ' . htmlspecialchars($_POST['serial']);
  }
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Try this/that is your variant with as little as possible modifications/:

 // open file
  $file = fopen("Data.txt","r");
 $str = '';     

  // loop lines
  while(!feof($file)){
      $line =  fgets($file);
      $explode_line = explode(";",$line);
      $id = $explode_line[0];
      $serial = $explode_line[1];

      if ($serial == $_POST['serial'])) {
          $str = $id . ' - ' . $serial;
      }
  }

  echo $str;

  fclose($file);
mlinuxgada
  • 580
  • 3
  • 7
0

You could try;

  // open file
  $file = fopen("Data.txt","r");

  // loop lines
  while(!feof($file)){
      $line =  fgets($file);
      $explode_line = explode(";",$line);
      $id = $explode_line[0];
      $serial = $explode_line[1];
      if ($serial == $_POST['serial'])) {
        $ret_id = $id;
        $ret_serial = $serial;
        }         
      }
   fclose($file);
   echo $ret_id . ' - ' . $ret_serial;
James Anderson
  • 27,109
  • 7
  • 50
  • 78