0
<?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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

1

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 "

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

Community
  • 1
  • 1
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0
<?php
header("Content-type: text");

$xmlout="<xml version=\"1.0\" ?>\n";
$xmlout.="<Raport>\n";

    session_start();
    require("Connection.php");
$stmt=$conn->prepare("Select * FROM Categories");
$stmt->execute();

while($row=$stmt->fetch())
{

 $xmlout .="\t<RaportPeCategorii>\n";
 $xmlout .="\t\t<NumeCategorie>".$row['categoryName']."</NumeCategorie>\n";

  $xmlout .="\t\t<TipCategorie>".$row['Type']."</TipCategorie>\n";
   $xmlout .="\t<RaportPeCategorie>\n";

}

 $xmlout .="</Raport>\n";

echo $xmlout;

?>

Just tried generating the XML using your code, using some similar tables, this solution doesen't create an actual .XML file, but you can download the generated text and save it as a xml file. For more details about this you can view a step by step tutorial here: Tutorial

student0495
  • 171
  • 3
  • 15