nargy | Ok, je te file le code que j'utilise. Je le metes dans le fichier rss.inc, que j'inclue quand j'en ai besoin avec include("~/phplib/rss.inc" ).
rss.inc:
Code :
- <?
- $depth = array();
- $xml_tree = array();
- function debutElement($parser, $name, $attrs)
- {
- global $depth;
- global $xml_tree;
- $xml_tree[$parser][$depth[$parser]][]=array($name,$attrs);
- $depth[$parser]++;
- }
- function finElement($parser, $name)
- {
- global $depth;
- global $xml_tree;
- $xml_tree[$parser][$depth[$parser]-1][count($xml_tree[$parser][$depth[$parser]-1])-1][2]=$xml_tree[$parser][$depth[$parser]];
- unset($xml_tree[$parser][$depth[$parser]]);
- $depth[$parser]--;
- }
- function characterData($parser, $data)
- {
- global $xml_tree;
- global $depth;
- $xml_tree[$parser][$depth[$parser]-1][count($xml_tree[$parser][$depth[$parser]-1])-1][3].=$data;
- }
- function xml2array($file)
- {
- global $depth;
- global $xml_tree;
- $xml_parser = xml_parser_create();
- $depth[$xml_parser]=0;
- xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
- xml_set_element_handler($xml_parser, "debutElement", "finElement" );
- xml_set_character_data_handler($xml_parser, "characterData" );
- $err=error_reporting(0);
- if (!($fp = fopen($file, "r" ))) {
- error_reporting($err);
- $xml_tree[$xml_parser]=array();
- return array();
- }
- error_reporting($err);
- while ($data = fread($fp, 4096))
- {
- if (!xml_parse($xml_parser, $data, feof($fp)))
- {
- fclose($fp);
- $xml_tree[$xml_parser]=array();
- return array();
- }
- }
- $xml_array=$xml_tree[$xml_parser][0];
- unset($xml_tree[$xml_parser]);
- xml_parser_free($xml_parser);
- fclose($fp);
- return $xml_array;
- }
- function xmlarray2rss($a)
- {
- $r=array();
- if (!$a) return $r;
- if (is_string($a[0]))
- {
- if ($a[0]=='ITEM')
- $r[]=$a[2][0][3].": ".$a[2][1][3];
- if (is_array($a[2]))
- {
- for(reset($a[2]);list(,$v)=each($a[2]);)
- {
- $pr=xmlarray2rss($v);
- if (count($pr))
- for(reset($pr);list(,$vpr)=each($pr);)
- $r[]=$vpr;
- }
- }
- }
- else
- for(reset($a);list(,$v)=each($a);)
- {
- $pr=xmlarray2rss($v);
- if (count($pr))
- for(reset($pr);list(,$vpr)=each($pr);)
- $r[]=$vpr;
- }
- return $r;
- }
- function show_rss($title,$file,$width, $delay)
- {
- $xml_array=xml2array($file);
- $rss=xmlarray2rss($xml_array);
- $rsstxt=ereg_replace("[ \t\n]+"," ",implode(" -=-=-=- ",$rss));
- $jsrsstxt=ereg_replace("(['\"\\])","\\\\1",$rsstxt);
- ?>
- <table cellpadding=0 cellspacing=0 border=0><tr><td>
- <? echo htmlentities($title) ?>
- </td><td><span id='st' style="white-space:pre;font-family:monospace;"></span>
- </td><td>
- <SCRIPT language="JavaScript" type="text/javascript">
- <!--
- var mtt_db = (document.compatMode && document.compatMode != 'BackCompat')? document.documentElement : document.body? document.body : null,
- mtt_n = navigator.userAgent.toLowerCase(),
- mtt_op = !!(window.opera && document.getElementById),
- mtt_ie = mtt_n.indexOf('msie') != -1 && document.all && mtt_db && !mtt_op,
- mtt_n4 = (document.layers && typeof document.classes != "undefined" );
- var tiptext='<? echo $jsrsstxt ?>';
- var tiptextx=Math.random()*tiptext.length;
- var tiptextl=<? echo $width ?>;
- var tiptextdir=1;
- function GetById(t_id)
- {
- return (
- mtt_n4? (document.layers[t_id] || null)
- : mtt_ie? (document.all[t_id] || null)
- : (document.getElementById(t_id) || null)
- );
- }
- function movetext()
- {
- var st=GetById('st');
- if (!st) return;
- var str='';
- var i;
- var l=0;
- if (tiptextx<tiptextl)
- {
- for(i=0;i<tiptextl-tiptextx;i++,l++) str+=' ';
- var s=tiptext.substr(0, tiptextx);
- str+=s;
- l+=s.length;
- }
- else
- {
- str=tiptext.substr(tiptextx-tiptextl,tiptextl);
- l+=str.length;
- }
- for(i=l;i<tiptextl;i++) str+=' ';
- if (tiptextdir)
- {
- st.innerHTML='<pre style="margin:0px">'+str+'<'+'/pre>';
- tiptextx=(tiptextx+tiptextdir)%(tiptext.length+tiptextl);
- if (tiptextx<0) tiptextx+=tiptext.length+tiptextl;
- tiptextx=(tiptextx)%(tiptext.length+tiptextl);
- }
- setTimeout('movetext();',<? echo $delay; ?> );
- }
- document.write(' <INPUT style="font-size:xx-small;" type=button value="<" onclick="javascript:tiptextdir=tiptextdir>0?0:tiptextdir-1;"><INPUT style="font-size:xx-small;" type=button value=">" onclick="javascript:tiptextdir=tiptextdir<0?0:tiptextdir+1;">');
- movetext();
- // -->
- </SCRIPT>
- </td></tr></table>
- <?
- }
- function show_rss2($title,$file,$width, $delay)
- {
- global $skins, $skin, $graphics, $print;
- $xml_array=xml2array($file);
- $rss=xmlarray2rss($xml_array);
- foreach($rss as $k => $v)
- $rss[$k]="<span style=\"white-space:nowrap\"> -===- ".ereg_replace("(['\"\\])","\\\\1",$v)." </span>";
- $rsstxt=implode("','",$rss);
- ?>
- <table style="align:right;" cellpadding=0 cellspacing=0 border=0><tr><td style="white-space:nowrap">
- <? echo htmlentities($title) ?>
- </td><td width="100%">
- <script language="JavaScript" type="text/javascript">
- <!--
- var messages=new Array('<? echo $rsstxt ?>');
- var newsscrollerspeed0=6;
- var newsscrollerbgspeed0=2;
- var newsscrollerinterval=<? echo $delay ?>;
- var newsscrollerspeed=newsscrollerspeed0;
- var newsscrollerbgspeed=newsscrollerbgspeed0;
- -->
- </script>
- <DIV style="<? echo ($graphics!='n' && $print!='y'?"background:url(/~nico/pres/".$skins[$skin]['img_bg']." );":"" ) ?>border:inset #efefef 3px;width:<? echo $width ?>%;overflow:hidden;white-space:pre;" onmouseover="javascript:newsscrollerspeed=0;newsscrollerbgspeed=0;" onmouseout="javascript:newsscrollerspeed=newsscrollerspeed0;newsscrollerbgspeed=newsscrollerbgspeed0;" id="scroller">
- </div>
- <script language="JavaScript" type="text/javascript">
- <!--
- var scroller=document.getElementById('scroller');
- var newsscrolltext='';
- for(i=0;i<messages.length;i++)
- {
- if(i==0) newsscrolltext+='<table id="scrollercontent" style="cursor:default;position:relative;vertical-align:middle;white-space:pre;" border=0 cellpadding=0 cellspacing=0><tr><td id="scrollercell'+i+'" style="white-space:pre;">';
- else newsscrolltext+='</td><td id="scrollercell'+i+'">';
- newsscrolltext+=messages[i];
- }
- newsscrolltext+='</td></tr></table>';
- scroller.innerHTML=newsscrolltext;
- function newsscrollbounds()
- {
- var firstcell;
- while(newsscrollerx>(firstcell=document.getElementById('scrollercell0')).offsetWidth)
- {
- newsscrollerx-=firstcell.offsetWidth;
- var firsthtml=firstcell.innerHTML;
- for(i=1;i<messages.length;i++)
- {
- document.getElementById('scrollercell'+(i-1)).innerHTML=
- document.getElementById('scrollercell'+i).innerHTML;
- }
- document.getElementById('scrollercell'+(messages.length-1)).innerHTML=
- firsthtml;
- }
- }
- var newsscrollerx=parseInt((new Date()).getTime()/newsscrollerinterval*newsscrollerspeed)%document.getElementById('scrollercontent').offsetWidth;
- var newsscrollerbgx=newsscrollerx;
- function newsscroll()
- {
- newsscrollerx+=newsscrollerspeed;
- newsscrollbounds();
- newsscrollerbgx=(newsscrollerbgx+newsscrollerbgspeed);
- var scroller=document.getElementById('scroller');
- newsscrollerbgx%=scroller.offsetWidth;
- scroller.style.backgroundPosition=(-newsscrollerbgx)+'px 0px';
- var scrollercontent=document.getElementById('scrollercontent');
- scrollercontent.style.left=(-newsscrollerx)+'px';
- scrollercontent.visible=true;
- }
- setInterval('newsscroll()',newsscrollerinterval);
- -->
- </script>
- </td></tr></table>
- <?
- }
- ?>
|
Il y a deux affichages différents, tous deux en javascript. L'un affiche avec un défilement de caractères, l'autre est plus joli. Après avoir inclu rss.inc, tu utilise donc l'une des deux fonctions:
show_rss($title,$file,$width, $delay)
show_rss2($title,$file,$width, $delay)
- $title: titre à afficher , par exemple: 'news portables:'
- $file: flux rss. normalement tu indique ici un nom de fichier, mais si tu n'a pas trop de traffic sur ton site, tu peut indiquer l'adresse web (http://...bestofmicro.....).
- $width: taille en caractères pour show_rss (par ex. 50), taille CSS pour show_rss2() (en pourcentage '80%', en pixels '400px', en caractères '30ex')
- $delay: intervalle de défilement, en millisecondes, essaye des valeurs proches de 100.
|