You are here:  » Display no table line feeds or spaces, no formatting?


Display no table line feeds or spaces, no formatting?

Submitted by Alpha1tec on Thu, 2007-04-26 23:52 in

Hi, Please help, we ran the test demo from your website Option 2 - Parse a feed from an Internet URL: and the output display was perfect with line feeds and spaces, all formatted. We then used the generated output code and then planned to modify the code to make the links hyper open? When we installed your code on our server and ran the same generated code, there was no table, line feeds or spaces, no formatting?
Are there any special PHP settings required which could cause this? How do we also get the links to open? Our sample generated code below:

XSLT (XML->HTML transformation)
<?php
  require("MagicParser.php");
  function myRecordHandler($record)
  {
    // This is where you write your code to process each record, such as loading a database
    // Here we just display the record contents using PHP's internal print_r() function
    print_r($record);
  }
  MagicParser_parse("{snipped}","myRecordHandler","xml|DEALS/DEAL/");
?>

Example output jumble Array no longer formatted?
( [DEAL] => [HOTELID] => 1041829 [DESTINATION] => Abbadia San Salvatore [DEALTEXT] => hotel gambrinus [URL] => {snipped} [RATING] => 30 [PRICE] => £29.00 [BOOKSTART] => 17/04/2007 [BOOKEND] => 16/05/2007 [TRAVSTART] => 17/04/2007 [TRAVEND] => 31/05/2007 [IMAGEURL] => http://media.expedia.com/hotels/2000000/1050000/1041900/1041829/1041829_1_t.jpg ) Array ( [DEAL] => [HOTELID] => 1662178 [DESTINATION] => Aberystwyth [DEALTEXT] => The Conrah Hotel [URL] => {snipped} [RATING] => 40 [PRICE] => £115.00 [BOOKSTART]

Submitted by support on Fri, 2007-04-27 01:54

Hi,

The print_r() function as you have seen just displayed a dump of the record variables. If you wish to display the record in a table like the Magic Parser demo script, you would need to write code to generate the HTML table. It's easy to do - here's an example running on this server - only displaying the first record (by returning TRUE from myRecordHandler() as the feed contains quite a few hotel deals:

http://www.magicparser.com/examples/wwte4_demo1.php

Here's the source:

<?php
  header
("Content-Type: text/html;charset=utf-8");
  require(
"MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<table border='1'>";
    foreach(
$record as $key => $value)
    {
      print 
"<tr>";
      print 
"<th>".$key."</th>";
      print 
"<td>".$value."</td>";
      print 
"</tr>";
    }
    print 
"</table>";
    return 
TRUE;
  }
  
// replace wwte4.xml with your XML URL
  
$xml "wwte4.xml";
  
MagicParser_parse($xml,"myRecordHandler","xml|DEALS/DEAL/");
?>

Now, in "real life", you wouldn't normally just display all the fields in the feed straight into a table like this - as you say in your question you want to display links to the hotel etc. What this involves is writing to code to generate HTML that is specific to the $record and refers to values within the array to create the output you want. For example, to access the destination, you would use the following variable within myRecordHandler():

$record["DESTINATION"];

You would then build up the display you want using these variables, and creating the necessary HTML to display them. Here's another example using the samel XML running on this server:

http://www.magicparser.com/examples/wwte4_demo2.php

And the source code:

<?php
  header
("Content-Type: text/html;charset=utf-8");
  require(
"MagicParser.php");
  function 
myRecordHandler($record)
  {
    print 
"<table>";
    print 
"<tr>";
    print 
"<td width='100' valign='top'>";
    print 
"<img src='".$record["IMAGEURL"]."' />";
    print 
"</td>";
    print 
"<td>";
    print 
"<h4><a href='".$record["URL"]."'>".$record["DESTINATION"]."</a></h4>";
    print 
"<p>".$record["PRICE"]."</p>";
    print 
"<p>Booking From: ".$record["BOOKSTART"]." - ".$record["BOOKEND"]."</p>";
    print 
"<p>Travel From: ".$record["TRAVSTART"]." - ".$record["TRAVEND"]."</p>";
    print 
"<br /><br />";
    print 
"</td>";
    print 
"</tr>";
    print 
"</table>";
  }
  print 
"<h3>Hotel Deals</h3>";
  
// replace wwte4.xml with your XML URL
  
$xml "wwte4.xml";
  
MagicParser_parse($xml,"myRecordHandler","xml|DEALS/DEAL/");
?>

Notes:

You will also see the following line in the above example:

  header("Content-Type: text/html;charset=utf-8");

This is because the feed is encoded in the UTF-8 character set, so you need to make sure that the web page you are generating is in the same character set. This header does this by telling the browser which character set to use when rendering the HTML.

Finally, in the above examples note that I have saved the XML to a local file. You can of course replace $xml="..." with the URL to the original XML online as included in your post.

Hope this helps!
Cheers,
David.

Submitted by Alpha1tec on Fri, 2007-04-27 05:19

Thanks, this has been very helpful, great support.