By scarer on
Hi,
I'm trying to create a news ticker for my drupal site.
Here's the code I have so far in a block.
//Author:Sarah Vardy
//Feel free to modify this however you want.
$news = array();
$count = 0;
$result = db_query("SELECT uid, nid, title FROM node WHERE type = 'news_article' ORDER BY created DESC LIMIT 3");
while ($nodeData = db_fetch_object($result)) {
$news[$count] = $nodeData->title;
$count++;
}
$outputJava.= '
<script language="javascript">
var numNews,counter;
// count number of news
numNews=news.length;
// initialize news counter
counter=0;
createNewsDiv=function(){
// create news containing <div> element ';
$outputJava .= "var newsDiv=document.createElement('div');
newsDiv.id='news';
// insert news <div> into document structure
document.body.appendChild(newsDiv);
}
rotateNews=function(){
// get news containing <div>
var newsDiv=document.getElementById('news');
if(!newsDiv){return;}
// create new <div> element
var div=document.createElement('div');
div.id='news';
// create paragraph for each news line
var p=document.createElement('p');
// style <p> element
p.style.fontFamily='Verdana';
p.style.fontSize='11px';
p.style.fontWeight='bold';
p.style.color='#c00';
if(counter==numNews){counter=0;}
p.appendChild(document.createTextNode(news[counter]));
// insert paragraph into <div> news
div.appendChild(p);
// replace old <div> node with new <div> node
newsDiv.parentNode.replaceChild(div,newsDiv);
counter++;
// rotate news every 10 seconds
setTimeout('rotateNews()',10*1000);
}
// execute functions when page is loaded
window.onload=function(){
if(document.getElementById){
createNewsDiv();
rotateNews();
}
}
</script>";
$outputJava .='
<style type="text/css">
/*define style for news container*/
#news {
background: #eee;
padding: 2px 0px 2px 10px;
border: 1px solid #000;
}
</style>
';
print $outputJava;
Any help would be greatly appreciated.
Thanks,
Sarah
Comments
simple on ewith scrolling marquee to put in block
This is just a simple one using a scrolling marquee:
printing twice
the only thing i can't figure out is why it's printing twice.
does anyone have any ideas?
it was a faulty query
..
last two results not printing
I've created a ticker but the last two sets of results don't seem to be printing.
This code is installed in a block:
Please help!
Thanks,
Sarah
I would make sure the query
I would make sure the query is returning something. For example after the call to db_query() you might add something like
thanks for the tip
there's something happening with the last bit of the query in the last two results. have shortened query and it works again.