troubleshooting Question

swapping out text from php loop with js

Avatar of helpchrisplz
helpchrisplz asked on
JavaScriptjQuery
22 Comments2 Solutions641 ViewsLast Modified:
hi all am new to javascript and need some help.

what am trying to make is a testimonial text slider / updater so i can show some nice customer feedback on a homepage for people to see how good we are rated.

the website has some guestbook submissions so i am getting them out of the database. I guess i want to loop through them and update the paragraph tag every 8 seconds then repeat this so it loops over all post and then restarts the loop over and over.

this is the code that i have managed to piece together by searching the internet.

<?php
// ADD PHP VARABLE TO ARRAY
$text= array();
	
	foreach($guestbook as $post) {
		$text[] = $post;
	}
?>

<script type="text/javascript">
var fromPHP=<? echo json_encode($text); ?>;

(function myLoop (i) {          
	setTimeout(function () {      
		for (i=0; i<fromPHP.length; i++) {

			var a= fromPHP[i];
			// update html with new text    
			 $("p").html(a);
			}

			if (--i) myLoop(i);      //  decrement i and call myLoop again if i > 0
   }, 8000)})(999999);               //  pass the number of iterations as an argument
</script>

i haven't tested my code yet but i want to know if this looks ok? and how i could animate the change in text (fade in) and is it ok to have a never ending loop, what should i load it in? document ready?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 22 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 22 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros