Jump to content

Archived

This topic is now archived and is closed to further replies.

caine

how to sort a news feed according to the categories?

Recommended Posts

I'm currently working on php news feed, but I have to sort the news according to departments before the display. In my case, I must arranged those news according to department "[xxxx]" following <title> in the http://everling.nierchi.net/mmubulletins.php. The following is my xml parser. Then I have to activate the hyperlink. Wad else can I do in the following code?

<?php

$URL="http://everling.nierchi.net/mmubulletins.php";

$file = fopen($URL, 'r');

if(!$file)
die("Cannot open file.");


//read rss feed contents
$readfile = fread($file, 1000);
$searchfile = eregi("<item>(.*)</item>", $readfile, $array);
//place articles in array
$article = explode("<item>", $array[0]);
$counter = count($article);
echo "<table border='1'>";
echo "<th colspan='2'>Lastest News</th>";



//loop to display all bulletins
for ($i=0; $i<count; $i++)
{

//fetch titles
ereg("<title>(.*)</title>", $article[i], $title);
//fetch categories
ereg("<category>(.*)</category>", $article[i], $category);
//fetch descriptions
ereg("<description>(.*)</description>", $article[i], $description);
//fetch links
ereg("<link>(.*)</link>", $article[i], $link);

echo "<tr><td>";
echo $title[1];
echo "</td><td>";
echo "<a href='$link[1]'\>$link[1]</a>";
echo "</td></tr>";
}

echo "</table>"

?>

Share this post


Link to post
Share on other sites
Save this as "bulletin.xsl"
[code]
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>Bulletin</h2>
    <table border="1">
      <tr bgcolor="#CCCCCC">
        <th>Category</th>
        <th>Title</th>
        <th>Link</th>
      </tr>
      <xsl:for-each select="rss/channel/item">
      <xsl:sort select="category"/>
      <tr>
        <td><xsl:value-of select="category"/></td>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="link"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>[/code]

and run this script

[code]<?php
    $xml = simplexml_load_file('http://everling.nierchi.net/mmubulletins.php');
    $xsl = simpleXML_load_file('bulletin.xsl');
    $proc = new XsltProcessor();
    $proc->importStylesheet($xsl);
    $newxml = $proc->transformToDoc($xml);
    print $newxml->saveXML();
?>[/code]

Share this post


Link to post
Share on other sites
How do I add on this subfunction into the xsl and php code?

function decode($text)
{
$text =  html_entity_decode($text,ENT_QUOTES);
return $text;

}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.