Loop between an existing Simplexml Photo Tag loop

Asked

Viewed 91 times

0

How do I convert it (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 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 did upstairs?

--------------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 />"

; 
}

2 answers

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 got :D Now just use a str_replace via file_get_content and switch from photochange to photo, since I had to unset still in simplexml.

0

I developed a webservice for the integration of the real estate of our real estate with the beview application I think you can understand what I’m doing and adapt in yours, it was 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, it displays the link inside the

  • Very interesting, in your case you used a mysql query as an array. I will try to use this foreach idea and modify mine. Thanks a lot :D

  • It’s.... Unfortunately it’s not working out so well.... :(

  • @Diego. Which error returned to you?

  • Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes). Or Execution time.... 90 Seconds. I will edit the main topic with the code I used.

  • There is something related to your error at this link:(http://stackoverflow.com/questions/561066/fatal-error-allowed-memory-sizof-134217728-bytes-exhausted-codeigniter-xml). However, post the code you are running.

  • Already posted, is in EDIT in the main post. Take a look.

Show 1 more comment

Browser other questions tagged

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