Looping through xml issue

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
jdsflash
Forum Newbie
Posts: 5
Joined: Mon Nov 30, 2009 11:59 am

Looping through xml issue

Post by jdsflash »

http://www.flashmajic.com/Google/index22.pgp
Im getting 6 broken images on this page for some reason. Can you explain what im doing wrong?

source files
http://www.flashmajic.com/Google/index22.zip

Code: Select all

 
<?php
 
// Simple enough, the location of the XML file
$xml_file = "../Surveys.xml";
 
// These are both keys that we will use later.
$xml_headline_key = "*NEWS*STORY*HEADLINE";
$xml_description_key = "*NEWS*STORY*DESCRIPTION";
$xml_link_key = "*NEWS*STORY*LINK";
$xml_image_key = "*NEWS*STORY*IMAGE";
// An array for storing our information. An array is nice to use here
// because it allows us to parse the XML and then temporarily forget about it
// allowing use greater freedom to edit and maniplulate the output.
$story_array = array();
 
// A counter that will come into use later.
$counter = 0;
 
// A simple class that will make our life easier. We could use an 
// associative array as well, but I prefer to just write up the class. =)
class xml_story{
    var $headline, $description, $links, $image;
}
 
// Once again, this is what we want our parser to do when it reaches a start tag
function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}
 
// Same thing here as well. This tells the parser what to do when it trips over an end tag.
function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}
 
// When the parser hits the contents of the tags it will perform this function.
// This will all be explained word for word in the tutorial
function contents($parser, $data){
    global $current_tag, $xml_headline_key, $xml_description_key, $xml_link_key, $xml_image_key, $counter, $story_array;
    switch($current_tag){
        case $xml_headline_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->headline = $data;
            break;
        case $xml_description_key:
            $story_array[$counter]->description = $data;
            break;
        case $xml_link_key:
            $story_array[$counter]->links = $data;
            $counter++;
            break;
        case $xml_image_key:
            $story_array[$counter]->image = $data;
            $counter++;
            break;
    }
}
 
// Creates the parser
$xml_parser = xml_parser_create();
 
// Sets the element handlers for the start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");
 
// Sets the data handler, same as before...
xml_set_character_data_handler($xml_parser, "contents");
 
// Opens the file or gives an error message
$fp = fopen($xml_file, "r") or die("Could not open file");
 
// Reads the file or gives an error message
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
 
// This if statement is exactly the same as before. It parses the xml document
// according to the functions we have defined; and it returns an error message
// if the parsing fails
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}
 
// Frees up the memory 
xml_parser_free($xml_parser);
 
// Closes the file
fclose($fp);
 
?>
 
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center"><h2>Public Surveys</h2></div>
<table width="90%">
<tr>
<td valign="top">
<?php 
// A simple for loop that outputs our final data.
for($x=0;$x<count($story_array);$x++){  
 
echo "<h3><a href=".$story_array[$x]->links.">".$story_array[$x]->headline."</h3></a>";
 
echo "<h4>".$story_array[$x]->description."</h4>";
echo "<h4><img src='".$story_array[$x]->image."'/></h4>";
 
}
?>
</td>
<td valign="top"><script type="text/javascript"><!--
google_ad_client = "pub-7629688820642243";
/* 336x280, created 10/17/09 */
google_ad_slot = "7123381247";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></td>
</tr>
</table>
</body>
</html>
 
 
xml page

Code: Select all

 
<news>
<story>
 
<headline>Title</headline>
<description>Description</description>
<link>http://www.freesurveybuilder.com/quiz.php?objID=61119029102009&title=Free Survey Builder</link>
<image>http://www.flashmajic.com/Google/Soccer_News/screen.jpg</image>
</story>
<story>
 
<headline>Title</headline>
<description> Description </description>
<link>http://www.freesurveybuilder.com/quiz.php?objID=61119029102009&title=Free Survey Builder</link>
<image>http://www.flashmajic.com/Google/Soccer_News/screen.jpg</image>
</story>
</news>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Looping through xml issue

Post by AbraCadaver »

What does the URL in the img tag look like when you view the source of the page? I would assume maybe it has been converted to HTML entities.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply