godsent Posted March 20, 2009 Share Posted March 20, 2009 Hello all, im trying to dump data from SQL into xml file but im having some problems. Basicly i would create txt file like this: $file = "file.txt"; $text = "some text"; $fp = fopen ($file, "a+"); fwrite ($fp, $text); fclose ($fp); but this is little more complex. after i got information from SQL $query = "SELECT * FROM list"; $res = mysql_query($query); while($arr = mysql_fetch_row($res)) { i must put that in this format <ban nick="<?php echo $arr[0]; ?>" deleted="0"> <comment><?php echo $arr[1]; ?></comment> <lastchange> <date><?php echo $arr[2]; ?></date> <time></time> </lastchange> </ban> But i have no idea how i could save everything to file. Please help me if you know how Quote Link to comment https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/ Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 Just assign $text, from you example, to the xml... $query = "SELECT * FROM list"; $res = mysql_query($query) or die(mysql_error); while($arr = mysql_fetch_row($res)) { $text .= "\" deleted=\"0\">\n \n \n \n \n \n \n" } $file = "file.txt"; $fp = fopen ($file, "a+"); fwrite ($fp, $text); fclose ($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/#findComment-789450 Share on other sites More sharing options...
BloodyMind Posted March 20, 2009 Share Posted March 20, 2009 first of all better you can use xml functions or simple xml but you can also store the output of the foreach to a string and then write it to the file Quote Link to comment https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/#findComment-789542 Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 but you can also store the output of the foreach to a string and then write it to the file Is there an echo in here? Quote Link to comment https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/#findComment-789549 Share on other sites More sharing options...
dgoosens Posted March 20, 2009 Share Posted March 20, 2009 the easiest way would be to use XMLWriter that would give something like this: class MySQLDump { private $_rootEl = "sql"; private $_banEl = "ban"; private $_nickAttr = "nick"; private $_delAttr = "deleted"; private $_commentEl = "comment"; private $_lastChangeEl ="lastChange"; private $_dateEl = "date"; private $_timeEl = "time"; public function sqlResToXml($sqlRes, $targetFile) { $xmlWriter = new XMLWriter(); $xmlWriter->openUri($targetFile); // set indent to TRUE $xmlWriter->setIndent(true); $xmlWriter->setIndentString("\t"); // xml file header $xmlWriter->startDocument('1.0', 'utf-8'); // write a root element $xmlWriter->startElement($this->_rootEl); foreach ($sqlRes as $arr) { // start ban element $xmlWriter->startElement($this->_banEl); // nick attribute $xmlWriter->writeAttribute($this->_nickAttr, $arr[0]); // deleted attribute $xmlWriter->writeAttribute($this->_delAttr, '0'); // comment element $xmlWriter->writeElement($this->_commentEl, $arr[1]); // start lastChange element $xmlWriter->startElement($this->_lastChangeEl); // date element $xmlWriter->writeElement($this->_dateEl, $arr[2]); // time element $xmlWriter->writeElement($this->_timeEl, ''); // end LastChange element $xmlWriter->endElement(); // end ban element $xmlWriter->endElement(); } // end root element $xmlWriter->endElement(); } } hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/#findComment-789564 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.