How do I make a logical condition by comparing lines from a text file using fopen in php?

Asked

Viewed 157 times

1

I have the code below where he opens the file material1.txt, read to the end and need to make a comparison of one line with another for example:

$material = fopen("c:/inetpub/wwwroot/material1.txt","r");

while(!feof($material)){
    $linha = fgets($material,4096);

    echo $linha;
}

fclose($material);

In the file I have lines that fields are delimited by "|"

Example:

 material | nivel | quantidade | tipo    |
 lápis    |   1   |      20    | escolar |
 mochila  |   2   |      2     | escolar |

The comparison would be at the level if the value of the next line is > that the previous one the level is child if the next line the value e < it is parent of another level if value is equal is the same level.

what would you like to do another column where the child level will receive the parent level material column for example:

| material_pai | material | level | quantity | | pencil | pencil | 1 | 20 | | pencil | backpack | 2 | 2 | | pencil | notebook | 2 | 5 | | case | case | 1 | 3 | | case | rubber | 2 | 10 | | rubber | pen | 3 | 20 | | case | brush | 2 | 10 |

  • Make a line explode using the delimiter | and scans the values using i Dice of the result array.

  • +1 This complexity exercises our brain

1 answer

0

In general your question is confused, compares and plays where, there is a logic hole, when I arrive at the last line I compare with who? it was not easier to group all levels and then compare?

Anyway I think for this case of yours, it’s best to use the function file PHP, because it will treat your file like a large array, each line being a position, so you would be able to go back and forth. A very simple example that I can give you is this below, as I said is strange this logic of yours.

Example #1 (try to follow your logic)

<?php
// Abre o arquivo e trasforma cada linha uma posição de uma matriz
$materiais = file("material1.txt");

// Percore todas as linhas do arquivo (ignorando a primeira linha)
// E agrupa todo mundo nos respectivos niveis
$grupos = array();  // Variavel vazia para montar os niveis
for ($l = 1; $l < count($materiais); $l++) {
 list($material, $nivel, $quantidade, $tipo) = explode("|", $materiais[$l]);

 // FIX: Verifica se existe proxima linha
 if (isset($materiais[($l + 1)])) {
  list($material2, $nivel2, $quantidade2, $tipo2) = explode("|", $materiais[($l + 1)]); // Lê a próxima linha

  if ($nivel2 > $nivel) {
   echo "[" . $material2 . "] é filho de [" . $material . "]\r\n";
  } else if ($nivel2 < $nivel) {
   echo "[" . $material2 . "] é pai [" . $material . "]\r\n";     
  } else {
   echo "[" . $material2 . "] é mesmo nivel de [" . $material . "]\r\n";
  }

  $l++; // Adianta uma linha já que li duas num laço apenas 
 } else {
  echo "[" . $material . "] não tem próxima comparação\r\n";
 }   
}
?>


To fix that hole like I said I would gather everyone into groups of levels like all level 1,2,3 and etc. into matrices, and then just filter from one level to the maximum level, for example if it goes up to the 5 I could ask from the 3 to the 5. Getting more or less like this the code.

Example #2 (by my logic)

<?php
// Abre o arquivo e trasforma cada linha uma posição de uma matriz
$materiais = file("material1.txt");

// Percore todas as linhas do arquivo (ignorando a primeira linha)
// E agrupa todo mundo nos respectivos niveis
$grupos = array();  // Variavel vazia para montar os niveis
for ($l = 1; $l < count($materiais); $l++) {
 list($material, $nivel, $quantidade, $tipo) = explode("|", $materiais[$l]); 
 $grupos[$nivel][] = array($material, $nivel, $quantidade, $tipo); 
}

// --------------------------------------------------------------
$nivel_inicio = 2; // Nivel minino a mostar (no exemplo 2)

// Inicia a filtragem atarves do nivel
$novo_grupo = array(); 
foreach ($grupos as $nivel => $valores) {
 if ($nivel_inicio <= $nivel) {
  $novo_grupo[$nivel][] = $valores;
 }
}

print_r($novo_grupo);
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.