How can I fill in the XML file after PHP obtains data from the database?

Source: Internet
Author: User
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
   #
   #
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.