[JESSE] RSS Parser / Loader

Gestart doorOne2xs28 reacties29 weergaven

29 berichten · pagina 2 van 2

One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Bedankt, nog meer ideeën? smile.
0
Berichten
2.692
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nice Jesse biggrinZie je wel dat je wel wat kunt biggrin emo
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ik zou er een class van maken

dus

$rss = new RSS(_parameters_);


En dan dat je bepaalde dingen kan doen:

$rss->getArticleById(1); // Haalt het eerste artikel op
$rss->getArticleByText("Hank"); // Haalt artikelen met het woord "hank" erin op.


Dan kan je de tweet namelijk ook appart inbouwen:

$rss->Tweet($rss->getArticleById(1)); // Tweet artikel 1


Zo kan je in de Tweet functie weer een tweetclass aanroepen, en ga je meer richting OOP.

Zo kan je namelijk ook code en layout gescheiden houden, wat je momenteel niet doet.
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Bedankt voor je advies, ik zal mijn volgende scripts ook proberen in OOP te schrijven smile
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Xjessex schreef:
Bedankt voor je advies, ik zal mijn volgende scripts ook proberen in OOP te schrijven smile
Het is niet zozeer OOP. Het is OOP gericht. Maar ik zou zeker dit script in een class willen zien. Als je het niet erg vind kan ik wel een voorbeeldje baseren op jouw code?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Als je een klein voorbeeldje hebt graag, want zo goed ben ik niet in OOP..
Op Glybe.nl werken we met classes alleen Bas maakt meestal die classes en ik neem de basis over..
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
Zolang je dan maar geen HTML-output door een class laat uitspugen.
Houd de logica en de view altijd gescheiden van elkaar. smile
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
En hoe houd je dat precies gescheiden? Ik wil namelijk wel de eenvoud erin terug hebben, dus dat je een class include en deze oproept met 2 á 3 regels, en dat die HTML shit wel allemaal in de class zit, maar dan in een aparte functie?
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
Je hebt een class, en die laat je een string of een array returnen.
Door de functie/method (bijv. generateRSS() ) aan te roepen in je view kan je die als array in een foreach uitwerpen....
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Een klein beginnetje. Dit is waar ik mee zou beginnen althans.

[COLOR= #0000BB]<?php[/COLOR]

[URL="http://www.php.net/class"]class[/URL] RSS_Reader
[COLOR= #007700]{[/COLOR]
    
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$strUrl[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$arrValues[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$arrIndex[/COLOR][COLOR= #007700];[/COLOR]
    
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]is_url[COLOR= #007700]([/COLOR][COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700];[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]load[COLOR= #007700]();
            
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #FF8000]// TODO: betere errorafhandeling.[/COLOR]
            throw [COLOR= #007700]new[/COLOR] Exception[COLOR= #007700]([/COLOR][COLOR= #DD0000]"Parameter not an Url"[/COLOR][COLOR= #007700]);    
        }
    }[/COLOR]
    
    [COLOR= #FF8000]// TODO: Alle artikelen in een array terug geven[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getAllArticle[COLOR= #007700]()
    {
        
    }[/COLOR]
    
    [COLOR= #FF8000]// TODO: Artikel met een bepaald ID terug geven[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getArticleById[COLOR= #007700]()
    {
        
    }[/COLOR]
    
    [COLOR= #FF8000]// TODO: Alle artikelen met een bepaald woord / regex erin teruggeven als array[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] getArticleBySearch[COLOR= #007700]()
    {
        
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] setUrl[COLOR= #007700]([/COLOR][COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700])
    {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700];[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]load[COLOR= #007700]();
            
    }[/COLOR]
    
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #007700]function[/COLOR] load[COLOR= #007700]()
    {[/COLOR]
        [COLOR= #0000BB]$objParser[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/xml_parser_create"]xml_parser_create[/URL][COLOR= #007700]();[/COLOR] 
        [COLOR= #0000BB]$strFile[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/implode"]implode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000][/COLOR][COLOR= #007700],[/COLOR] [URL="http://www.php.net/file"]file[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl[COLOR= #007700]));[/COLOR] 
        [URL="http://www.php.net/xml_parser_set_option"]xml_parser_set_option[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$objParser[/COLOR][COLOR= #007700],[/COLOR] XML_OPTION_SKIP_WHITE[COLOR= #007700],[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]);[/COLOR] 
        [URL="http://www.php.net/xml_parser_set_option"]xml_parser_set_option[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$objParser[/COLOR][COLOR= #007700],[/COLOR] XML_OPTION_CASE_FOLDING[COLOR= #007700],[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700]);[/COLOR] 
        [URL="http://www.php.net/xml_parse_into_struct"]xml_parse_into_struct[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$objParser[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strFile[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$tmpValues[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$tmpIndex[/COLOR][COLOR= #007700]);[/COLOR] 
        [URL="http://www.php.net/xml_parser_free"]xml_parser_free[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$objParser[/COLOR][COLOR= #007700]);[/COLOR] 
            
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrValues [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$tmpValues[/COLOR][COLOR= #007700];[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrIndex [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$tmpIndex[/COLOR][COLOR= #007700];    
    }[/COLOR]
    
    [COLOR= #FF8000]// TODO: Valideer url.[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #007700]function[/COLOR] is_url[COLOR= #007700]([/COLOR][COLOR= #0000BB]$parUrl[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] true[COLOR= #007700];    
    }
    
    
}[/COLOR]

?>


Zoals aarclay zegt wil je dus in mijn getter-methods (getArticleBy... functies) arrays teruggeven.

Je kan dan iets in de trant van:

[COLOR= #0000BB]<?php
$rss[/COLOR] [COLOR= #007700]= new[/COLOR] RSS_Reader[COLOR= #007700]([/COLOR][COLOR= #DD0000]"http://www.mijnurl.nl/rss.xml"[/COLOR][COLOR= #007700]);[/COLOR]

[COLOR= #0000BB]$tmpArr[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$rss[/COLOR][COLOR= #007700]->[/COLOR]getArticleBySearch[COLOR= #007700]([/COLOR][COLOR= #DD0000]"Hank"[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpArr[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$tmpKey[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$tmpValue[/COLOR][COLOR= #007700])
{[/COLOR]
    [COLOR= #FF8000]// HTML voor weergeven[/COLOR]
[COLOR= #007700]}[/COLOR]

?>


doen.

Het lijkt misschien moeilijk, maar op deze manier kan je code makkelijker hergebruiken in meerdere projecten.
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Precies wat ik vroeger nodig had maar nooit vinden kon, toch bedankt smile
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
Yess, dat bedoel ik.

je doet alleen weinig met setUrl(), Rik wink.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Aarclay schreef:
Yess, dat bedoel ik.

je doet alleen weinig met setUrl(), Rik wink.
Het is even een snel voorbeeldje. Zal vast hier en daar iets hebben laten vallen. Ik valideer namelijk nog helemaal niks. Maar dat is voor Jesse een leuke uitdaging om te doen. Hij wilde tenslotte ook proberen het in een class te zetten.
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Zeker, ik hoef ook geen hele scripts voor gekauwd te krijgen, als ik de plaintext uitleg niet snap is een kleine opzet / voorbeeld wel handig.. Maar wederom is Google mijn mattie wink

Bedankt Rik, ik zal zodra hij af is hem hier neer zetten!
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren