Thursday 15 April 2010

php - How do you make javascript echo all the data whilst in a SQL while loop? -


So im trying to do a timer for my script but it only calculates the final result instead of all of them The results that come back from the query, show a countdown to the end result. Any help would be appreciated. Thanks

  & lt ;? Php $ q = $ handler- & gt; Prepare ("SELECT * FROM pending_payments WHERE user_id =? And condition =?"); $ Q- & gt; Bind para (1, $ session_id); $ Q- & gt; Baidam Parm (2, $ pending); $ Q- & gt; Executed (); If ($ q-> gtk gt; punctiquity () & gt; null) {? & Gt; & Lt; H4 class = "title" & gt; Pending payment & lt; / H4 & gt; & Lt; Div squares = "squiggly-boundary" & gt; & Lt; / Div & gt; & Lt; Table class = "table-table-index" & gt; & Lt; Thead & gt; & Lt; TR & gt; & Lt; Th & gt; # & Lt; / Th & gt; & Lt; Th & gt; Item & lt; / Th & gt; & Lt; Th & gt; Finishing up & lt; / Th & gt; & Lt; Th & gt; & Lt; / Th & gt; & Lt; / TR & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & Lt ;? Php while ($ r = $ q-> Fetch ()) {$ pending_payment_id = $ r ['id']; $ Pending_item = $ r ['item']; $ Pending_expiry = $ r ['termination']; ? & Gt; & Lt; Script type = "text / javascript" & gt; Var calculus = & lt ;? Php if ($ dateUNIX & lt; $ pending_expiry) {echo $ pending_expiry - $ dateUNIX; }? & Gt ;; Var counter = set interval (timer, 1000); // 1000 will run every 1 second function timer () {count = count - 1; If (calculation == -1) {clear interval (counter); Return; } Var seconds = Count% 60; Var minute = Math. Floor (count / 60); Var hours = Math.flur (min / 60); Min% = 60; Hours% = 60; Document.getElementById ("Pfephimer & lt ;? php echo $ r ['id']? & Gt;"). InnerHTML = hours + "hours" + minutes + "minutes"; // clock for spelling}} & lt; / Script & gt; & Lt; TR & gt; & Lt; Td class = "text" & gt; & Lt ;? Php; Echo $ pending_payment_id; ? & Gt; & Lt; / TD & gt; & Lt; Td square = "text" & gt; & Lt ;? Php echo $ pending_item? & Gt; & Lt; / Td> & Lt; Td class = "text" id = "pIfimer  & Lt; Td square = "text" & gt; & Lt; A href = "pending payment & lt ;? php echo $ r ['id'] ;? gt;" & Gt; & Lt; Button class = "BTN BTN-Success BTN-X" & gt; Proceed to pay & lt; / Button & gt; & Lt; / A & gt; & Lt; / Td> & Lt; / TR & gt; & Lt ;? Php} echo '& lt; / Tbody & gt; & Lt; / Table & gt; '; }? & Gt;  

Javascript functions are variables that execute some code, so basically the function Instead of making every row, the function you are working on is updating the function to track only the last element.

We should be clear from those places where each language works, PHP produces your HTML in the server, the HTML is parsed, the browser is executed in browser and client (client side) after that JavaScript is done.

It is being said, the best way to solve this is that you should store the data in HTML as a special data attribute, with the element of each row, retrieving it with Javascript , And then finish the one time that updates all the elements at the same time.


No comments:

Post a Comment