Jump to content

MySQL --> PHP --> XML Question!


adover

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/131487-mysql-php-xml-question/
Share on other sites

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

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

 

 

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.