Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2013
    Thanked 0 Times in 0 Posts

    Running a JS Function in a PHP Query while loop

    Hello. I need to run a JS Function in a query. I did a google on it and they were saying that I should try running the function when the page is loaded, but I can't do that because the JS Function requires information from PHP.
    These are the functions:

    function countdownTimer(){
    	var settime = new Date(dateTime*1000);
    	var now = new Date();
    	var timeDiff = settime.getTime() - now.getTime();
    	if(timeDiff <= 0){
    		document.write("<p>The Event Is Ongoing!</p>");	
    	/*Time Units*/
    	var seconds = Math.floor(timeDiff / 1000);
    	var minutes = Math.floor(seconds / 60);
    	var hours = Math.floor(minutes / 60);
    	var days = Math.floor(hours / 24);
    	hours %= 24;
    	minutes %= 60;
    	seconds %= 60;
    	document.getElementById("daysBox").innerHTML = PrependZero(days) + ':';
    	document.getElementById("hoursBox").innerHTML = PrependZero(hours) + ':';
    	document.getElementById("minsBox").innerHTML = PrependZero(minutes) + ':';
    	document.getElementById("secsBox").innerHTML = PrependZero(seconds);
    	var timer = setTimeout('countdownTimer()', 1000);
    PHP Code:
    function timerTable(){
    '<table class="alignTableInTable"><tr>
    <td class="countDownBoxes" id="daysBox"> : </td>
    <td class="countDownBoxes" id="hoursBox"> : </td>
    <td class="countDownBoxes" id="minsBox"> : </td>
    <td class="countDownBoxes" id="secsBox"> : </td>

    This is my while loop:
    PHP Code:
            <script type="text/javascript">countdownTimer();
            var dateTime = <?php echo $eventTime?>;
            alert('<?php echo $eventTime?>');
    "</table> </div>";
    "<h2><p>$eventTitle</h2> <br />
                 <span class=\"CenterText\"></span> <br />
    Everything is loading fine, and only the first table row has the function ran on it. all others won't show up with the timer.

    How should I deal with this situation?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 645 Times in 635 Posts
    You can only use a given id once per web page so even your HTML is broken.

    I'd set up a field that starts at 0 and has one added to it each time around the loop. You can then add that to the end of the ids to fix the HTML problem.

    Then I'd move the JavaScript out of the loop and have it use its own loop that uses the same range of numbers.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts