Page 1 of 1
While loop, timer between loops??
Posted: Mon Aug 15, 2005 7:40 am
by ashleek007
Hi,
Ive got a while loop, which displays news stories from a database. At the moment it only displays the last news story, think this is because the loop goes through all the news stories.
What i want is a piece of code that allows the while looop to be manipulated so that the news stories loop around, and change after 3 seconds?
Is this possible?, and if so how do i go about it?
Thanks
Ash

Posted: Mon Aug 15, 2005 7:45 am
by feyd
loop around?
well.. you're basically talking about a meta-refresh or similar idea. As for looping around... if I understand correctly, you're talking about a "circular" array (list). Which can be faked by selecting a set of articles, if the amount of articles is less than the maximum (and you aren't repeating any), you select some of the newest ones using the remainder needed. Watch out for repeated articles though.
Posted: Mon Aug 15, 2005 8:01 am
by ashleek007
let me try and rephrase that!:D
all news stories are obatined from a DB using a while loop and stored in an array:-
while($row1 = mysql_fetch_array($result,MYSQL_ASSOC))
{
$values[] = $row1[newstitle];
$desc[] = $row1[newsdescription];
$pic[] = $row1[newspicture];
}
so there i have all the news stories and pictures in variables.
i can then display the values of the array:-
echo $values[0];
echo $desc[0];
but what i want is to display the contents of the array periodically. So first to be displayed would be:-
echo $values[
0];
echo $desc[
0];
then after three seconds the red value is incremented by 1. This will then display the second news story.....and so on!
until it reaches the final value in the array, at this point the value is reset to zero and the process starts again!
Hope this is a better explanation of what i want?!
Thanks
Ash

Posted: Mon Aug 15, 2005 8:04 am
by feyd
you could do that all in Javascript. Basically, you output all the information and using a timer and visibility control you can hide and display each article as time passes.
Posted: Mon Aug 15, 2005 8:16 am
by ashleek007
thanks pal.
Could you point me in the right direction or should i post similar in a javascript forum?
Ash
Posted: Mon Aug 15, 2005 8:33 am
by feyd
the basics are: you use
Window.setInterval() to create your timing functionality. You use
object.style.display to control the visibility.
You can simply store the limit and the active position in the queue in a variable.
Posted: Thu Aug 18, 2005 1:34 pm
by ashleek007
hey, im sorry, but im in a pickle!
i dont know where to start, could i use a countdown timer and when the value hits zero a varibale is incremented by one, whilst the timer is reset?!
thanks again
Ash
Posted: Thu Aug 18, 2005 2:28 pm
by raghavan20
I was thinking of two options:
the page refreshing should not be seen by the user
1. iframe
2. ajax
hi, ashleek007
1. you have to include
//news.php
Code: Select all
<iframe src = "contents.php" width='0' height='0'></iframe>
<div id='news'></div>
in the page you want to display
//create contents.php where in javascript have this
<div id="contents">
Code: Select all
<?
//get one record
$query = "";
$result = mysql_query($query);
if (is_resource($result)){
while($row = mysql_fetch_row($result)){
if ($i++ == $_SESSION["pointer"]){
//display the array, row
//store current record pointer in $_SESSION["pointer"]; increment the pointer each time and use modulus to return to first record when you reach the last record
$_SESSION["pointer"] = ($_SESSION["pointer"] + 1)%mysql_num_rows($result)
}
}
}
?>
Code: Select all
</div>
<script>
setTimeout("sendContent()","2000);
function sendContent(){
parent.loadContents(document.getElementById("contents").innerHTML);
window.location="contents.php";,
}
</script>
//finally at news.php
<script>
function loadContents(content){
document.getElementById("news").innerHTML = content;
}
</script>
2. you can use XMLHTTP to get news responses from a php file. Go through an example chat application in the code snippets forum.
hope this helps

Posted: Thu Aug 18, 2005 3:52 pm
by korto
What about the sleep and set_time_limit php functions?
You could use sleep within an loop and loop every given seconds and perhaps stop the loop with set_time_limit (however I am not sure about that one as it would not terminate gracefully unless you provide).
Posted: Thu Aug 18, 2005 5:14 pm
by John Cartwright
Korto, PHP is a server side language. Javascript is much more suited for this kind of behavior.
Posted: Thu Aug 18, 2005 5:21 pm
by feyd
neither AJAX, an iframe, nor sleep() is needed for this.
Here's an (untested) example:
Code: Select all
<?php
$records = array(
'some text',
'some more text',
'even more text',
);
$hidden = '';
for($i = 2; $i < count($records); $i++)
{
$hidden .= (empty($hidden) ? '' : ', ') . 'row' . $i;
}
if(!empty($hidden)) $hidden .= '
{
display: none;
}';
$rows = array();
foreach($records as $row => $text)
{
$rows[] = '<div id="row' . ($row+1) . '">' . $text . '</div>';
}
$rows = implode("\n",$rows);
echo <<<STOP
<html>
<head>
<style>
{$hidden}
</style>
<script language="Javascript">
function nextArticle()
{
var count = 0;
var visible = null;
var temp = null;
for(;;)
{
temp = document.getElementById('row'+(count+1));
if(temp)
{
count++;
visible = ((!visible && temp.style.display == 'block') ? count : null);
}
else
{
break;
}
}
if(count > 1)
{
temp = document.getElementById('row'+visible);
temp.style.display = 'none';
temp = document.getElementById('row'+((visible+1) % count));
temp.style.display = 'block';
}
}
</script>
</head>
<body>
{$rows}
</body>
<script language="Javascript">
var timer = window.setInterval('nextArticle()',5000);
</script>
</html>
STOP;
?>
will output
Code: Select all
<html>
<head>
<style>
#row2, #row3
{
display: none;
}
</style>
<script language="Javascript">
function nextArticle()
{
var count = 0;
var visible = null;
var temp = null;
for(;;)
{
temp = document.getElementById('row'+(count+1));
if(temp)
{
count++;
visible = ((!visible && temp.style.display == 'block') ? count : null);
}
else
{
break;
}
}
if(count > 1)
{
temp = document.getElementById('row'+visible);
temp.style.display = 'none';
temp = document.getElementById('row'+((visible+1) % count));
temp.style.display = 'block';
}
}
</script>
</head>
<body>
<div id="row1">some text</div>
<div id="row2">some more text</div>
<div id="row3">even more text</div>
</body>
<script language="Javascript">
var timer = window.setInterval('nextArticle()',5000);
</script>
</html>
Posted: Thu Aug 18, 2005 5:25 pm
by korto
That's true Jcart. Although there are cases where the use of client side scripts is prohibited