Loop between an existing loop SimpleXML Photo Tag

0

How do I convert this (which only brings me the first photo)

//corrige foto
$enderecofoto = $xml->imovel[$i]->fotos->foto;
$nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$enderecofoto);
$removenomeeextensao = str_replace($nomeeextensao, "", $enderecofoto);
$nomefoto = substr($nomeeextensao, 0 , (strrpos($nomeeextensao, ".")));
//remove antiga tag antes de adicionar a nova, para evitar remoção de ambas
unset($xml->imovel[$i]->fotos->foto);
$foto = $xml->imovel[$i]->fotos->addChild('foto');
$foto->addAttribute('path', $removenomeeextensao);
$foto->addAttribute('arquivo', $nomeeextensao);
$foto->addAttribute('titulo', $nomefoto);

In a loop?

My script is basically this, and this "conversion" would be there:

$xml=simplexml_load_file("feed.xml") or die("Error: Cannot create object");
for ($i = 0; $i < count($xml); $i++) {
@@@@AQUI@@@@
}
echo $xml->asXML();
$xml->asXML('xml.xml');

The Loop should result in:

<fotos>
<foto path="http://localhost:8090/images/" arquivo="1.jpg" titulo="1"/>
<foto path="http://localhost:8090/images/" arquivo="2.jpg" titulo="2"/>
<foto path="http://localhost:8090/images/" arquivo="3.jpg" titulo="3"/>
</fotos>

Instead of (what it is currently):

<fotos>
<foto path="http://localhost:8090/images/" arquivo="1.jpg" titulo="1"/>
</fotos>

What do you say?

Looking at the StackOverflow I found this :

foreach($xml->imovel[$i]->fotos->foto as $foto) { 
  if(isset($xml->imovel[$i]->fotos->foto)) {
    $fotos = array();
    foreach ($xml->imovel[$i]->fotos->foto as $foto) {
      $fotos[] = "$foto"; 
      // or you could use, I believe: $fotos[] = $foto[0] 
    }
  } 
  else $fotos = "";
  var_dump($fotos); 
  echo "<hr />"; 
}

But how do I make this foreach use my settings? For a new XML tag to receive the data as it was set in the config I made up there?

-------------- EDIT

    foreach($xml->imovel[$i]->fotos->foto as $foto) { 
      if(isset($xml->imovel[$i]->fotos->foto)) {
        $fotos = array();
        foreach ($xml->imovel[$i]->fotos->foto as $foto) {
          $fotos[] = "$foto"; 
          $nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$foto);
          $removenomeeextensao = str_replace($nomeeextensao, "", $foto);
          $nomefoto = substr($nomeeextensao, 0 , (strrpos($nomeeextensao, ".")));
          $fotoxml = $xml->imovel[$i]->fotos->addChild('foto');
          $fotoxml->addAttribute('path', $removenomeeextensao);
          $fotoxml->addAttribute('arquivo', $nomeeextensao);
          $fotoxml->addAttribute('titulo', $nomefoto);
        }
      } 
      else $fotos = "";
      var_dump($fotos); 
      echo "<hr />"

; 
}
    
asked by anonymous 25.08.2015 / 21:50

2 answers

0

I developed a webservice for the integration of the real estate of our real estate with the application beview I think you can understand what I'm doing and adapt in yours, it looks like this:

$result = mysql_query(sua query);

$registros = mysql_num_rows($result);
        while ($row = mysql_fetch_array($result)){

    $fotos = explode(',', $row['fotos']);
                    $ord = 2;
                    foreach ($fotos as $foto){
                    $xml .="<arquivo>\n";
                        $xml .="<titulo>Foto Galeria</titulo>\n";
                        $xml .="<url>http://*******.com/cpanel/fotos/$row[id_imovel]/$foto</url>\n";
                        $xml .="<tipo>Foto</tipo>\n";
                        $xml .="<ordem>".$ord++."</ordem>\n";
                        $xml .="<categoria>Foto do Empreendimento</categoria>\n";
                    $xml .="</arquivo>\n";
}

While finding photos, you'll see the link within the

    
25.08.2015 / 23:30
0
foreach($xml->imovel[$i]->fotos->foto as $foto) { 
    $fotos = array();
    $nomeeextensao = preg_replace("(^.*\/(.*)\$)", "$1$2",$foto);
    $removenomeeextensao = str_replace($nomeeextensao, "",$foto);
    $nomefoto = substr((string)$nomeeextensao, 0 , (strrpos((string)$nomeeextensao, ".")));
    $fotoxml = $xml->imovel[$i]->fotos->addChild('fotomudar');
    $fotoxml->addAttribute('path', (string)$removenomeeextensao);
    $fotoxml->addAttribute('arquivo', (string)$nomeeextensao);
    $fotoxml->addAttribute('titulo',(string) $nomefoto);
}
unset($xml->imovel[$i]->fotos->foto);

I was able to: D Now just use a str_replace via file_get_content and switch from photo to photo, since I had to give unset still in simplexml.

    
26.08.2015 / 21:02