While loop, timer between loops??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

While loop, timer between loops??

Post 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 :cry:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

Post 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 :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

Post by ashleek007 »

thanks pal.

Could you point me in the right direction or should i post similar in a javascript forum? :D

Ash
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 :)
Last edited by raghavan20 on Thu Aug 18, 2005 4:05 pm, edited 1 time in total.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Korto, PHP is a server side language. Javascript is much more suited for this kind of behavior.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

That's true Jcart. Although there are cases where the use of client side scripts is prohibited
Post Reply