SimplePortal

Customization => Custom Coding => Topic started by: Nabil on December 30, 2008, 02:09:15 PM

Title: rss Block but not the Original
Post by: Nabil on December 30, 2008, 02:09:15 PM
hi ,
I want to add a php block with rss display function , I actually not using the Original rss block with Simple Portal, because I want to change the way it looks , add some div styles, display it with different colors than the Original , so what is the function I should use in the php block in order to display rss feeds from Websites, I know that this function I mean the rss function is there in the Sportal1-1.php in the Source folder , but I'm not sure how to use it for my custom php block , does anyone know how ?
any help will be appreciated . 
Title: Re: rss Block but not the Original
Post by: [SiNaN] on February 03, 2009, 07:51:38 AM
You can use built-in functions to fetch the data and modify the look as you wish. You can use these codes in a PHP block:

Code: [Select]
$feed = 'FEEDURL';

$rss_data = array();
$rss_current = "";
$rss_counter = 0;
$rss_main = '';

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "sp_startElement", "sp_endElement");

xml_set_character_data_handler($xml_parser, "sp_characterData");

if (!($fp = @fopen($feed, "r")))
return;

while ($data = fread($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{
$xmlerr = (sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));

break;
}
}

fclose($fp);

xml_parser_free($xml_parser);

if (!empty($xmlerr))
{
echo $xmlerr;

return;
}

if (empty($rss_data))
return;

if (!empty($rss_data['IMAGE']))
echo '<a href="', $rss_data['LINK'], '" target="_blank"><img src="', $rss_data['IMAGE']['URL'], '" align="middle" alt="', $rss_data['IMAGE']['TITLE'], '" /></a>', $rss_data['TITLE'], '<br /><br />';
else
echo $rss_data['TITLE'], '<br />';

echo '<em>', $rss_data['DESCRIPTION'], '</em><br />';

if (isset($rss_data['ITEMS']))
{
if (count($rss_data['ITEMS']) > 0)
{
for($i = 0; $i < count($rss_data['ITEMS']); $i++)
{
echo '<span style="font-weight: bold;"><a href="', $rss_data['ITEMS'][$i]['LINK'], '" target="_blank">', $rss_data['ITEMS'][$i]['TITLE'], '</a></span><br />';
echo html_entity_decode($rss_data['ITEMS'][$i]['DESCRIPTION']) . '<br />';
if ($i != count($rss_data['ITEMS']) - 1)
echo '<hr />';
}
}
else
return;
}

The part you should modify is this:

Code: [Select]
if (!empty($rss_data['IMAGE']))
echo '<a href="', $rss_data['LINK'], '" target="_blank"><img src="', $rss_data['IMAGE']['URL'], '" align="middle" alt="', $rss_data['IMAGE']['TITLE'], '" /></a>', $rss_data['TITLE'], '<br /><br />';
else
echo $rss_data['TITLE'], '<br />';

echo '<em>', $rss_data['DESCRIPTION'], '</em><br />';

if (isset($rss_data['ITEMS']))
{
if (count($rss_data['ITEMS']) > 0)
{
for($i = 0; $i < count($rss_data['ITEMS']); $i++)
{
echo '<span style="font-weight: bold;"><a href="', $rss_data['ITEMS'][$i]['LINK'], '" target="_blank">', $rss_data['ITEMS'][$i]['TITLE'], '</a></span><br />';
echo html_entity_decode($rss_data['ITEMS'][$i]['DESCRIPTION']) . '<br />';
if ($i != count($rss_data['ITEMS']) - 1)
echo '<hr />';
}
}
else
return;
}

Also the $feed variable at the top of course.
Title: Re: rss Block but not the Original
Post by: 130860 on February 03, 2009, 10:50:07 AM
Thank you for posting this !! I was looking for something like that  ;P

thanks !!!     :applause:
Title: Re: rss Block but not the Original
Post by: Nabil on February 03, 2009, 01:59:10 PM
Thanks Blue Dream , that was an old Topic , I'm already
using rss with my ad Mod http://simpleportal.net/index.php?topic=1219.0

