Jump to content

writing to file problems


godsent

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/150321-writing-to-file-problems/
Share on other sites

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

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

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.