I have a javascript I use on my site to scroll through recent news. I'm trying to set something up so I do not have to edit the .js file manually, but instead it draws the five most recent news items from the database.
Here's the code for the ticker:
// Ticker startup
function startTicker()
{
// Define run time values
theCurrentStory = -1;
theCurrentLength = 0;
// Locate base objects
if (document.getElementById)
{
theAnchorObject = document.getElementById("tickerAnchor");
runTheTicker();
}
else
{
document.write("<style>.ticki{display:none;}.ticko{border:0px; padding:0px;}</style>");
return true;
}
}
// Ticker main run loop
function runTheTicker()
{
var myTimeout;
// Go for the next story data block
if(theCurrentLength == 0)
{
theCurrentStory++;
theCurrentStory = theCurrentStory % theItemCount;
theStorySummary = theSummaries[theCurrentStory].replace(/"/g,'"');
theTargetLink = theSiteLinks[theCurrentStory];
theAnchorObject.href = theTargetLink;
thePrefix = "<span class=\"tickerLatest\"><b>" + theLeadString + "</b></span>";
}
// Stuff the current ticker text into the anchor
theAnchorObject.innerHTML = thePrefix +
theStorySummary.substring(0,theCurrentLength) + whatWidget();
// Modify the length for the substring and define the timer
if(theCurrentLength != theStorySummary.length)
{
theCurrentLength++;
myTimeout = theCharacterTimeout;
}
else
{
theCurrentLength = 0;
myTimeout = theStoryTimeout;
}
// Call up the next cycle of the ticker
setTimeout("runTheTicker()", myTimeout);
}
function whatWidget()
{
if(theCurrentLength == theStorySummary.length)
{
return theWidgetNone;
}
if((theCurrentLength % 2) == 1)
{
return theWidgetOne;
}
else
{
return theWidgetTwo;
}
}
var theCharacterTimeout = 50;
var theStoryTimeout = 5000;
var theWidgetOne = "_";
var theWidgetTwo = "-";
var theWidgetNone = "";
var theLeadString = "LATEST NEWS: ";
var theSummaries = new Array();
var theSiteLinks = new Array();
var theItemCount = 5;
theSummaries[0] = "Headline 1";
theSiteLinks[0] = "link1";
theSummaries[1] = "Headline 2";
theSiteLinks[1] = "link2"
theSummaries[2] = "Headline 3";
theSiteLinks[2] = "link3";
theSummaries[3] = "Headline 4";
theSiteLinks[3] = "link4"
theSummaries[4] = "Headlaine 5";
theSiteLinks[4] = "link5";
startTicker();
Basically, I need to get the headline and newsID from the 5 most recent entries in the DB in the table 'news'.