Random Text Display

12 posts by 3 authors in: Forums > CMS Builder
Last Post: September 17, 2009   (RSS)

By gagester - September 4, 2009

Hello,

I have no idea if this is possible.

A client wants a blurb of text in the upper right hand corner of their site to randomly 'change' every 7 seconds or so. (I've played with random image displays before, and am assuming I'll find some javascript somewhere that is capable of this. I hope ? :))

Is this an option pulling in entries for this field in the Admin area? And if so... how would i even start??

Re: [gagester] Random Text Display

By Chris - September 4, 2009 - edited: September 4, 2009

Hi gagester,

The easiest way to do this would be to load in all the possible entries when the page loads. That way, you don't need to build a separate page to deal with AJAX requests (and deal with possibly having to debug asynchronous requests.)

While we don't normally offer JavaScript support, I thought I'd give this one a go for fun.

Here's some sample code which populates a JavaScript array and then continuously changes an element on the page. You'll need to replace the parts in red with your own record list variable, field, and element id.

<script type="text/javascript">
var entries = [
<?php
function jsencode($string) {
$string = preg_replace('/"/', '\\"', $string);
$string = preg_replace("/'/", "\\'", $string);
return $string;
}

function collectContentFieldAndMakeJSString($record) {
return '"' . jsencode($record['title']) . '"';
}

echo join(', ', array_map('collectContentFieldAndMakeJSString', $newsRecords));
?>
];

setInterval(function(){document.getElementById("html_element_id").innerHTML = entries[Math.floor(Math.random()*entries.length)];}, 1000)
</script>


I hope this helps! Please let us know if you have any questions or comments.
All the best,
Chris

Re: [chris] Random Text Display

By gagester - September 4, 2009

Thanks Chris!!

okay... I must be doing something wrong...

Take a look at the attached:
http://centrasodablasting.com/about-us.php
Attachments:

about-us.php 9K

Re: [gagester] Random Text Display

By Chris - September 4, 2009 - edited: September 4, 2009

Hi gagester,

It looks like you changed the name of one extra variable. I probably should have mentioned that $record in that context is supposed to stay as-is. Sorry for the confusion. :)

Change this:

function collectContentFieldAndMakeJSString($record) {
return '"' . jsencode($top_rightRecord['content']) . '"';
}


back to this:

function collectContentFieldAndMakeJSString($record) {
return '"' . jsencode($record['content']) . '"';
}


Hope this helps!
All the best,
Chris

Re: [chris] Random Text Display

By gagester - September 4, 2009

yeah, i actually caught that and changed it... but it still didn't work .

hmmmmm...

Re: [gagester] Random Text Display

By Chris - September 4, 2009 - edited: September 4, 2009

Hi gagester,

Are you sure you changed it back? I'm looking at the source of your page at http://centrasodablasting.com/about-us.php and seeing the following:

<script type="text/javascript">
var entries = [

Notice: Undefined variable: top_rightRecord in /hsphere/local/home/c257177/centrasodablasting.com/about-us.php on line 98

Notice: Undefined variable: top_rightRecord in /hsphere/local/home/c257177/centrasodablasting.com/about-us.php on line 98

Notice: Undefined variable: top_rightRecord in /hsphere/local/home/c257177/centrasodablasting.com/about-us.php on line 98

...


And line 98 in your attachment above is:

return '"' . jsencode($top_rightRecord['content']) . '"';
All the best,
Chris

Re: [chris] Random Text Display

By gagester - September 4, 2009

gee. i think so. see attached.
Attachments:

about-us_001.php 9K

Re: [gagester] Random Text Display

By Chris - September 4, 2009 - edited: September 4, 2009

Maybe it's a caching problem? Are you able to make other changes to that page?
All the best,
Chris

Re: [chris] Random Text Display

By gagester - September 16, 2009

this still isn't working. am i missing something fundamental?