This file is supposed to output a series of concatenated strings like the following:
"id number" "name of the fruit" "color of the fruit"
But when I test this out it just gives me a blank screen unless I use var_dump() to echo out the information. This information comes from a database and is assembled in a file called fruitxml.php. That's the file where my $contents variable comes from.
Even the echo $xml doesn't do anything. That should at least work. But it doesn't. What am I doing wrong which is giving me a blank screen?
   <?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>
When I use var_dump(), like I said, I get all my information but in JSON arrays and not in strings as I wanted.
 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"
    }
  }
}
And here is the file with the XML that takes the info from the database.
<?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");
?>
 
     
    