Diese Datei arbeitet zur Ausgabe soll eine Reihe von verketteten Zeichenketten wie folgt aus:file_get_contents XML foreach-Schleife nicht
"id number" "name of the fruit" "color of the fruit"
Aber wenn ich dies testen es gibt mir nur einen leeren Bildschirm, wenn ich var_dump()
verwenden, um echo die Informationen aus. Diese Informationen stammen aus einer Datenbank und werden in einer Datei namens fruitxml.php
zusammengestellt. Das ist die Datei, aus der my $contents
Variable stammt.
Auch die echo $xml
macht nichts. Das sollte zumindest funktionieren. Aber das tut es nicht. Was mache ich falsch, was mir einen leeren Bildschirm gibt?
<?php
$contents = file_get_contents("http://localhost:8888/SSL/Week3/Activity%203-3/fruitxml.php");
$xml = new SimpleXMLElement($contents);
echo $xml;
foreach($xml->fruits as $fruit) {
echo $fruit->fruitid . $fruit->fruitcolor . $fruit->fruitname . '.<br />';
} ?>
<pre><?php var_dump($xml) ?></pre>
Wenn ich var_dump()
verwenden, wie ich schon sagte, ich alle meine Daten aber in JSON-Arrays und nicht in Strings als ich wollte.
object(SimpleXMLElement)#1 (3) {
["fruitid"]=>
array(5) {
[0]=>
object(SimpleXMLElement)#3 (1) {
["id"]=>
string(1) "1"
}
[1]=>
object(SimpleXMLElement)#5 (1) {
["id"]=>
string(1) "4"
}
[2]=>
object(SimpleXMLElement)#8 (1) {
["id"]=>
string(1) "6"
}
[3]=>
object(SimpleXMLElement)#11 (1) {
["id"]=>
string(1) "8"
}
[4]=>
object(SimpleXMLElement)#14 (1) {
["id"]=>
string(1) "9"
}
}
["fruitname"]=>
array(5) {
[0]=>
object(SimpleXMLElement)#2 (1) {
["name"]=>
string(6) "Orange"
}
[1]=>
object(SimpleXMLElement)#6 (1) {
["name"]=>
string(6) "Grapes"
}
[2]=>
object(SimpleXMLElement)#9 (1) {
["name"]=>
string(5) "Apple"
}
[3]=>
object(SimpleXMLElement)#12 (1) {
["name"]=>
string(10) "Grapefruit"
}
[4]=>
object(SimpleXMLElement)#15 (1) {
["name"]=>
string(4) "Lime"
}
}
["fruitcolor"]=>
array(5) {
[0]=>
object(SimpleXMLElement)#4 (1) {
["color"]=>
string(6) "Orange"
}
[1]=>
object(SimpleXMLElement)#7 (1) {
["color"]=>
string(6) "Purple"
}
[2]=>
object(SimpleXMLElement)#10 (1) {
["color"]=>
string(3) "Red"
}
[3]=>
object(SimpleXMLElement)#13 (1) {
["color"]=>
string(14) "Pinkish Orange"
}
[4]=>
object(SimpleXMLElement)#16 (1) {
["color"]=>
string(5) "Green"
}
}
}
Und hier ist die Datei mit dem XML, die die Informationen aus der Datenbank übernimmt.
<?php
$dbh = new PDO("mysql:host=localhost;port=8889;dbname=ssl", "root", "root");
$sth = $dbh->prepare('SELECT fruitid, fruitname, fruitcolor FROM fruitapp');
$sth->execute();
$result = $sth->fetchall();
header("Content-type: application/xml");
$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlfile .= "<fruits>";
foreach($result as $user) {
$xmlfile .= '<fruitid>';
$xmlfile .= "<id>" . $user['fruitid'] . "</id>";
$xmlfile .= '</fruitid>';
$xmlfile .= '<fruitname>';
$xmlfile .= "<name>" . $user['fruitname'] . "</name>";
$xmlfile .= '</fruitname>';
$xmlfile .= '<fruitcolor>';
$xmlfile .= "<color>" . $user['fruitcolor'] . "</color>";
$xmlfile .= '</fruitcolor>';
};
$xmlfile .= "</fruits>";
echo $xmlfile;
$dom = new DOMDocument("1.0");
$dom->loadXML($xmlfile);
$dom->save("myfruitxml.xml");
?>
Ein * leerer Bildschirm * bedeutet, dass etwas kaputt ist. Fügen Sie diese Zeilen hinzu 'ini_set (' display_errors ', 1); error_reporting (E_ALL); 'ganz oben in Ihrem PHP-Skript und sehen, ob es einen Fehler ergibt oder nicht. –
Versuchte es. Keine Fehler. Nur ein leerer Bildschirm. –
Bitte zeigen Sie uns die Ausgabe von var_dump ($ xml) – NineBerry