Hello all,
I'm working on an entry page where I would like to show some of the
current headlines from technology websites. I've been following and
pretty much understand the article on devshed
http://www.devshed.com/Server_Side/PHP/PHPRDF/page1.html Plugging RDF
content into your Website. From what I gather from the article is that
he uses a foreach loop to display the Title, link and description which
could be anywhere from 5 to 15 links. I only have room on my page for 3
and then I plan on putting a more link taking visitors to a page where
I'll have more room.
I was reading in the Welling/Thomson book about converting a foreach
loop into another loop. Here is the original foreach loop and then what
I was trying to convert it to:
foreach ($items as $item)
{
echo "<tr><td><a href=" . $item["link"] . ">" . $item["title"] .
"</a><br>" . $item["description"] . "</td></tr>"; }
//**********************************************************************
*
for ($row=0; $row<2; $row++)
{
while (list ( $key, $value) = each ($items[$row]))
{
echo "<tr><td><a href=" . $item["link"] . ">" . $item["title"] .
"</a><br>" . $item["description"] . "</td></tr>";
}
}
Can anyone point me in the right direction? Right now I don't get an
error but I also don't get any text or results.
Thanks - Frank
Here is the code from the original article:
//****************************************
<html>
<head>
<basefont face="Verdana">
</head>
<body>
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td><b>New releases on freshmeat.net today:</b></td>
</tr>
<?php
// XML file
$file = "http://www.freshmeat.net/backend/fm-releases.rdf";
// set up some variables for use by the parser
$currentTag = "";
$flag = "";
$count = 0;
// this is an associative array of channel data with keys
("title","link","description")
$channel = array();
// this is an array of arrays, with each array element representing an
<item>
// each outer array element is itself an associative array
// with keys ("title", "link", "description")
$items = array();
// opening tag handler
function elementBegin($parser, $name, $attributes)
{
global $currentTag, $flag;
$currentTag = $name;
// set flag if entering <channel> or <item> block
if ($name == "ITEM")
{
$flag = 1;
}
else if ($name == "CHANNEL")
{
$flag = 2;
}
}
// closing tag handler
function elementEnd($parser, $name)
{
global $currentTag, $flag, $count;
$currentTag = "";
// set flag if exiting <channel> or <item> block
if ($name == "ITEM")
{
$count++;
$flag = 0;
}
else if ($name == "CHANNEL")
{
$flag = 0;
}
}
// character data handler
function characterData($parser, $data)
{
global $currentTag, $flag, $items, $count, $channel;
$data = trim(htmlspecialchars($data));
if ($currentTag == "TITLE" || $currentTag == "LINK" ||
$currentTag == "DESCRIPTION")
{
// add data to $channels[] or $items[] array
if ($flag == 1)
{
$items[$count][strtolower($currentTag)] .=
$data;
}
else if ($flag == 2)
{
$channel[strtolower($currentTag)] .= $data;
}
}
}
// create parser
$xp = xml_parser_create();
// set element handler
xml_set_element_handler($xp, "elementBegin", "elementEnd");
xml_set_character_data_handler($xp, "characterData");
xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, TRUE);
// read XML file
if (!($fp = fopen($file, "r")))
{
die("Could not read $file");
}
// parse data
while ($xml = fread($fp, 4096))
{
if (!xml_parse($xp, $xml, feof($fp)))
{
die("XML parser error: " .
xml_error_string(xml_get_error_code($xp)));
}
}
// destroy parser
xml_parser_free($xp);
// now iterate through $items[] array
// and print each item as a table row
foreach ($items as $item)
{
echo "<tr><td><a href=" . $item["link"] . ">" . $item["title"] .
"</a><br>" . $item["description"] . "</td></tr>"; }
?>
</table>
</body>
</html>
//******************************************************************
Frank Miller
Computer Specialist and Webmaster
Texas A&M University-Texarkana
2600 N. Robison Rd
Texarkana, Texas 75501
Office 165
Phone (903)223-3156 (2*2*61*37017349)
Fax (903)223-3139
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php