1
<?php
$busca = $_GET['genero'];
$xml_string = file_get_contents("livros.xml");
$xml_object = simplexml_load_string($xml_string);
for ($i=0; $i < count($xml_object->livro); $i++) {
for ($j=0; $j < count($xml_object->livro[$i]->genero->descricao); $j++) {
if($busca == $xml_object->livro[$i]->genero->descricao[$j]){
echo $xml_object->livro[$i]->titulo."<br>";
echo $xml_object->livro[$i]->genero->descricao."<br>";
echo $xml_object->livro[$i]->isbn."<br>";
echo $xml_object->livro[$i]->autor."<br>";
echo $xml_object->livro[$i]->publicacao."<br>";
echo fLocalMostraGenero($xml_object->livro[$i])."<br>";
}
}
$xml_object->livro[$i]->titulo;
}
function fLocalMostraGenero($livro){
for($i = 0; $i < count($livro->genero->descricao); $i++)
{
echo $livro->genero->descricao[$i];
}
}
I made this code to perform a gender search within an xml. A colleague told me to use the foreach instead of my for normal, but I did not understand very well how it works and how to make this change in my code. Can someone give me an explanation?
Take a look at this Soen question: https://stackoverflow.com/questions/4637617/how-to-use-foreach-with-php-xml-simplexml
– Andre Mesquita