Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotating Testimonial Help 1

Status
Not open for further replies.

jomuga

Programmer
Sep 21, 2005
15
UG
I would like to implement the equivalent of an adRotator (in ASP) using php. I would like visitors on my site to see a different customer testimonial every time they visit and a different testimonial as they browse through the different pages on the website. However the best implementation would be for the testimonials to keep changing say after every 5 seconds. I intend to store the testimonials in an xml file. Any help will be appreciated.
 
if you wish them to change every five seconds then you will need either to load all the testimonials into each page and use js to rotate them or make AJAX calls.

if you want them to rotate each page refresh then you need no js.

the process is straightforward, but I would store the testimonials in a database or a csv. database is easiest because you don't have to read the entire set of testimonials into an array each time.

Code:
function deliverTestimonial(){
	//start the session if not already started
	if (session_name() == '') {
		session_start();
	}
	$cur = isset($_SESSION['currentTestimonial']) ? $_SESSION['curentTestimonial'] :'';

	//count number of testimonials
	$num = mysql_result(mysql_query("select count(*) from table where id <> $cur"), 0,0);
	$random = rand(0, $num-1); //create random integer
	
	//get testimonial
	$result = mysql_query( 	"Select
								id, testimonial
							From table
							where id <> $cur
							limit 1
							offset $random");
	$row = mysql_fetch_assoc($result);
	//assign new current id to session
	$_SESSION['currentTestimonial'] = $row['id'];
	return $row['testimonial'];
}
 
on reflection you could simplify the code even more by pushing the randomisation into mysql (not possible with xml/csv of course). Code as follows:

Code:
function deliverTestimonial(){
	//start the session if not already started
	if (session_name() == '') {
		session_start();
	}
	$cur = isset($_SESSION['currentTestimonial']) ? $_SESSION['curentTestimonial'] :'';

	/*
	//count number of testimonials
	$num = mysql_result(mysql_query("select count(*) from table where id <> $cur"), 0,0);
	$random = rand(0, $num-1); //create random integer
	*/
	//get testimonial
	$result = mysql_query( 	"	Select
									id, testimonial
								From table
								where id <> $cur
								order by rand()
								limit 1");
	$row = mysql_fetch_assoc($result);
	//assign new current id to session
	$_SESSION['currentTestimonial'] = $row['id'];
	return $row['testimonial'];
}
 
Thanks jpadie, will update you tomorrow on how the code works. I am gonna use mysql database.
 
have fun. here is an ajax implementation for you to try as well

Code:
<head>
<script language="javascript1.5">
var xObj;
var uri = "rotateTestimonial.php";	//relative path
var tObj, testimonial 
var timeout = 10; //number of seconds

window.onload = function () {
		//need to call this after the window has loaded
		testimonial = document.getElementById("testimonial");
		//get content for the initial display
		refreshTestimonial();
		tObj = window.setInterval(refreshTestimonial, (timeout * 1000));
	}
function init_transport() {
	var A;
	
	var o = new Array(
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP');
		
	for (var i = 0; i < o.length; i++) {
		try {
			A = new ActiveXObject(o[i]);
		} catch (e) {
			A = null;
		}
	}
	
	if(!A && typeof XMLHttpRequest != "undefined") {
		A = new XMLHttpRequest();
	}
	xObj = A;
}
function refreshTestimonial(){
	if (!xObj) {
		init_transport();
	}
	try {
		xObj.open("GET", uri, true);
	} catch (e) {
		return;
	}
	
	xObj.onreadystatechange = function() {
			if (xObj.readyState != 4) {
				return;
			} else {
				testimonial.innerHTML = xObj.responseText;
			}
		}
	
	xObj.send(null);	
}
</script>
<style type="text/css">
#testimonial { width: 120px; height: 200px; border: red thin solid; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:9px; color: #006699; background-color:#FFCCFF;}

</style>
</head>
<body>
<div id="testimonial">
</div>

Code:
echo deliverTestimonial();
function deliverTestimonial(){
    //start the session if not already started
    if (session_name() == '') {
        session_start();
    }
    $cur = isset($_SESSION['currentTestimonial']) ? $_SESSION['curentTestimonial'] :'';

    //count number of testimonials
    $num = mysql_result(mysql_query("select count(*) from table where id <> $cur"), 0,0);
    $random = rand(0, $num-1); //create random integer
    
    //get testimonial
    $result = mysql_query(     "Select
                                id, testimonial
                            From table
                            where id <> $cur
                            limit 1
                            offset $random");
    $row = mysql_fetch_assoc($result);
    //assign new current id to session
    $_SESSION['currentTestimonial'] = $row['id'];
    return $row['testimonial'];
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top