0

I've had this issue with an XML File i've been trying to echo out using PHP Without making 1 long line of text.

I run this code first to create my XML page, of all the products in my database:

<?php $cfg = Array(); $cfg['db_host'] = '***'; $cfg['db_user'] = '***'; $cfg['db_password'] = '***'; $cfg['db_name'] = '***'; mysql_connect($cfg['db_host'], $cfg['db_user'], $cfg['db_password']); mysql_select_db($cfg['db_name']); unset($cfg); $result = mysql_query("SELECT * FROM Artikelen"); $xml = new SimpleXMLElement('<xml/>'); while($row = mysql_fetch_assoc($result)) { $draw = $xml->addChild('draw'); $draw->addChild('Product_Id',$row['Pr_Id']); $draw->addChild('Product',$row['Product']); $draw->addChild('Prijs',$row['Prijs']); } $fp = fopen("Artikelen.xml","wb"); fwrite($fp,$xml->asXML()); fclose($fp); ?> 

Then i run this code from another file to show the .XML:

<?php $xml=simplexml_load_file("Artikelen.xml"); print_r($xml); ?> 

What this shows is:

SimpleXMLElement Object ( [draw] => Array ( [0] => SimpleXMLElement Object ( [Product_Id] => 1 [Product] => Pizza Margherita [Prijs] => 7.00 ))) 

Times 11 in 1 long line, since i have 11 products. I've tried some things as:

<?php $xml=simplexml_load_file("Artikelen.xml"); echo $xml->Product_Id . "<br>"; echo $xml->Product . "<br>"; echo $xml->Prijs . "<br><br>"; ?> 

But this doesn't seem to work.

This is my first time working with XML files, so it might be a very simple solution.

Thanks in Advance!, If you seem to miss information please let me know.

3 Answers 3

1

If you want to stick with print_r(), just echo pre tags before and after.

eg:

<?php $xml=simplexml_load_file("Artikelen.xml"); echo '<pre>'; print_r($xml); echo '</pre>'; ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

This looks cool, is there also a way to remove the tags and only show the information, however this might look even better.
Not with print_r() - it's effectively a dump of the object. You'll need to loop through, and echo each attribute you want in the format you would like.
1

No need to re-invent the wheel. Echo file_get_contents(), as in the below solution.

https://stackoverflow.com/a/1199643/2257899

Edit:

If you look at your print_r() results, you'll see it contains an array. Try:

<?php $xml=simplexml_load_file("Artikelen.xml"); echo $xml["Product_Id"] . "<br>"; echo $xml["Product"] . "<br>"; echo $xml["Prijs"] . "<br><br>"; ?> 

3 Comments

Thanks, this at least got rid of all the tags, but is there an easy way to <br> after every line and 2 <br> after the price of each product?
Thanks! I will check it first thing tomorrow morning
This didn't work, i will accept your other answer since it improved alot.
0

You can display xml using asXml method:

$xml=simplexml_load_file("Artikelen.xml"); echo $xml->asXML(); 

1 Comment

Thanks, this at least got rid of all the tags, but is there an easy way to <br> after every line and 2 <br> after the price of each product?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.