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

scarer’s picture

This is just a simple one using a scrolling marquee:


//Author:Sarah Vardy
//Feel free to modify this however you want.

$news = array();
$nidArray = array();
$bodyStore = array();
$imageLocation = array();
$count = 0;

$result = db_query("SELECT n.uid, n.nid, nr.title, nr.body, f.filepath FROM {node} n JOIN {node_revisions} nr ON n.nid = nr.nid INNER JOIN {image_attach} ia ON n.nid = ia.nid JOIN {files} f ON ia.iid = f.nid WHERE type = 'news_article' ORDER BY created DESC LIMIT 3");
  

   while ($nodeData = db_fetch_object($result)) {
      $news[$count] = $nodeData->title;
	  $nidArray[$count] = $nodeData->nid;
	  $teaser = substr($nodeData->body, 0, 200);
	  $bodyStore[$count] = $teaser;
	  $imageLocation[$count] = $nodeData->filepath;
      $count++;
   }

echo '<marquee scrollamount="2" onmouseover="this.scrollAmount=0" onmouseout="this.scrollAmount=2" direction="up">';


for ($i = 0; $i < count($news);$i++){
      echo '<a href="node/' . $nidArray[$i] . '"><img src="http://mysite.com/' . $imageLocation[$i] . '" width="50px"/></a><br />';
      echo '<p><a href="node/' . $nidArray[$i] . '" >' .  $news[$i]. '</a><br />' . $bodyStore[$i] . '</p><p>&nbsp;</p><p>&nbsp;</p></p>';
} 

echo '</marquee>';


scarer’s picture

the only thing i can't figure out is why it's printing twice.

does anyone have any ideas?

scarer’s picture

..

scarer’s picture

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:


//Author:Sarah Vardy
//Feel free to modify this however you want.

/* news article query to retrieve 2 most recently posted news articles */
	$result = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'news_article' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 3");
  

   	while ($nodeNews = db_fetch_object($result)) {
		$news.= '<p><a href="node/' . $nodeNews->nid . '">' . $nodeNews->title . '</a><br /><a href="node/' . $nodeNews->nid . '"><img src="' . $nodeNews->filepath . '" width="50px"/></a><br />' . substr($nodeNews->body, 0, 200) . '<br /><a href="node/' . $nodeNews->nid . '">read more</a></p><p>&nbsp;</p>';
   	}

/* /news article query to retrieve 2 most recently posted news articles */

/* book launch query to retrieve 2 most recently posted book launches */
	$result2 = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'book_launch' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 2");
  

   	while ($nodeBookLaunch = db_fetch_object($result2)) {
		$bookLaunches.= '<p><a href="node/' . $nodeBookLaunch->nid . '">' . $nodeBookLaunch->title . '</a><br /><a href="node/' . $nodeBookLaunch->nid . '"><img src="' . $nodeBookLaunch->filepath . '" width="50px"/></a><br />' . substr($nodeBookLaunch->body, 0, 200) . '<br /><a href="node/' . $nodeBookLaunch->nid . '">read more</a></p><p>&nbsp;</p>';
   	}
/* /book launch query to retrieve 2 most recently posted book launches */

/* conferences query to retrieve 2 most recently posted conferences */
	$result3 = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'conferences' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 2");
  

   	while ($nodeConferences = db_fetch_object($result3)) {
		$conferences.= '<p><a href="node/' . $nodeConferences->nid . '">' . $nodeConferences->title . '</a><br /><a href="node/' . $nodeConferences->nid . '"><img src="' . $nodeConferences->filepath . '" width="50px"/></a><br />' . substr($nodeConferences->body, 0, 200) . '<br /><a href="node/' . $nodeConferences->nid . '">read more</a></p><p>&nbsp;</p>';
   	}
/* /conferences query to retrieve 2 most recently posted conferences */

/* courses query to retrieve 2 most recently posted courses */
	$result4 = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'course' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 2");
  

   	while ($nodeCourses = db_fetch_object($result4)) {
		$course.= '<p><a href="node/' . $nodeCourses->nid . '">' . $nodeCourses->title . '</a><br /><a href="node/' . $nodeCourses->nid . '"><img src="' . $nodeCourses->filepath . '" width="50px"/></a><br />' . substr($nodeCourses->body, 0, 200) . '<br /><a href="node/' . $nodeCourses->nid . '">read more</a></p><p>&nbsp;</p>';
   	}
/* /courses query to retrieve 2 most recently posted courses */

/* seminars query to retrieve 2 most recently posted seminars */
	$resultSem = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'seminars' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 2");
  

   	while ($nodeSem = db_fetch_object($resultSem)) {
		$seminars.= '<p><a href="node/' . $nodeSem->nid . '">' . $nodeSem->title . '</a><br /><a href="node/' . $nodeSem->nid . '"><img src="' . $nodeSem->filepath . '" width="50px"/></a><br />' . substr($nodeSem->body, 0, 200) . '<br /><a href="node/' . $nodeSem->nid . '">read more</a></p><p>&nbsp;</p>';
   	}
/* /seminars query to retrieve 2 most recently posted seminars */

/* workshops query to retrieve 2 most recently posted workshops */
	$result6 = db_query("SELECT node.title, node.nid, node_revisions.body, files.filepath FROM node JOIN node_revisions ON node.nid = node_revisions.nid INNER JOIN image_attach ON node_revisions.nid = image_attach.nid INNER JOIN files ON image_attach.iid = files.nid WHERE node.type = 'workshop' AND files.filename = 'thumbnail' ORDER BY node.created DESC LIMIT 2");
  

   	while ($nodeWorkshops = db_fetch_object($result6)) {
		$workshop.= '<p><a href="node/' . $nodeWorkshops->nid . '">' . $nodeWorkshops->title . '</a><br /><a href="node/' . $nodeWorkshops->nid . '"><img src="' . $nodeWorkshops->filepath . '" width="50px"/></a><br />' . substr($nodeWorkshops->body, 0, 200) . '<br /><a href="node/' . $nodeWorkshops->nid . '">read more</a></p><p>&nbsp;</p>';
   	}
/* /workshops query to retrieve 2 most recently posted workshops */

echo '<marquee scrollamount="2" onmouseover="this.scrollAmount=0" onmouseout="this.scrollAmount=2" direction="up">';

echo $news;
echo $bookLaunches;
echo $conferences;
echo $course;
echo $seminars;
echo $workshop;

echo '</marquee>';


Please help!

Thanks,

Sarah

nevets’s picture

I would make sure the query is returning something. For example after the call to db_query() you might add something like

drupal_set_message("Seminar query returned " . db_num_rows(resultSem) . " rows");
scarer’s picture

there's something happening with the last bit of the query in the last two results. have shortened query and it works again.