Jdsflash Posted November 15, 2009 Share Posted November 15, 2009 The line in red isnt working it contains a link to google.com http://www.google.com What am I doing wrong in the code that its not working? The rest of the code is fine. I just cant get it to link to google. <? // A simple for loop that outputs our final data. for($x=0;$x<count($story_array);$x++){ echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n"; echo "\t\t<br />\n";?> [color=red] <a href = "<? echo $story_array[$x]->links; ?>">[/color] <? echo "\t<i>" . $story_array[$x]->description . "</i>\n"; ?> </a> <? echo "\t\t<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/ Share on other sites More sharing options...
premiso Posted November 15, 2009 Share Posted November 15, 2009 <?php // A simple for loop that outputs our final data. for($x=0;$x<count($story_array);$x++){ echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n"; echo "\t\t<br />\n"; echo '<a href="' . $story_array[$x]->links . '">'; echo "\t<i>" . $story_array[$x]->description . "</i>\n"; echo "</a>"; echo "\t\t<br />\n"; } ?> You overcomplicated it. Keep it all in echos and it should work just fine for you. Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-957965 Share on other sites More sharing options...
Irresistable Posted November 15, 2009 Share Posted November 15, 2009 Use <?php instead of <?. Also, you don't have to end the PHP to do HTML. Someone beat me to it though.. Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-957967 Share on other sites More sharing options...
Jdsflash Posted November 15, 2009 Author Share Posted November 15, 2009 Thanks for your help. That did not accomplish what im trying to do though for some reason. Here is my full source. The link does not work in red. It links to some internal forbiden page on my server not to google. The xml link is here. Im pulling in xml parsing it and want to display it. http://www.freesurveybuilder.com/pages/xml_intermediate.xml http://www.freesurveybuilder.com/pages/xml_intermediate.php <?php // Simple enough, the location of the XML file $xml_file = "xml_intermediate.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"; // 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; } // 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, $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; $counter++; break; case $xml_link_key: $story_array[$counter]->links = $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"> <?php // A simple for loop that outputs our final data. for($x=0;$x<count($story_array);$x++){ echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n"; echo "\t\t<br />\n"; [color=red] echo '<a href="' . $story_array[$x]->links . '">';[/color] echo "\t<i>" . $story_array[$x]->links . "</i>\n"; echo "\t<i>" . $story_array[$x]->description . "</i>\n"; echo "</a>"; echo "\t\t<br />\n"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-957986 Share on other sites More sharing options...
chadayers Posted November 16, 2009 Share Posted November 16, 2009 I believed I solved your puzzle! lol demo of it working: http://code.chadayers.org/helper/xml_intermediate.php The echo was very messy so I cleaned it up a bit, but the real issue is: case $xml_description_key: $story_array[$counter]->description = $data; $counter++; <----------------- This does not belong here!!! break; case $xml_link_key: $story_array[$counter]->links = $data; $counter++; break; <?php // Simple enough, the location of the XML file $xml_file = "xml_intermediate.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"; // 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; } // 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, $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; } } // 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"> <?php // A simple for loop that outputs our final data. for($x=0;$x<count($story_array);$x++){ echo "<h2>".$story_array[$x]->headline."</h2>"; echo "<br>"; echo "<a href=".$story_array[$x]->links.">".$story_array[$x]->description."</a>"; echo "<br><br>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-958329 Share on other sites More sharing options...
Irresistable Posted November 16, 2009 Share Posted November 16, 2009 The headlines link to "google.com" Why google.. Wouldn't it be best to link to the full article.. Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-958487 Share on other sites More sharing options...
Jdsflash Posted November 16, 2009 Author Share Posted November 16, 2009 Thanks that worked perfectly. I used google as a placeholder only. This was jsut to learn from now ill adapt it to my real project. Thanks for your help. Its greatly apreciated. Quote Link to comment https://forums.phpfreaks.com/topic/181618-solved-simple-links-issue-newbe-here/#findComment-958689 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.