Jump to content

XML, PHP and MySQL


NaniG

Recommended Posts

Hi to all,

 

        Here is my code to out put the and saved in xml file...

 

<?php

ob_start();

 

$conn=mysql_connect("localhost","root","");

$mysql_select_db("test",$conn);

 

$selqry=mysql_query("select * from table_name");

 

$doc = new DomDocument('1.0');

 

$root = $doc->createElement('userlist');

$root = $doc->appendChild($root);

 

while($row = mysql_fetch_assoc($selqry))

{

    $users= $doc->createElement('users');

    $users= $root->appendChild($users);

 

foreach($row as $fieldname => $fieldvalue)

{

$child = $doc->createElement($fieldname);

$child = $calls->appendChild($child);

 

$value = $doc->createTextNode($fieldvalue);

$value = $child->appendChild($value);

}

}

echo 'Wrote: ' . $doc->save("userslist.xml") . '  bytes';

?>

 

the values are stored in xml format as

<userlist>

      <users>

              <date>2011-09-27 22:47:55</date>

              <channel>abc/3024-b5f96be8</channel>

              <name>Test</name>

              <email>[email protected]</email>

    </users>

</userlist>

 

But i wanted to store blow mentioned as it is...

<userlist>

      <users>

              <date>2011-09-27 22:47:55</date>

              <channel>3024</channel>

              <name>Test</name>

              <email>[email protected]</email>

    </users>

</userlist>

 

Please help me out....!

 

Thanks and Regards....

 

 

 

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

He wants to extract 3024 out of abc/3024-b5f96be8

 

String functions if the data is as simple as your example. RegEx if it's not.

 

<?php 

$fieldname = 'channel';
$fieldvalue = 'abc/3024-b5f96be8';

if( $fieldname == 'channel' ) {
$slashPos = strpos( $fieldvalue,'/' ) + 1;
$fieldvalue = substr( $fieldvalue,
		$slashPos,
		strpos( $fieldvalue,'-') - $slashPos
	);
}

echo $fieldvalue;

?>

Link to comment
https://forums.phpfreaks.com/topic/248062-xml-php-and-mysql/#findComment-1273743
Share on other sites

Thanx xyph...

 

Solved my problem... and am having another issue...

 

depending upon the fieldvalue, i have to create a new attribute (which is not in the DB fields).

 

suppose if the fieldvalue is 3024, then i have to add new own child-attribute. ie input or output

 

and the out put would be like these....

 

<userlist>

      <users>

              <date>2011-09-27 22:47:55</date>

              <channel>3024</channel>

              <name>Test</name>

              <email>[email protected]</email>

            <io>I</io>----------------------------------------------------------- (Own child attribute)

    </users>

</userlist>

 

thanks in advance...

 

Link to comment
https://forums.phpfreaks.com/topic/248062-xml-php-and-mysql/#findComment-1274010
Share on other sites

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.