Xml -> php -> img

bonjour,

Je souhaite afficher des images venant d’un xml
voici le code :

 <?php
  $library = simplexml_load_file('test.xml');
	foreach ($library->shelf as $shelf) {
//      printf("<b> Shelf </b> %s\n", $shelf['id']);
	foreach ($shelf->book as $book) {

		 // printf("%s\n", $book->title);

         // printf("<b>Author: </b>%s\n", $book->author);
          print '<br>';
		  
		  echo '<div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/"'.printf("%s\n", $book->title).'".jpg" alt="" width="148" height="111" border="0"/></a> </div>';

      }
		echo '<div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/norm1.jpg" alt="" width="148" height="111" border="0"/></a> </div>';

  }


  ?> 
printf("<b>Author: </b>%s\n", $book->author);

fonctionne super mais je n’arrive pas à récupérer le nom pour afficher l’image. Je dois oublier un truc, merci de votre aide
Edité le 19/06/2008 à 11:54

J’ai pas trop compris le probleme exactement, tu as un message d’erreur ?
Sinon le printf en plien milieu d’un echo me fait un peu douté, ca aurais été sprintf encore.

Utilise la fonction sprintf :slight_smile:

Merci des reponses j’ai donc essayé de faire

<?php
  $library = simplexml_load_file('test.xml');
	foreach ($library->shelf as $shelf) {
//      printf("<b> Shelf </b> %s\n", $shelf['id']);
	foreach ($shelf->book as $book) {
		$plup = "%s\n";
		  printf($plup, $book->title);

         // printf("<b>Author: </b>%s\n", $book->author);
          print '<br>';
		  
		  echo '<div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/"'.sprintf($plup, $book->title).'".jpg" alt="" width="148" height="111" border="0"/></a> </div>';

      }
		echo '<div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/norm1.jpg" alt="" width="148" height="111" border="0"/></a> </div>';

  }
?>

avec fr.php.net… mais les images dans le second echo ne s’affichent pas correctement.

oulah… tu utilises mal la fonction sprintf, en tout cas pour ton contexte.

<?php
 $library = simplexml_load_file('test.xml');
	foreach ($library->shelf as $shelf) {
// printf("<b> Shelf </b> %s\n", $shelf['id']);
	foreach ($shelf->book as $book) {
		$plup = "%s"; // <-- sans \n ! 
		 printf($plup, $book->title);

 // printf("<b>Author: </b>%s\n", $book->author);
?><br><div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/<?php printf($plup, $book->title); ?>".jpg" alt="" width="148" height="111" border="0"/></a> </div><?php

 }
?><div id="min1"><a href="img/norm1.jpg" rel="lightbox"><img src="img/norm1.jpg" alt="" width="148" height="111" border="0"/></a> </div><?php

 }
?>

En gros: si tu fais %s\n tu affiches comme chemin img/xxx .jpg et ça foire.

Oula… mon niveau en php est pire que ce que je croyais.

Merci Sans-Nom :super:

Ouais… ou en html? En tout cas, elle ne sert à rien là, tel que tu t’en sers. Tu aussi vite fait de faire un echo $book->title.