HI i would be more than thankful if anybody can tell me why this don't work,
the problem is that the JS counter won't work when there are more then 1 entrys in mysql.
With one entry the script runs fine.
Heres the code:

<table>
<?
//Connect to mysql server
	$link = mysql_connect("database connect works","...","...");
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	//Select database
	$db = mysql_select_db("niisama");
	if(!$db) {
		die("Unable to select database");
	}
$asjad = mysql_query("SELECT aegs,aegm,aegh,aegp,aegk,aega FROM tehingud;");
	if (mysql_num_rows($asjad) == 0) break;
		while ($asi = mysql_fetch_array($asjad))
		{?>
		<table width="200" border="0">
  <tr>
   <tr>
    <th scope="row">Aega Jäänud</th>
    <td>
    
    
	<form name="counter"><input type="text" size="8" 
name="d2"></form> <script> 
<!-- 
// 
		
<?
	$aegs=60-date("s" ,time());
	$aegm=$asi[aegm]-date("i" ,time())-1;
	$pkuus=$asi[aegp]-date("d" ,time());
	if($pkuus<0)
	{
	$pkuus=date("t" ,time())-date("d" ,time())+$asi[aegp];
	}
	
	$aegh=$pkuus*24+$asi[aegh]-date("H" ,time());
	if($aegm<0)
	{
	$aegh-=1;
	$aegm=60+$aegm;
	}
	$aegp=$asi[aegm];
	$aegk=$asi[aegm];
	$aega=$asi[aegm];
	?>
 var milisec=0;
 var seconds= <?=$aegs?>; 
 var minutes= <?=$aegm?>;
 var hours= <?=$aegh?>;
 document.counter.d2.value='30' 

function display(){ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    seconds=59 
    minutes-=1 
 } 
  if (minutes<=-1){ 
    minutes=59 
    hours-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1;
    document.counter.d2.value=hours+":"+minutes+":"+seconds;
    setTimeout("display()",100) 
} 
display() 
--> 
</script> </td>
</td>
  </tr>
</table>
<br />
<?
}
?>
</table>

Recommended Answers

All 5 Replies

var seconds= <?=$aegs?>;

Try with double quotes around.
ie., var seconds= "<?=$aegs?>"; Do the same with the rest of javascript variables.

You have the javascript within the PHP loop.

You need to take it out so that it loads only once.

Hm upper quotes didn't work,
i'm not getting the second idea pretty well,
how am i supposed to take the script out and doesent it only load 1 counter then?

No one?

A solution has already been suggested to you. Move the dynamically generated Javascript out of the looping construct. To understand more, take a look at the generated source code by navigating to 'View' -> 'View Source' in Firefox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.