Andreea Prodan Andreea Prodan - 5 months ago 46
SQL Question

generate an XML with PHP with data from mysql pdo

<?php

$xml=new DomDocument("1.0", "UTF-8");

session_start();
require("Connection.php");

$ses = $_SESSION['user'];

$Raport= $xml->createElement("Raport");
$Raport= $xml->appendChild($Raport);

$UltimaLuna= $xml->createElement("UltimaLuna");
$UltimaLuna=$Raport->appendChild($UltimaLuna);

$sql=$conn->prepare("SELECT Sum FROM Operations WHERE Type='Expense' AND ID_User = (SELECT ID_User FROM Users WHERE Email = '$ses') ");
$sql->execute();
$Suma = $sql->fetchAll(PDO::FETCH_ASSOC);

foreach ($Suma as $row)
$TotalS = $TotalS + $row["Sum"];

echo "$TotalS";

$sql = $conn->prepare("SELECT categoryName FROM Categories WHERE Type='Expense' AND ID_User = (SELECT ID_User FROM Users WHERE Email = '$ses')");
$sql->execute();
$result = $sql->fetchAll();
foreach ($result as $row) {
print '<option value="'.$row['categoryName'].'">'.$row['categoryName'].'</option>';
echo "<p>".$row['categoryName']."</p>";
$Categorie=$xml->createElement('"'.$row['categoryName'].'"');
$Categorie=$UltimaLuna->appendChild($Categorie);
$Suma=$xml->createElement("Suma","$100");
$Suma=$Categorie->appendChild($Suma);
}
print '</select>';

$Total=$xml->createElement("Total");
$Total=$UltimaLuna->appendChild($Total);
$Suma=$xml->createElement("Suma",'$TotalS');
$Suma=$Total->appendChild($Suma);

$Categorie=$xml->createElement( "Categorii");
$Categorie=$UltimaLuna->appendChild($Categorie);
$Suma=$xml->createElement("Suma",'$TotalS');
$Suma=$Categorie->appendChild($Suma);

$xml->FormatOutput=true;
$string_value=$xml->saveXML();
$xml->save("example.xml")

?>


I want to generate an XMLwith PHP... I have in MySQLCategories(CategoryName..) And I want to display a new child for each Category (Holiday, School etc).
In $TotalS I calculate SUM of Categories , but when I want to Append to Sum the value, it's not working ...
I would really apreciate your help!
Thanks in advance

Answer

Change

$Suma=$xml->createElement("Suma",'$TotalS');

To

$Suma=$xml->createElement("Suma",$TotalS); // or $Suma=$xml->createElement("Suma","$TotalS");

See difference : https://eval.in/592177

Read about Single quotes ' and double quotes "

http://stackoverflow.com/a/3446286/2815635