Jump to content


Photo

how to sort a news feed according to the categories?


  • Please log in to reply
2 replies to this topic

#1 caine

caine
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 05 September 2006 - 12:42 PM

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.nier...ubulletins.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.nier...bulletins.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>"

?>

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 05 September 2006 - 03:43 PM

Save this as "bulletin.xsl"
<?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>

and run this script

<?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();
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 caine

caine
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 08 February 2007 - 08:50 AM

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;

}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users