Adding (news)records to news scroller
7 posts by 3 authors in: Forums > CMS Builder
Last Post: January 3, 2010 (RSS)
By kovali - February 26, 2009
I want to insert the 3 first records of a news listing to a javascript news scroller, so that the scroller will always show the latest (3) news (title + content) added into the cms. Below is my code:
TOP OF PAGE:
<?php
require_once "/home/cooxmoto/public_html/beheer/lib/viewer_functions.php";
list($nieuwsRecords, $nieuwsMetaData) = getRecords(array(
'tableName' => 'nieuws',
'limit' => '3',
'loadUploads' => '0',
'allowSearch' => '0',
));
?>
2 VARIABLES USED: <?php echo $record['title'] ?> and <?php echo $record['content'] ?>
JAVASCRIPT (where the actual content is added):
<script type="text/javascript">
var pausecontent=new Array()
pausecontent[0]='<a href="news.php">Title (1)</a>Content (1)'
pausecontent[1]='<a href="news.php">Title (2)</a>Content (2)'
pausecontent[2]='<a href="news.php">Title (3)</a>Content (3)'
</script>
The script works well with static data, but i want it to fetch the 3 latest records and display them automatically. I hope it can be done, but i'm a newbie to PHP so pls can you help ? Thanks !!
I also want to limit the amount of words (characters) of the content each time, i think this can be done with this function?:
<?php
function cutText($string, $setlength) { $length = $setlength; if($length<strlen($string)){ while (($string{$length} != " ") AND ($length > 0)) { $length--; } if ($length == 0) return substr($string, 0, $setlength); else return substr($string, 0, $length); }else return $string; }
?php>
Re: [kovali] Adding (news)records to news scroller
By ross - February 27, 2009 - edited: March 2, 2009
Thanks for posting!
We normally like to focus on our own scripts but I think I can at least get you on the right track.
It's a little hard for me to test this out as I don't have all the script for your slider but something like this should get things going:
<script type="text/javascript">
var pausecontent=new Array()
<?php foreach ($nieuwsRecords as $record): ?>
pausecontent[<?php echo $record['num'] ?>]='<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><?php echo $record['content'] ?>'
<?php endforeach; ?>
</script>
I am basically setting up a foreach loop to print out one line of javascript for every record. You may need to change a few of the variables around to get it to line up with your own site.
Let me know how you make out :)
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Re: [ross] Adding (news)records to news scroller
By kovali - February 28, 2009
I tried it but no succes yet... There are some things that i don't really understand:
Where does the variable <?php echo $record['num'] ?> come from? Do i have to create/insert it with the cms?
On the other hand, I came up with some (bad?) code like this (to increase the number of 'pausecontent []' from 0 to 1,2,3,...): It's not working yet but i think you can guess what i want to achieve:
<script type="text/javascript">
<?php
$number = -1;
$count = "$number +1";
?>
<?php foreach ($nieuwsRecords as $record): ?>
pausecontent[<?php echo $count ?>]='<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><?php echo $record['content'] ?>'
<?php endforeach; ?>
</script>
The actual url of the page is: http://www.coox-motors.be/site/indextest.php so you can check all the code if you want to.
As an example, the scroller is working ok here but with static data only.
Hope you can get me a little more on the right track :-)
Koen
Re: [kovali] Adding (news)records to news scroller
By ross - March 2, 2009
We are starting to get into a little more than I think we can really do through the forums. Would you be interested in going through consulting for this? Basically, I would just set the code up for you myself. I could also spend some time explaining how it all works.
Send me an email through consulting@interactivetools.com and we'll go from there.
Thanks!
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Re: [ross] Adding (news)records to news scroller
By kovali - March 3, 2009
After having some fun with a "Beginners PHP Manual" and a lot of Googling I managed to fix the problem myself and I'm very happy with the result, thx anyway !
Koen
Re: [kovali] Adding (news)records to news scroller
By ross - March 3, 2009
Thanks for the update! Glad to hear you got things going :). Keep us up to date with how you are making out.
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Re: [kovali] Adding (news)records to news scroller
I know that it was a while ago, but would you be willing to share the php code that you finally came up with?
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php