A newbie asks how PHP fills in XML files after obtaining data from the database: I know the user ID and query the user's information based on the user id. the background SQL statement has been written, now, in the logic layer, we can fill in the data to XML through the holidays, and then use the previous JS code to get the data, please take a look at the logic that I wrote for me. I would like to ask you how to fill in the XML file after PHP obtains data from the database.
Requirements:
I know the user ID and query related information of the user based on the user id. the SQL statement in the background has been written. now, the data is filled into XML in the logic layer through holidays, then we can use the JS code in the previous section to obtain data. please take a look at the problems with the logic-layer code I have written,
PHP code
// Reference the database processing file require_once ".. /.. /DBHandle/DBUser. php "; $ lefNav =$ _ REQUEST [" lefNav "]; $ action =$ _ REQUEST [" action "]; if ($ lefNav =" userinfo ") {if (action = "init") {$ userId = $ _ REQUEST ['User _ id']; $ rs = DBUser: getUserInfoByID ($ userId ); if ($ rs) {// place the queried User information results in XML and transfer them to the previous section $ doc = new DOMDocument ("1.0", "UTF-8 "); $ doc-> appendChild ($ doc-> createElement ("xml"); $ userNode = $ doc-> createElement ("rs "); $ userId = $ doc-> createElement ("userId"); $ userIdTx = $ doc-> createTextNode ($ rs [0]); $ userId-> appendChild ($ userIdTx ); $ name = $ doc-> createElement ("name"); $ nameTx = $ doc-> createTextNode ($ rs [1]); $ name-> appendChild ($ nameTx ); $ rname = $ doc-> createElement ("rname"); $ rnameTx = $ doc-> createTextNode ($ rs [2]); $ rname-> appendChild ($ rnameTx ); $ gname = $ doc-> createElement ("gname"); $ gnameTx = $ doc-> createTextNode ($ rs [3]); $ gname-> appendChild ($ gnameTx ); $ userNode-> appendChild ($ userId); $ userNode-> appendChild ($ name); $ userNode-> appendChild ($ rname); $ userNode-> appendChild ($ gname ); $ doc-> documentElement-> appendChild ($ userNode); echo $ doc-> saveXML ();} else {echo "FAIL ";}}}
------ Solution --------------------
Mutual Conversion between PHP and XML encapsulation function: xmlparse. php
PHP code
Include (dirname (_ FILE __). "/xmlparse. php "); $ listtags = explode (" "," 2 name "); $ arr = array (1," name "=>" SOM "," admin ", 100, array ("name" => "TOM", "Lily"); $ contXML = dump_xml_config ($ arr, "xml", "UTF-8 "); // put the file // file_put_contents_safe ("/path/test. xml ", $ contXML," w "); echo $ contXML; # Source code #
#
# <0> 1
#
SOM
# <1> admin
<2> 100
# <3> #
TOM
# <0> Lily
#
#