by using the rss block but giving it for example #adNews
to avoid the block frames
Title: Re: rss Block but not the Original
Post by: yield on February 22, 2009, 12:40:44 PM
Hi! I was looking for a way to just show the feeds headers/titles and not the full text -- which may be clickable/expandable.

Is it possible to achieve that with any setup on the code posted by Blue Dream?

Thanks
Title: Re: rss Block but not the Original
Post by: Robbo on February 22, 2009, 06:59:34 PM
Hi! I was looking for a way to just show the feeds headers/titles and not the full text -- which may be clickable/expandable.

Is it possible to achieve that with any setup on the code posted by Blue Dream?

Thanks

Copy this into a PHP block. I haven't tested it very much but it should be fine (could look prettier).

People with JavaScript disabled will have all descriptions expanded.

Code: [Select]
    $feed = '';

    $rss_data = array();
    $rss_current = "";
    $rss_counter = 0;
    $rss_main = '';

    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "sp_startElement", "sp_endElement");

    xml_set_character_data_handler($xml_parser, "sp_characterData");

    if (!($fp = @fopen($feed, "r")))
    {
        echo 'Invalid feed URL.';
        return;
    }

    while ($data = fread($fp, 4096))
    {
        if (!xml_parse($xml_parser, $data, feof($fp)))
        {
            $xmlerr = (sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser)));

            break;
        }
    }

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

    if (!empty($xmlerr))
    {
        echo $xmlerr;
        return;
    }

    if (empty($rss_data))
    {
        echo 'No items to show.';
        return;
    }

    echo '<center>';
    if (!empty($rss_data['IMAGE']))
        echo '<a href="', $rss_data['LINK'], '" target="_blank"><img src="', $rss_data['IMAGE']['URL'], '" align="middle" alt="', $rss_data['IMAGE']['TITLE'], '" /></a>', $rss_data['TITLE'], '<br /><br />';
    else
        echo '<b>', $rss_data['TITLE'], '</b><br />';

    echo '<em>', $rss_data['DESCRIPTION'], '</em><br /><hr /></center><br />';

    if (isset($rss_data['ITEMS']))
    {
        echo '
        <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
            function toggleDescription(id)
            {
                id = \'rss_descr_\' + id;
                if(document.getElementById(id).style.display == \'none\')
                    document.getElementById(id).style.display = \'\';
                else
                    document.getElementById(id).style.display = \'none\';
            }
        // ]]></script>';
       
        $max = count($rss_data['ITEMS']);
        if ($max > 0)
        {
            for($i = 0; $i < $max; $i++)
            {
                echo '<div id="test"></div>
                    <span style="font-weight: bold;"><a href="javascript:toggleDescription(', $i, ')">', $rss_data['ITEMS'][$i]['TITLE'], '</a></span><br /><br />';
                echo '
                    <div id="rss_descr_', $i, '">
                        ', html_entity_decode($rss_data['ITEMS'][$i]['DESCRIPTION']) . '<br /><br />
                        <a class="smalltext" href="', $rss_data['ITEMS'][$i]['LINK'], '" target="_blank"><b>view link »</b></a>';
                if ($i != $max - 1)
                    echo '<hr />';
                echo '
                    </div>
                    <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
                        toggleDescription(', $i, ');
                    // ]]></script>';
               
            }
        }
    }
Title: Re: rss Block but not the Original
Post by: yield on February 22, 2009, 08:43:35 PM
Hello Robbo! Thank you very much for replying to my request.

I put that into a php block, filled the feed url at $feed variable, but then all I got is this message: "No items to show. "

However, if I paste the same feed url into the default rss block, I got a few items, but with a huge amount of text --hence my request.

Any idea of what's wrong here?

Thanks
Title: Re: rss Block but not the Original
Post by: Robbo on February 22, 2009, 09:09:31 PM
My bad.

Add at the top...

Code: [Select]
global $rss_data, $rss_current, $rss_counter, $rss_main;
Title: Re: rss Block but not the Original
Post by: [SiNaN] on February 23, 2009, 12:18:38 AM
You'll be able to limit items and body in next version. And making body collapsable sounds like fun, I'll try that.
Title: Re: rss Block but not the Original
Post by: yield on February 23, 2009, 07:24:18 PM
Hi!

Thank again Robbo, it works now! :D

I know very few of html and nothing of php/javascript, but I managed to do some modifications (throught trial and error):
- removed the rss title (because that will be the block name);
- inserted class="smalltext" to make text smaller, it was occupying too much space.

Well, but I still got a problem (sorry being so boring!): I'd like to display two different feeds (news and events), but when both blocks are active, the text shows up expanded by default on both blocks, and impossible to collapse on the second block. Is it some problem with the variables names being the same? Which ones are needed to change?

Blue Dream, that would be very nice! Can't wait for next version :) Also, I guess I forgot to thank you for your reply on my first thread: thank you!
Title: Re: rss Block but not the Original
Post by: Robbo on February 24, 2009, 07:29:31 AM
Yeah it is a problem with the description ids being the same. I never thought of multiple ids.

Use this code instead. I made the changes you said you did so you should be able to just copy it over (backup just incase).

Code: [Select]
    global $rss_data, $rss_current, $rss_counter, $rss_main;

    // This needs to be unique to each block
    $id = 1;
   
    // Your feed url
    $feed = 'http://simpleportal.net/index.php?type=rss2;action=.xml;board=8.0;sa=news';

    $rss_data = array();
    $rss_current = "";
    $rss_counter = 0;
    $rss_main = '';

    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "sp_startElement", "sp_endElement");

    xml_set_character_data_handler($xml_parser, "sp_characterData");

    if (!($fp = @fopen($feed, "r")))
    {
        echo 'Invalid feed URL.';
        return;
    }

    while ($data = fread($fp, 4096))
    {
        if (!xml_parse($xml_parser, $data, feof($fp)))
        {
            $xmlerr = (sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser)));

            break;
        }
    }

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

    if (!empty($xmlerr))
    {
        echo $xmlerr;
        return;
    }

    if (empty($rss_data))
    {
        echo 'No items to show.';
        return;
    }

    if (isset($rss_data['ITEMS']))
    {
        echo '
        <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
            function toggleDescription(id)
            {
                id = \'rss_descr_\' + id;
                if(document.getElementById(id).style.display == \'none\')
                    document.getElementById(id).style.display = \'\';
                else
                    document.getElementById(id).style.display = \'none\';
            }
        // ]]></script>';
       
        $max = count($rss_data['ITEMS']);
        if ($max > 0)
        {
            for($i = 0; $i < $max; $i++)
            {
                echo '
                    <span style="font-weight: bold;" class="smalltext"><a href="javascript:toggleDescription(\'', $id, '_', $i, '\')">', $rss_data['ITEMS'][$i]['TITLE'], '</a></span><br />';
                echo '
                    <div id="rss_descr_', $id, '_', $i, '" class="smalltext">
                        ', html_entity_decode($rss_data['ITEMS'][$i]['DESCRIPTION']) . '<br /><br />
                        <a href="', $rss_data['ITEMS'][$i]['LINK'], '" target="_blank"><b>view link »</b></a>';
                if ($i != $max - 1)
                    echo '<hr />';
                echo '
                    </div>
                    <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
                        toggleDescription(\'', $id, '_', $i, '\');
                    // ]]></script>';
               
            }
        }
    }

At the top you will see an id, simply make that different for each block and it should work fine.
Title: Re: rss Block but not the Original
Post by: yield on February 24, 2009, 09:29:55 AM
Robbo, now it's working flawlessly! Thank you very much  ;D

I did two more changes:

1. removed the "view link" hyperlink at the end of every item. I noticed that it would point to a topic when using SP feed, but with the feed I'm using it will point to an inexistent url (not a problem);
2. added a little dot before titles, to make it easier to distinguish each title.

Best regards
SimplePortal 2.3.8 © 2008-2024, SimplePortal