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>test@test.com</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>test@test.com</email>

    </users>

</userlist>

 

Please help me out....!

 

Thanks and Regards....

 

 

 

Link to comment
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
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>test@test.com</email>

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

    </users>

</userlist>

 

thanks in advance...

 

Link to comment
Share on other sites

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.