How can I get the code below to output node names that I define? As of now, it creates the nodes based on column names of an SQL table. I want to define both the columns and the nodes. Also, how can I code it so that when someone inserts data into my table it appends that data to the inserted data? E.g.:
Input data: "davidjmorin"
Data inserted: "http://someurl.com/davidjmorin"
Here is my code for the original question:
<?
//header('Content-type: text/xml');
$link = mysql_connect('localhost','root','IhaveAlLthEanSwers2012!');
mysql_select_db('bb_links');
$sql = "Select * from `links`";
$run = mysql_query($sql, $link);
if( $run && mysql_num_rows( $run ) ) {
$doc = new DOMDocument( '1.0' );
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$root = $doc->createElement( 'data' );
$doc->appendChild( $root );
while( ( $fetch = mysql_fetch_assoc( $run ) )!== false ) {
$node = $doc->createElement( 'channel' );
$root->appendChild( $node );
foreach( $fetch as $key => $value ) {
createNodes( $key, $value, $doc, $node );
}
}
$doc->save("thelinks.xml");
}
//$node = "channel";
function createNodes( $key, $value, $doc, $node ) {
$key = $doc->createElement( $key );
$node->appendChild( $key );
$key->appendChild( $doc->createTextNode( $value ) );
}
?>
I’m not entirely sure what you’re asking in your first question, but for your second question (which really should be split up into a second question on here), I think what you’re asking is just basic string concatenation. For example,
Also, please don’t use
mysql_*functions for new code. They are no longer maintained and the community has begun the deprecation process (see the red box). Instead, you should learn about prepared statements and use either PDO or MySQLi. If you can’t decide which, this article will help you. If you care to learn, this is a good PDO tutorial.I’m serious. Don’t use those functions. At all.