javascript within php loop
Is it possible to run javascript code within a php loop? the javascript works perfect the problem is that it is currently not executing more than once.
while(. ) < $l=$l+1; $linha="#x".$l; $linha2="x".$l; ?> >
First, you can’t «run» javascript code within a PHP loop because PHP is server side and javascript is client side. You can generate JS from PHP — is this what you mean? Secondly, please show what you are expecting as output, and what you are getting. Also, please show us what the continue condition of the while loop is in PHP.
OK, please show us the table, what you want to do with it, and what you are getting at the moment. Please edit the question with the HTML that is being generated at the moment, and what you would like it to be.
what i am getting is the additional information from the first guy for every one. I’m sorry but what is the difference between javascript and JS? I would like to try with JS
2 Answers 2
Yes you can output javascript within a PHP loop, to be executed by the browser, when the page loads.
The problem here is that you’ve got tons of variables colliding. You need to encapsulate each output of that script tag in order to keep that from happening. Here’s one suggestion:
(function($) < var mensagem = "", , nextMsgOptions = < msg: mensagem, side: "bottomMiddle", CSSClass: "nextMsg-LightTheme" >; $(document).ready(function() < $(id).click(function()< $(id).nextMsg(nextMsgOptions); >); >); >)(jQuery);
This keeps each $linha and $mensagem variable scoped away from each other for each iteration of the loop. What I think is/was happening is in your old code, you’d set $linha to some variable, and output id = however many times your loop executed. When $(document).ready() executed for each output, $linha had already been interpreted to be the last value that you loop output. This caused document.ready to attach an event N times (N = number of iterations of your while loop) to the same DOM element (whichever the last iteration of your while loop output $linha to be). With the above snippet, it keeps each id and $linha variable scoped away and private from each other, so you shouldn’t have to worry about collisions.
I realize that explanation is kind of convoluted and might be hard to grok; but javascript interpretation/execution/scoping has special rules that aren’t incredibly simple to convey without examples.
Pass PHP variable from while loop to javascript function
Before anyone says «try searching», I have — I realize this is probably a simple solution, but I just can’t get it to work. This is my first venture into AJAX — and my knowledge of javascript is slightly above a 1st grader. I know I need to get up to speed on it. I’m trying to build a nested task manager, using AJAX. I’m getting jammed up on the AJAX implementation. the list works well otherwise. The basic concept should output like this:
Goal: Create a nested task list Milestone: Build the basic setup — completed 2/11/12 Task: Design the database — completed 2/11/12
Task: Design the php stuff — completed 2/11/12
As you click on the link, it runs my MySQL update to show the item being completed. I have three nested while loops (one for goals, one for milestones and one for tasks). They are near identical. Here’s the most deeply nested loop:
$query_tasks = mysql_query("SELECT * FROM task WHERE task_gid = '$task_gid' AND task_mid = '$task_mid' AND task_tid IS NOT NULL"); $t_numrows = mysql_num_rows($query_tasks); if($t_numrows > 0) < while( $get_task = mysql_fetch_array($query_tasks))< $task_id = $get_task['task_id']; $task_goal = $get_task['task_goal']; $task_due = $task_task['task_due']; $task_due = date("m/d/Y", $task_due); $task_complete = $get_task['task_complete']; $task_complete_date = $get_task['task_complete_date']; ?>
I’ve got it to work for one link (if I click on the last rendered link, it works as desired — but no other links do). I’ve tried calling the javascript in the head (my preferred method) as well as calling it each time the loop passes (not sure that’s a good idea, but whatever). My thought is to use the variable from the while loop for each task in the javascript function. I’ve tried placing the ajax script into a javascript function and calling it on the onClick behavior for the link, but no luck. Thoughts?
Here is how it can be done (we are using a element so that the menu easily can be styled with CSS later): Example
Welcome to my home page!
Some text.
Some more text.
Run example » Example 3 Assume we have a file called «vars.php», with some variables defined: Then, if we include the «vars.php» file, the variables can be used in the calling file: Example
Welcome to my home page!
Run example » PHP include vs. require The statement is also used to include a file into the PHP code. However, there is one big difference between include and require; when a file is included with the statement and PHP cannot find it, the script will continue to execute: Example
Welcome to my home page!
Run example » If we do the same example using the statement, the echo statement will not be executed because the script execution dies after the statement returned a fatal error: Example
Welcome to my home page!
Run example » Use when the file is required by the application.
In JavaScript do while loop executes a statement block once and then repeats the execution until a specified condition evaluates to false.
In while loop, the given condition is tested at the beginning, i.e. before executing any of the statements within the while loop. In case of do while loop the condition is tested after execution of the statements within the while loop. This means that do-while would execute it’s statements at least once, even if the condition fails for the first time itself.
Pictorial Presentation:
The following web document calculates the sum of even numbers between 0 to 10. The do while loop starts with x = 0 and runs until it equals to 10. If the remainder of x/2 is equals to 0 we add x with y and after completion of the loop, y returns the sum of even numbers.
JavaScript : do while statement
The do while loop calculate the sum of even numbers between 0 to 10.
var x = 1; var y = 0; var z = 0; document.getElementById("result").innerHTML = "List of numbers : "; do < z = x % 2; if (z === 0) < var newParagraph1 = document.createElement("p"); var newText1 = document.createTextNode(x); newParagraph1.appendChild(newText1); document.body.appendChild(newParagraph1); y=y+x; >x++; > while (x
View the example in the browser
Practice the example online
See the Pen do-while-1 by w3resource (@w3resource) on CodePen.
Dowhile - JavaScript, dowhile The dowhile statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once. Try it Syntax do statement while (condition); statement
While Loop using PHP with a MySQL server
mysql_fetch_array() only retrieves a single row from the database. You need to call it inside your while loop to retrieve all rows. The $looping increment is unnecessary here, since mysql_fetch_array() returns false when no more rows are available.
while ($row = mysql_fetch_array($result))
while ($row = mysql_fetch_assoc($result)) < // print $row; >
while ($row = mysql_fetch_array($result))
Java while loop with Examples, Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. While loop in Java comes into use when we need to repeatedly execute a block of statements. The while loop is considered …
JavaScript | Statements
The programming instructions written in a program in a programming language are known as statements.
Order of execution of Statements is the same as they are written.
- Semicolons:
- Semicolons separate JavaScript statements.
- Semicolon marks the end of a statement in javascript. Example:
How to print a page using javascript for every php while loop?
But this is not working. Please help me. I want to print the page similiar to Print But this should be automatic as while condition will bring more result in looping process Is there any way to send the page directly to printer one by one from loop or other method. Please help me by giving an example
Do you mean print to the screen, or print to the printer? Also, if this code is copied directly, you're missing a " after your SQL statement.
So you want to print row one on to a sheet of paper, rows one and two on a second sheet, rows one, two and three on a third … and so on?! That sounds insane.
"But this is not working" - why not? Errors? Unexpected behavior? Please explain step by step what you want to achieve, what you expect and what really happens.
You can't mix your PHP and JavaScript this way. PHP is server-side, JavaScript is client-side. Furthermore, you don't really have control over the print process. All you can do is launch the print dialog, and sometimes even that won't work. You're better off outputting your whole document, and then print.
2 Answers 2
Could you provide more details on what you're trying to do. PHP is server side while javascript is client side. When php is done producing the page (loops or not) you still have just 1 page rendered. I am not sure what javascript:window.print() in the loop gives you.
If you want to trigger the page to print after the whole thing is rendered, you can put
at the bottom of your page but not in your loop.
If I add this at bottom of the page, then how the loop will work. Means what will be the mean of loop. Is there any way to send the page directly to printer one by one from loop or other method. Please help me by giving an example
That's exactly my question. What are you trying to do? PHP is server side script that runs first. Then javascript (client side) when it gets to your browser. You can't have a piece of js code run inside your loop on the server and send to printer. What's gonna happen is what @quentin mentioned - insanity, although in a slightly different way. You will have X number if window.print() statements written out to your page by php and then executed one after another when you get to the browser. What are you trying to achieve?
@IndianGirl I'm not sure I'm understanding your question, but are you trying to use JavaScript to call the browser's print function and want to do so one page at a time?
@IndianGirl If so, the method you are trying in the OP is not going to work. You need to use AJAX (jquery is easy enough, I would recommend looking into it) to make calls to the PHP script and have it return one page at a time (pass it some get or post vars to keep track of which page/row you're on). Then call print and after that you can run another AJAX call to the PHP script for the next page, and so on. I'm not sure how you'll handle the time between prints as you will need to account for the user taking time to confirm or reject the print dialog.