Who Among You Dare To Answer My Question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noyb

    Who Among You Dare To Answer My Question?

    Posted this a while ago, never got so much as a peep. Please take a look
    and give any thoughts.



    I've set up a news links section using the RSS parsing tutorial found
    here: http://www.sitepoint.com/article/560
    Everything works great but depending on the feed source there may be 3
    news links or 30. How can I limit the amount of titles, descriptions,
    and links output to be no more than 10?
    Thanks,
    Steve.

    [MY SLIGHTLY MODIFIED CODE PASTED BELOW]


    <dl>
    <?php

    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $feedurl="http://rss.news.yahoo. com/rss/business";

    function startElement($p arser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    $tag = $name;
    } elseif ($name == "ITEM") {
    $insideitem = true;
    }
    }

    function endElement($par ser, $name) {
    global $insideitem, $tag, $title, $description, $link;
    if ($name == "ITEM") {
    $description=st r_replace("'"," ",$description) ; //take out '
    characters that were causing probs with tooltip javascript
    printf("<dt><a href='%s' target='mtgnews ' onmouseover=\"r eturn
    overlib('%s')\" onmouseout=\"re turn nd();\">%s</a></dt>",
    trim($link),htm lspecialchars(t rim($descriptio n)),htmlspecial chars(trim($tit le)));

    $title = "";
    $description = "";
    $link = "";
    $insideitem = false;
    }
    }

    function characterData($ parser, $data) {
    global $insideitem, $tag, $title, $description, $link;
    if ($insideitem) {
    switch ($tag) {
    case "TITLE":
    $title .= $data;
    break;
    case "DESCRIPTIO N":
    $description .= $data;

    break;
    case "LINK":
    $link .= $data;
    break;
    }
    }
    }

    $xml_parser = xml_parser_crea te();
    xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
    xml_set_charact er_data_handler ($xml_parser, "characterData" );
    $fp = fopen($feedurl, "r")
    or die("Business News Unavailable.");

    while ($data = fread($fp, 4096))
    xml_parse($xml_ parser, $data, feof($fp))
    or die(sprintf("XM L error: %s at line %d",
    xml_error_strin g(xml_get_error _code($xml_pars er)),
    xml_get_current _line_number($x ml_parser)));
    fclose($fp);
    xml_parser_free ($xml_parser);

    ?>
    </dl>
  • Tim Van Wassenhove

    #2
    Re: Who Among You Dare To Answer My Question?

    In article <j_ewc.22136$Tn 6.13676@newsrea d1.news.pas.ear thlink.net>, Noyb wrote:[color=blue]
    > Posted this a while ago, never got so much as a peep. Please take a look
    > and give any thoughts.[/color]

    A few days is not a while.
    [color=blue]
    > I've set up a news links section using the RSS parsing tutorial found
    > here: http://www.sitepoint.com/article/560
    > Everything works great but depending on the feed source there may be 3
    > news links or 30. How can I limit the amount of titles, descriptions,
    > and links output to be no more than 10?[/color]

    You have a look in the PHP manual, and lookup control structures. I'm
    pretty sure there will be something that allows you repeated 10 or less
    times.

    Another option would be to write an XSL that generates a new XML
    document with maximum 10 items. Therefore you need to have a look at the XSL manual.

    Happy coding..

    --
    Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>

    Comment

    • Filth

      #3
      Re: Who Among You Dare To Answer My Question?

      > while ($data = fread($fp, 4096))[color=blue]
      > xml_parse($xml_ parser, $data, feof($fp))
      > or die(sprintf("XM L error: %s at line %d",
      > xml_error_strin g(xml_get_error _code($xml_pars er)),
      > xml_get_current _line_number($x ml_parser)));
      > fclose($fp);[/color]

      couldnt't you do something like OR $i <= '10' in the while statement and
      incrememnt $i in your while statement


      Comment

      • Shane Lahey

        #4
        Re: Who Among You Dare To Answer My Question? - rssfeed.php (0/1)

        On Sat, 05 Jun 2004 07:53:51 GMT, Noyb <zarwell@hotmai l.com> wrote:
        [color=blue]
        >Posted this a while ago, never got so much as a peep. Please take a look
        >and give any thoughts.
        >
        >
        >
        >I've set up a news links section using the RSS parsing tutorial found
        >here: http://www.sitepoint.com/article/560
        >Everything works great but depending on the feed source there may be 3
        >news links or 30. How can I limit the amount of titles, descriptions,
        >and links output to be no more than 10?
        >Thanks,
        >Steve.
        >
        >[MY SLIGHTLY MODIFIED CODE PASTED BELOW]
        >[/color]

        now Here's my slightly modified code :D ((attached as well so you don't have to cut n' paste))

        <?php

        $insideitem = false;
        $tag = "";
        $title = "";
        $description = "";
        $link = "";
        $rsscount = 0;
        $max_feeds_show n = false;
        $feeds_to_show = 10;

        $feedurl="http://rss.news.yahoo. com/rss/business";


        function startElement($p arser, $name, $attrs)
        {
        global $insideitem, $tag, $title, $description, $link, $max_feeds_show n;

        if ($max_feeds_sho wn == true)
        return; // already shown max # of items

        if ($insideitem)
        $tag = $name;
        elseif ($name == "ITEM")
        $insideitem = true;
        }

        function endElement($par ser, $name)
        {
        global $insideitem, $tag, $title, $description, $link, $max_feeds_show n, $rsscount,
        $feeds_to_show;
        if ($name == "ITEM" && $max_feeds_show n == false)
        {
        //take out ' characters that were causing probs with tooltip javascript
        //$description = str_replace("'" ,"",$descriptio n);
        // better yet, replace ' with \' so they don't cause problems
        $description = str_replace('\' ', '\\\'', trim($descripti on));

        printf("<dt><a href='%s' target='mtgnews ' onmouseover=\"r eturn overlib('%s')\"
        onmouseout=\"re turn nd();\">%s</a></dt>\n",
        trim($link), htmlspecialchar s($description) , htmlspecialchar s(trim($title)) );

        $title = '';
        $description = '';
        $link = '';
        $insideitem = false;


        if (++$rsscount >= $feeds_to_show)
        $max_feeds_show n = true;
        }
        }

        function characterData($ parser, $data)
        {
        global $insideitem, $tag, $title, $description, $link;
        if ($insideitem)
        {
        switch (strtoupper($ta g))
        {
        case "TITLE":
        $title .= $data;
        break;
        case "DESCRIPTIO N":
        $description .= $data;
        break;
        case "LINK":
        $link .= $data;
        break;
        }
        }
        }

        $xml_parser = xml_parser_crea te();
        xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
        xml_set_charact er_data_handler ($xml_parser, "characterData" );

        $fp = fopen($feedurl, "r")
        or die("Business News Unavailable.");

        while ($data = fread($fp, 4096))
        {
        if ($max_feeds_sho wn)
        break; // we have shown $feeds_to_show items..

        xml_parse($xml_ parser, $data, feof($fp))
        or die(sprintf("XM L error: %s at line %d",
        xml_error_strin g(xml_get_error _code($xml_pars er)),
        xml_get_current _line_number($x ml_parser)));
        }

        fclose($fp);
        xml_parser_free ($xml_parser);

        ?>

        Comment

        • Noyb

          #5
          Re: Who Among You Dare To Answer My Question? - rssfeed.php (0/1)

          Shane Lahey wrote:
          [color=blue]
          > On Sat, 05 Jun 2004 07:53:51 GMT, Noyb <zarwell@hotmai l.com> wrote:
          >
          >[color=green]
          >>Posted this a while ago, never got so much as a peep. Please take a look
          >>and give any thoughts.
          >>
          >>
          >>
          >>I've set up a news links section using the RSS parsing tutorial found
          >>here: http://www.sitepoint.com/article/560
          >>Everything works great but depending on the feed source there may be 3
          >>news links or 30. How can I limit the amount of titles, descriptions,
          >>and links output to be no more than 10?
          >>Thanks,
          >>Steve.
          >>
          >>[MY SLIGHTLY MODIFIED CODE PASTED BELOW]
          >>[/color]
          >
          >
          > now Here's my slightly modified code :D ((attached as well so you don't have to cut n' paste))
          >
          > <?php
          >
          > $insideitem = false;
          > $tag = "";
          > $title = "";
          > $description = "";
          > $link = "";
          > $rsscount = 0;
          > $max_feeds_show n = false;
          > $feeds_to_show = 10;
          >
          > $feedurl="http://rss.news.yahoo. com/rss/business";
          >
          >
          > function startElement($p arser, $name, $attrs)
          > {
          > global $insideitem, $tag, $title, $description, $link, $max_feeds_show n;
          >
          > if ($max_feeds_sho wn == true)
          > return; // already shown max # of items
          >
          > if ($insideitem)
          > $tag = $name;
          > elseif ($name == "ITEM")
          > $insideitem = true;
          > }
          >
          > function endElement($par ser, $name)
          > {
          > global $insideitem, $tag, $title, $description, $link, $max_feeds_show n, $rsscount,
          > $feeds_to_show;
          > if ($name == "ITEM" && $max_feeds_show n == false)
          > {
          > //take out ' characters that were causing probs with tooltip javascript
          > //$description = str_replace("'" ,"",$descriptio n);
          > // better yet, replace ' with \' so they don't cause problems
          > $description = str_replace('\' ', '\\\'', trim($descripti on));
          >
          > printf("<dt><a href='%s' target='mtgnews ' onmouseover=\"r eturn overlib('%s')\"
          > onmouseout=\"re turn nd();\">%s</a></dt>\n",
          > trim($link), htmlspecialchar s($description) , htmlspecialchar s(trim($title)) );
          >
          > $title = '';
          > $description = '';
          > $link = '';
          > $insideitem = false;
          >
          >
          > if (++$rsscount >= $feeds_to_show)
          > $max_feeds_show n = true;
          > }
          > }
          >
          > function characterData($ parser, $data)
          > {
          > global $insideitem, $tag, $title, $description, $link;
          > if ($insideitem)
          > {
          > switch (strtoupper($ta g))
          > {
          > case "TITLE":
          > $title .= $data;
          > break;
          > case "DESCRIPTIO N":
          > $description .= $data;
          > break;
          > case "LINK":
          > $link .= $data;
          > break;
          > }
          > }
          > }
          >
          > $xml_parser = xml_parser_crea te();
          > xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
          > xml_set_charact er_data_handler ($xml_parser, "characterData" );
          >
          > $fp = fopen($feedurl, "r")
          > or die("Business News Unavailable.");
          >
          > while ($data = fread($fp, 4096))
          > {
          > if ($max_feeds_sho wn)
          > break; // we have shown $feeds_to_show items..
          >
          > xml_parse($xml_ parser, $data, feof($fp))
          > or die(sprintf("XM L error: %s at line %d",
          > xml_error_strin g(xml_get_error _code($xml_pars er)),
          > xml_get_current _line_number($x ml_parser)));
          > }
          >
          > fclose($fp);
          > xml_parser_free ($xml_parser);
          >
          > ?>
          >[/color]

          Thanks Shane, I was trying the test down in the while loop and was stumped.
          Steve.

          Comment

          Working...