How to show news

Discussion in 'PHP' started by cobano, Jul 24, 2009.

  1. #1
    Hi all,

    I'm launching new jobs site - Environmental Jobs In USA.
    As you can see at the right side of the side I want to add news, there are any scripts so I can add RSS of news sites and that will be shown at my site.
    It's php based.

    Thanks for the helpers...
     
    cobano, Jul 24, 2009 IP
  2. blake3334

    blake3334 Member

    Messages:
    407
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    Cobano, if you want to find free scripts that can do this I am pretty sure hotscripts might have something your looking for.
     
    blake3334, Jul 24, 2009 IP
  3. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    here it is ...

    <?php

    $xml_parser = xml_parser_create();

    // Set the functions to handle opening and closing tags
    xml_set_element_handler($xml_parser, "startElement", "endElement");

    // Set the function to handle blocks of character data
    xml_set_character_data_handler($xml_parser, "characterData");

    // Open the XML file for reading
    $fp = fopen("http://www.sitepoint.com/rss.php","r")
    or die("Error reading RSS data.");

    // Read the XML file 4KB at a time
    while ($data = fread($fp, 4096))
    // Parse each 4KB chunk with the XML parser created above
    xml_parse($xml_parser, $data, feof($fp))
    // Handle errors in parsing
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));

    // Close the XML file
    fclose($fp);

    // Free up memory used by the XML parser
    xml_parser_free($xml_parser);

    ?>
     
    mdrobiul, Jul 24, 2009 IP
  4. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    sorry use this one......

    <?php

    class RSSParser {

    var $insideitem = false;
    var $tag = "";
    var $title = "";
    var $description = "";
    var $link = "";

    function startElement($parser, $tagName, $attrs) {
    if ($this->insideitem) {
    $this->tag = $tagName;
    } elseif ($tagName == "ITEM") {
    $this->insideitem = true;
    }
    }

    function endElement($parser, $tagName) {
    if ($tagName == "ITEM") {
    printf("\"<dt><b><a href='%s'>%s</a></b></dt>",trim($this->link),trim($this->title));
    printf("<b>%s</b>",trim($this->description));
    $this->title = "";
    $this->description = "";
    $this->link = "";
    $this->insideitem = false;
    }
    }

    function characterData($parser, $data) {
    if ($this->insideitem) {
    switch ($this->tag) {
    case "TITLE":
    $this->title .= $data;
    break;
    case "DESCRIPTION":
    $this->description .= $data;
    break;
    case "LINK":
    $this->link .= $data;
    break;
    }
    }
    }
    }

    $xml_parser = xml_parser_create();
    $rss_parser = new RSSParser();
    xml_set_object($xml_parser,&$rss_parser);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen("http://www.thedailystar.net/latest/rss/rss.xml","r")
    or die("Error reading RSS data.");
    while ($data = fread($fp, 4096))
    xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);

    ?>
     
    mdrobiul, Jul 24, 2009 IP
  5. mdrobiul

    mdrobiul Peon

    Messages:
    186
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    please change this line : printf("\"<dt><b><a href='%s'>%s</a></b></dt>",trim($this->link),trim($this->title));

    to

    printf("<dt><b><a href='%s'>%s</a></b></dt>",trim($this->link),trim($this->title));
     
    mdrobiul, Jul 24, 2009 IP