用PHP生成带有mysql pdo数据的XML

时间:2016-06-20 13:24:05

标签: php mysql xml

<?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")

?>

我想用PHP生成一个XML ...我在MySQLCategories(CategoryName ..)中我想为每个类别(假日,学校等)显示一个新的孩子。 在$ TotalS中,我计算了类别的SUM,但是当我想要附加Sum值时,它不起作用...... 我真的很感激你的帮助! 提前致谢

2 个答案:

答案 0 :(得分:1)

更改

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

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

查看差异:https://eval.in/592177

了解单引号'和双引号"

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

答案 1 :(得分: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;

?>

尝试使用您的代码生成XML,使用类似的表,此解决方案不会创建实际的.XML文件,但您可以下载生成的文本并将其另存为xml文件。有关此内容的更多详细信息,您可以在此处查看分步教程:Tutorial