help with PHP reading XML document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rydog65
Forum Newbie
Posts: 1
Joined: Wed Mar 26, 2008 10:47 am

help with PHP reading XML document

Post by rydog65 »

Hello all, thanks in advance for any help!

I am having trouble with a PHP script I have been piecing together for a real estate inventory management system. I have an XML document that is created from a form the user submits. That all works fine. Then I have a PHP page that displays the XML in an HTML table, one listing per row. It actually all works fine and looks good, but the problem is that it adds an extra entry at the beginning of the HTML table (it seems it runs through the script with no values first for some reason). I can't seem to find in my code where or why it adds this blank entry. It adds all other existing entries correctly.

Below is the PHP code:

Code: Select all

 
<?php
function start_tag($parser, $name, $attrs){
    global $table_string;
    $table_string .= "<tr>
        <td width=\"100\" align=left bgcolor=#e7eee8 height=\"120px\" valign=\"middle\">
            <span class=\"listingpadding03\"><img src=\"../images/thumbs/$attrs[title].gif\" width=\"100\" height=\"100\" align=\"middle\" /></span>
        </td>
        <td width=\"90\" align=\"center\" bgcolor=#e7eee8 height=\"120px\">
            <span class=\"listingpadding03\">$attrs[artist]</span>
        </td>
        <td width=\"170\" align=\"center\" bgcolor=#e7eee8 height=\"120px\">
            $attrs[path]
        </td>
        <td width=\"120px\" align=\"center\" bgcolor=#e7eee8 height=\"120px\">
            <span class=\"listingpadding03\">$attrs[squarefootage]</span>
        </td>
        <td width=\"120px\" align=\"center\" bgcolor=#e7eee8 height=\"120px\">
            <span class=\"listingpadding03\">&nbsp;&nbsp;&nbsp;&nbsp; $attrs[bedsbaths]</span>
        </td>
        <td width=\"90px\" align=\"right\" bgcolor=#e7eee8 height=\"120px\">
            <span class=\"listingpadding03\">$attrs[price] &nbsp;&nbsp;</span>
        </td>
    </tr>
    <tr>
        <td colspan=\"6\" height=\"7\" bgcolor=\"#ffffff\"></td>
    </tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#e7eee8\" width=\"816\" class=\"phptablefont\">
        <tr><td colspan=\"6\" bgcolor=\"#f6f6f6\"><img src=\"../images/list-fo.gif\" alt=\"Buffington Available Homes\" width=\"750\" height=\"22\" /></td></tr><tr><td colspan=\"6\"><img src=\"../images/list-head.gif\" alt=\"Buffington Available Homes\" width=\"816\" height=\"22\" /></td></tr>";
xml_parse($parser, file_get_contents("playlist.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
?> 
 
Below is the simple XML document that it reads:

Code: Select all

 
<songs><song title="mckinleya" artist="testing" path="one" squarefootage="two" bedsbaths="three" price="four" /><song title="mckinleya" artist="testing" path="one" squarefootage="two" bedsbaths="three" price="four" /></songs>
 
So for the above XML doc, it currently creates three entries (one blank one, and the two that are there). Also, please ignore the names of the attributes and tags in the XML...I am borrowing from old code, these are not songs, they are listings for homes.

Thanks again -

Ryan
Post Reply