Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/08/2008, 00:04
datauser
 
Fecha de Ingreso: septiembre-2007
Mensajes: 9
Antigüedad: 16 años, 7 meses
Puntos: 0
Mostrar solo un registro desde un archivo xml con php

Hola a todos tengo datos en un archivo xml el cual del cual quiero mostrar solo un registro con php dependiendo del id, algo así como se haría desde una base de datos mysql, gracias a quienes puedan ayudarme,

este es el XML

<?xml version='1.0' encoding='UTF-8'?>
<library>
<book id="1">
<title>Fahrenheit 451</title>
<author>R. Bradbury</author>
<publisher>Del Rey</publisher>
</book>
<book id="2">
<title>The Silmarillion</title>
<author>J.R.R. Tolkien</author>
<publisher>G. Allen Unwin</publisher>
</book>
<book id="3">
<title>1984</title>
<author>G. Orwell</author>
<publisher>Signet</publisher>
</book>
<book id="4">
<title>Frankenstein</title>
<author>M. Shelley</author>
<publisher>Bedford</publisher>
</book>
<book id="5">
<title>The Moon Is a Harsh Mistress</title>
<author>R. A. Heinlein</author>
<publisher>Orb</publisher>
</book>
</library>


para mostrar todos los registros desde el archivo XML seria asi.
<?php
$library = simplexml_load_file( 'data.xml' );
echo '<table>';
echo '<tr>';
echo '<th>Title</th><th>Author</th><th>Publisher</th>';
echo '</tr>';
foreach( $library->book as $book ) {
echo '<tr>';
echo '<td>' . $book->title . '</td>';
echo '<td>' . $book->author . '</td>';
echo '<td>' . $book->publisher . '</td>';
echo '</tr>';
}
echo '</table>';

?>

ahora bien como haría para mostrar solo un registro.


desde una base datos mysql seria asi.

<?php
include 'config_db.php'
$id = 1;
$result = mysql_query("SELECT * FROM data WHERE id = $id");
$row = mysql_fetch_assoc($result);
echo $row["title"];
echo $row["author"];
echo $row["publisher"];
?>