1
I have this code in PHP that takes information from a text file, this information that comes from this file, comes kind in columns delimited by | as you can see I did a explode in | the result came the whole column because the file information came like this:
Example:
| campo1 | nivel | campo3 | campo4 |
| material | 1 | componente | quantidade |
| material | 2 | componente2 | quantidade |
The result in the explosion comes out 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 take this table and play in the database so with the column2_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 the 2_parent column will receive the parent level components for each child level. Level 1 is the maximum level has no father your child will always be level 2 below him.
Example:
nivel
1
2
2
2
1
2
So in this case the first level 1 will only be parent of levels 2 up to the next level 1 the same thing serves for the next levels 1 and level 2 for the 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 of taking the level value for example 2 and subtract by 1 to catch the component in the previous level in the level 2 case will get the component of 1.
the string would be the column itself in the case of the level column I would need to make an if of the values of level 1 2 and 3 just as they are in the same string I’m having trouble making this condition, the 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;
}
it does not work the idea is to compare these levels, and take the component of level 1 and place of level 2 2_parent the component of level 2 will already be put in level 3 2_parent column and so on so that these levels are interspersed
level
1
2
2
2
1
2
2
3
2
3
4
4
5
3
2
1
2
2
3
2
I think it has some functions in php that does this: scanf
– Wallace Maxters
Hello, I think I got the logic. I didn’t get the column 2_pai of 1.1 and 1.2. Can you give more examples of the column please
– Tiago Gomes
What you want is a substring of a string ?
– Duque
It would not be simpler to put everything in an array of arrays and then get what you needed. So you are complicating things without being necessary.
– Jorge B.