I have this code in PHP that takes information from a text file, this information that comes from this file, comes type in columns delimited by | As you can see I made an explode in | the result came from the entire column because the file information came like this:
Example:
| campo1 | nivel | campo3 | campo4 |
| material | 1 | componente | quantidade |
| material | 2 | componente2 | quantidade |
The result does not explode like this: Example:
0=> material material material... até a ultima linha
mesmo coisa para as outras colunas
1=> 1 2 2 2 1 3 3 ... até a ultima linha do arquivo texto.
No nível 1 ele é pai do 2 e o 2 é pai no 3 e assim sucessivamente.
That is, I need to get this table and play in the database as well with the column 2_pai:
| coluna1 | nível | coluna2_pai | coluna2 | coluna3 |
| material | 1 | componente1 | componente1 | tipo |
| material | 2 | componente1 | componente2 | tipo |
| material | 3 | componente2 | componente3 | tipo |
| material | 1 | componente1.1 | componente1.1| tipo |
| material | 2 | componente1.1 | componente1.2| tipo |
Explaining what I want to do in column 2_pai will receive the parent level components for each child level. Level 1 is the maximum level has no parent your child will always be level 2 below it.
Example:
nivel
1
2
2
2
1
2
So in this case the first level 1 will only be the parent of levels 2 to the next level 1 the same thing goes for the next levels 1 and level 2 to 3 and so on.
$material = fopen("c:/inetpub/wwwroot/material1.txt","r");
//$materiais = file("material1.txt");
while(!feof($material)){
$linha = fgets($material,1024);
$valor = explode('|',$linha);
$nivel = $valor[3];
$component = $valor[4];
$array_nivel[$nivel] = $component;
$nivel_pai = $nivel - 1;
//$array_nivel[0] = $valor[1];
//echo $valor[3]."\n";
echo "Nivel ".$nivel_pai." Pai: ".$array_nivel[$nivel]." Filho: ".$component."\n";
}
The part in $nivel_pai = $nivel - 1
I tried to use the logic to get the level value for example 2 and subtract by 1 to get the component in the previous level in case level 2 will have the component of 1.
The string would be the column itself in the case of the column level I would need to do an if of the values of level 1 2 and 3 only as they are in the same string I am having problem to make this condition,
if($nivel == 1){
$array_nivel[$nivel_pai] = $component;
}elseif($nivel == 2){
$array_nivel[$nivel_pai] = $component;
$nivel_pai = $nivel - 1;
}... e por ia vai
já tentei if($nivel <> 1 || $nivel == 2 ...){
$array_nivel[$nivel_pai] == $component;
$nivel_pai = $nivel - 1;
}
The idea is to compare these levels, and to get the component of level 1 and to put of the column 2_pai of level 2 the component of level 2 will already be put in column 2_pai of level 3 and so on so that these levels are interleaved
level 1
2
2
2
1
2
2
3 2
3 4
4
5
3 2
1
2
2
3 2