while loop issue

JavaScript related questions should go here.
Post Reply
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

while loop issue

Post by sevvlor »

When I put the script in a while loop it only works on the top one, the first row that comes out of the database. Does anyone knows a solution?

 while($actie = mysql_fetch_assoc($sql_actielist)){
	?> 
	<script type="text/javascript">
	window.onload=WindowLoad;
	function WindowLoad(event){
	ActivateCountDown("tijd", <?php echo $actie['time'] - time(); ?>);
	}
	</script>
	<?php 
  echo "<tr>
			<td>".$actie['naam']."</td>
			<td><span id='tijd'></span></td>
		</tr>";
  }
Thanks

Sevvlor
Josh
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

You can't define a function more than once with the same name.

You should have the loop inside the function block to generate the code to add all of the countdowns.
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

I don't get it, doest that mean that I have to put the php while loop into the javascript box?
like this
<script type="text/javascript">
        window.onload=WindowLoad;
        function WindowLoad(event){
		<?php
		 while($actie = mysql_fetch_assoc($sql_actielist)){
		?>
        ActivateCountDown("tijd", <?php echo $actie['time'] - time(); ?>);
		<?php
		echo"
			<td>".$actie['naam']."</td>
            <td><span id='tijd'></span></td>

		";
		}
		?>
        }
        </script>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

sevvlor wrote:I don't get it, doest that mean that I have to put the php while loop into the javascript box?
Yes.

Except you can't have that html there.
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

oke, that worked only the bottom one counts down.
echo '
<script type="text/javascript">
window.onload=WindowLoad;
function WindowLoad(event){';
while($actie= mysql_fetch_assoc($sql_actielist)){
$time = $actie['time'] - time();
$tt++;
echo 'ActivateCountDown("tijd'.$tt.'", '.$time.' );';
}
echo '
}
</script>';
$sql_actielist = mysql_query("SELECT * FROM actie WHERE acnaam = '".$naam."'") or die(mysql_error());
while($actie= mysql_fetch_assoc($sql_actielist)){
$dt++;
echo "
<tr>
<td>".$actie['naam']."</td>
<td><span id='tijd".$dt."'></span></td>
</tr>";
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

Can you post the full code ? It looks like you made a mess of the echo line :?
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

$sql_actielist = mysql_query("SELECT * FROM actie WHERE acnaam = '".$naam."'") or die(mysql_error());
if(mysql_num_rows($sql_actielist) <= '0'){
echo "<tr><td colspan='2'>Je bent nu niets aan het doen.<br /></td></tr>";
} else {
echo '
<script type="text/javascript">
window.onload=WindowLoad;
function WindowLoad(event){';
while($actie= mysql_fetch_assoc($sql_actielist)){
$time = $actie['time'] - time();
$tt++;
echo 'ActivateCountDown("tijd'.$tt.'", '.$time.' );';
}
echo '
}
</script>';
$sql_actielist = mysql_query("SELECT * FROM actie WHERE acnaam = '".$naam."'") or die(mysql_error());
while($actie= mysql_fetch_assoc($sql_actielist)){
$dt++;
echo "
<tr>
<td>".$actie['naam']."</td>
<td><span id='tijd".$dt."'></span></td>
</tr>";
}
}
Edit: its inside a table but that can't be the problem. can it?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

Well you do not need to run the same query twice, and can you post the code of the ActivateCountDown function ?
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

Sure, Why not.
var _countDowncontainer=0;

var _currentSeconds=0;



function ActivateCountDown(strContainerID, initialValue) {

    _countDowncontainer = document.getElementById(strContainerID);

    

    if (!_countDowncontainer) {

        alert("count down error: container does not exist: "+strContainerID+

            "\nmake sure html element with this ID exists");

        return;

    }

    

    SetCountdownText(initialValue);

    window.setTimeout("CountDownTick()", 1000);

}



function CountDownTick() {

    if (_currentSeconds <= 0) {

        window.location.reload();

        return;

    }

    

    SetCountdownText(_currentSeconds-1);

    window.setTimeout("CountDownTick()", 1000);

}



function SetCountdownText(seconds) {

    //store:

    _currentSeconds = seconds;

    

    //get minutes:

    var minutes=parseInt(seconds/60);

    

    //shrink:

    seconds = (seconds%60);

    

    //get hours:

    var hours=parseInt(minutes/60);

    

    //shrink:

    minutes = (minutes%60);

    

    //build text:

    var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);

    

    //apply:

    _countDowncontainer.innerHTML = strText;

}



function AddZero(num) {

    return ((num >= 0)&&(num < 10))?"0"+num:num+"";

}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

The var _countDowncontainer=0; variable is global, meaning every time you call this function you overwrite the element it is applied to. you will have to either pass the element to the other two functions as a parameter, or create a function that does everything all in one function.
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

jacek wrote:The var _countDowncontainer=0; variable is global, meaning every time you call this function you overwrite the element it is applied to. you will have to either pass the element to the other two functions as a parameter, or create a function that does everything all in one function.
I have no clue how I can do that! I'm fairly new to JavaScript. It would be nice if you could explain me how to do it. :oops:

What I tried to do is this:
function ActivateCountDown(strContainerID, initialValue, _currentSeconds, _countDowncontainer) {
	_countDowncontainer = document.getElementById(strContainerID);
	
	if (!_countDowncontainer) {
		alert("count down error: container does not exist: "+strContainerID+
			"\nmake sure html element with this ID exists");
		return;
	}
	
	SetCountdownText(initialValue);
	window.setTimeout("CountDownTick()", 1000);
}

function CountDownTick() {
	if (_currentSeconds <= 0) {
		window.location.reload();
		return;
	}
	
	SetCountdownText(_currentSeconds-1);
	window.setTimeout("CountDownTick()", 1000);
}

function SetCountdownText(seconds) {
	//store:
	_currentSeconds = seconds;
	
	//get minutes:
	var minutes=parseInt(seconds/60);
	
	//shrink:
	seconds = (seconds%60);
	
	//get hours:
	var hours=parseInt(minutes/60);
	
	//shrink:
	minutes = (minutes%60);
	
	//build text:
	var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);
	
	//apply:
	_countDowncontainer.innerHTML = strText;
}

function AddZero(num) {
	return ((num >= 0)&&(num < 10))?"0"+num:num+"";
}
Adding the currentSeconds to the countdown function.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: while loop issue

Post by jacek »

You could try writing your own function based on this one that you copied from some random forum presumably ;)

It's always easier to make modifications when you understand what's going on.

With so many functions like this and using global variables it's going to be difficult to keep track of which elements have what time left.
Image
sevvlor
Posts: 22
Joined: Fri May 06, 2011 10:46 pm

Re: while loop issue

Post by sevvlor »

jacek wrote:You could try writing your own function based on this one that you copied from some random forum presumably ;)

It's always easier to make modifications when you understand what's going on.

With so many functions like this and using global variables it's going to be difficult to keep track of which elements have what time left.
True, I'll give it a shot I'll tell you when (if) it works. :lol:
Post Reply