Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.