Jump to content

Automatically changing links after a few seconds


talk2toyin

Recommended Posts

I'd like to have a script that displays results I extracted from the database. But it should display these results one at a time, ie. One result displays for a few seconds and then it changes to another and continue looping through the results. Help would be really appreciated.

You could do an ajax request to get the new link, one request by link. Or if the links in the database are not changing during the process, retrieve all the links at the beginning and replace the links using javascript or some javascript framework like jquery.

Thanks for the reply but, I don't really know Ajax. A good example of what I want can be seen on goal.com, where u have the recent news displayed in photos and these headlines are being displayed in a larger frame, one after the other. Can it ever be done with php?

here's an example

 

<?php
   include("db_inc.php");
   $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE);
   $sql = "SELECT CONCAT(firstname,' ',lastname) as name
	    FROM employees";
   $res = $db->query($sql);
   $total = $res->num_rows;
   $data=array();
   while ($row = $res->fetch_row()) {
    $data[] = $row[0];
   }
   $arrayData = '"' . join('","', $data) . '"';   // data for JS array
?>

<html>
<head>
<meta name="generator" content="PhpED Version 8.1 (Build 8115)">
<title>sample</title>
<meta name="author" content="Barand">
<link rel="shortcut icon"  href="">
<script type="text/javascript">
   var count = 0;
   var limit = <?php echo $total ?>;
   var data = Array(<?php echo $arrayData ?>);

   function displayNext()
   {
    var el = document.getElementById("demo");
    el.innerHTML = data[count];
    ++count;
    if (count==limit) count = 0;
    setTimeout("displayNext()", 2000);
   }

   onload = function() {displayNext();}
</script>
</head>
<body>
<div id="demo" style="width: 200px; height: 60px; border: 1px solid gray; padding: 20px 5px;">
watch this space
</div>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.