adover Posted November 5, 2008 Share Posted November 5, 2008 Hi all, I've been doing a tutorial on generating an XML file from php file which dynamically gets the data from the mySQL database. The problem is, it seems to stop when it gets to the header of the xml document to be generated as it contains a ?> as if it's closing the php!! As you've probably noticed I'm relatively new to PHP and the like and have encroaching deadlines wich is why I've come here for help! I'm mainly a flash developer hence the need to pull it into XML! <?php header("Content-type:text/html"); $host= "localhost"; $user = "root"; $pass = "root"; $database = "mysql"; $linkID = mysql_connect($host,$user,$pass) or die ("No connection" .mysql_error); mysql_select_db($database, $linkID) or die ("No DB!".mysql_error); $query = "SELECT * FROM hfactor ORDER BY name DESC"; $resultID = mysql_query($query, $linkID) or die("NO DATA!"); $xml_output = "<?xml version=\"1.0\"?>\n"; $xml_output .= "<entries>\n" for($x=0;$x<mysql_num_rows($resultID);$x++){ $row = mysql_fetch_assoc($resultID); $xml_output .= "\t<user>\n"; $xml_output .= "\t\t<username>".$row['name']."</username>\n"; $xml_output .= "\t\t<school>".$row['school']."</school>\n"; $xml_output .= "\t\t<dept>".$row['dept']."</dept>\n"; $xml_output .= "\t\t<email>".$row['email']."</email>\n"; $xml_output .= "\t\t<q1>".$row['q1']."</q1>\n"; $xml_output .= "\t\t<q2>".$row['q2']."</q2>\n"; $xml_output .= "\t\t<q3>".$row['q3']."</q3>\n"; $xml_output .= "\t\t<q4>".$row['q4']."</q4>\n"; $xml_output .= "\t\t<q5>".$row['q5']."</q5>\n"; $xml_output .= "\t\t<q6>".$row['q6']."</q6>\n"; $xml_output .= "\t\t<q7>".$row['q7']."</q7>\n"; $xml_output .= "\t\t<q8>".$row['q8']."</q8>\n"; $xml_output .= "\t\t<q9>".$row['q9']."</q9>\n"; $xml_output .= "\t\t<q10>".$row['q10']."</q10>\n"; $xml_output .= "\t\t<q11>".$row['q11']."</q11>\n"; $xml_output .= "\t\t<q12>".$row['q12']."</q12>\n"; $xml_output .= "\t\t<q13>".$row['q13']."</q13>\n"; $xml_output .= "\t\t<q14>".$row['q14']."</q14>\n"; $xml_output .= "\t\t<q15>".$row['q15']."</q15>\n"; $xml_output .= "\t\t<q16>".$row['q16']."</q16>\n"; $xml_output .= "\t\t<q17>".$row['q17']."</q17>\n"; $xml_output .= "\t\t<q18>".$row['q18']."</q18>\n"; $xml_output .= "\t\t<q19>".$row['q19']."</q19>\n"; $xml_output .= "\t\t<q20>".$row['q20']."</q20>\n"; $xml_output .= "\t\t<q21>".$row['q21']."</q21>\n"; $xml_output .= "\t\t<q22>".$row['q22']."</q22>\n"; $xml_output .= "\t\t<q23>".$row['q23']."</q23>\n"; $xml_output .= "\t\t<q24>".$row['q24']."</q24>\n"; $xml_output .= "\t\t<q25>".$row['q25']."</q25>\n"; $xml_output .= "\t\t<q26>".$row['q26']."</q26>\n"; $xml_output .= "\t\t<q27>".$row['q27']."</q27>\n"; $xml_output .= "\t</user>/n"; } $xml_output .= "</entries>"; echo $xml_output; ?> As you can see there is a username, school, dept, email and then 27 question strings for outputting results of an online questionnaire which I need in XML!! Also on a side note, I need the XML file to be arranged differently, so that it will go like this: <Community> <school attribute="school name"> <dept attribute="dept name"> <user attribute="username"> <q1></q1> and so on </user> </dept> </school> </community> I know it's a big ask but if someone could point me in the right direction I'd owe them a debt of gratitude and I'll scour the forums to see if I can help anyone else! Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted November 5, 2008 Share Posted November 5, 2008 <?php echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'; There should be a single quote in front of <?xml I think the code tags are removing it for some reason. hope that helps Quote Link to comment Share on other sites More sharing options...
kmaid Posted November 5, 2008 Share Posted November 5, 2008 I am afraid i don't know how to answer your original question however you may find this library of use even if it is just looking at how they dealt with it. Its pretty sweet it turns Associateive arrays into XML and back again. It even works with 2D arrays http://www.phpclasses.org/browse/file/9370.html Quote Link to comment Share on other sites More sharing options...
adover Posted November 5, 2008 Author Share Posted November 5, 2008 <?php echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'; There should be a single quote in front of <?xml I think the code tags are removing it for some reason. hope that helps Hmm changing the quotes wont work either, it even shows up on here as closing the php at that point which I really don't understand! Is there a way of escaping the ?> ? Quote Link to comment Share on other sites More sharing options...
kmaid Posted November 5, 2008 Share Posted November 5, 2008 You normaly escape special chars with \ Quote Link to comment Share on other sites More sharing options...
adover Posted November 5, 2008 Author Share Posted November 5, 2008 makes no difference funnily enough, even if u remove the first line it just seems to jump to the second line!! can't work out what's going on here! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2008 Share Posted November 5, 2008 The posted code does not work because it produces a php syntax error. The following line is missing the semi-colon ; at the end of the statement - $xml_output .= "<entries>\n" Quote Link to comment Share on other sites More sharing options...
adover Posted November 5, 2008 Author Share Posted November 5, 2008 Sorry, just worked it out, was an extremely stupid mistake on my part!! lol!! But for the second part of the question, how would I go about creating the XML document as described? <Community> <school attribute="school name"> <dept attribute="dept name"> <user attribute="username"> <q1></q1> and so on </user> </dept> </school> </community> As I see it this would involve first placing all of the people in the same department with the same school under the schools branch, then under the departments branch etc.... not an easy feat i expect?! Cheers Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 5, 2008 Share Posted November 5, 2008 Edit: Answer to your question before you edited it - Your file is not being parsed by php. What is the file extension? Quote Link to comment Share on other sites More sharing options...
adover Posted November 6, 2008 Author Share Posted November 6, 2008 That's what I noticed! I had it in the root folder but was not accessing it through localhost but merely through the location on my pc... my bad! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.