Jump to content

how to sort a news feed according to the categories?


caine

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>"

?>
Link to comment
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]
Link to comment
Share on other sites

  • 5 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.