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

1
  • The correct Value it's displaying now, but It;s not appending myCategories into new Child of my root ..Thanks in advance! Commented Jun 20, 2016 at 13:47

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I already tried this, I tried with " and with ' ... It's still not working. It's not introduce the value $TotalS
IT's working now, with the correct value of $TotalS, but I still don't know how I can append to my "root" for every Category
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

1 Comment

Thank you a lot! IT's wooorking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.