Aureole Posted February 3, 2008 Share Posted February 3, 2008 Ok, let's say I have a query that pulls something from a MYSQL Database and then loops through the results and let's say that there are 3 results. Now this query happens say about half way through the file. For each iteration of the loop, I want to somehow create some Javascript and put it higher up in the file. I know PHP can do this, that it can go back to an earlier point in the script and do things. I'll try keep this simple, let's say I have this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <!-- This is where I need to "go back to" and insert something for each iteration of the while loop. --> </head> <body> <?php $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); while( $row = mysql_fetch_assoc( $result ) ) { echo( '<a href="#" id="id-' . $row['id'] . '">' . $row['title'] . "</a><br>\n" ); } ?> </body> </html> I need, in the end... for the output of that (as in after the page has loaded and you view the source) for it to look a little something like this... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> $('#id-1').click(function() { $(this).css('color', 'red'); return false; } ); $('#id-2').click(function() { $(this).css('color', 'red'); return false; } ); $('#id-3').click(function() { $(this).css('color', 'red'); return false; } ); </head> <body> <a href="#" id="id-1">Title 1</a><br> <a href="#" id="id-2">Title 2</a><br> <a href="#" id="id-3">Title 3</a><br> </body> </html> Excuse the Javascript, that's jQuery syntax but you get the idea. Is this even possible? I'm pretty sure it is, but... Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/ Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 The simple solution is to move your query.... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <?php $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); while( $row = mysql_fetch_assoc( $result ) ) { echo( '<a href="#" id="id-' . $row['id'] . '">' . $row['title'] . "</a><br>\n" ); } ?> </head> <body> </body> </html> Can you explain why its not possible to it it this way? Because the other solution, could become a little complex. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456772 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 Well the file I am trying to do this in is extremely large. The while loop needs to be within the body, as for each iteration of the while loop it's outputting a lot of HTML etc. I could just add something like: <?php echo( '<script type="text/javascript">...</script>' ); ?> ...inside the while loop, but that's dirty, I want all Javascript to be within the head, that's the way it should be. When you say the other solutions could get complex, exactly how complex are we talking about here? Thanks for the quick response. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456773 Share on other sites More sharing options...
haku Posted February 3, 2008 Share Posted February 3, 2008 You have to reorganize your script a little, but it can be done. In your head, put this: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <?php $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); $iterations = mysql_num_rows($result); for($i = 0; $i < $iterations; $i++) { // This is your javascript loop. It will loop as many times // as there are rows in $result. You can use $result later // on the same way as you were using it before without // needing to do another query. } ?> </head> Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456774 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 I didn't think of it like that to be honest, just move the query higher up... as you can still do the while loop later on. I don't know how these things go over my head sometimes... Though one problem is, there is a high possibility that the ids won't be in order. The query isn't as simple as in my example, I just made it that simple to make it easier for people to try and help. There could be, 1, 5, 264, 396 etc. for the ids so that'd mean I'd have to do 2 while loops I think, instead of one while and one for. The query actually looks like this... <?php $query = "SELECT * FROM `replies` INNER JOIN `members` ON `members` . `mem_id` = `replies` . `reply_author_id` WHERE `reply_parent_id` = '{$t_id}' ORDER BY `reply_id` ASC"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456777 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 Ok, hang about... I see what your doing. I'm just going to post the solution and hopes that you might work it out. I'll be back to explain if you get stuck, its just I'm at work so kinda in a hurry. <?php ob_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <<HOLDER>> </script> </head> <body> <?php $buffer = ob_get_contents(); ob_end_clean(); $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); while( $row = mysql_fetch_assoc( $result ) ) { $js .= "\$('#id-{$row['id']}').click(function() { $(this).css('color', 'red'); return false; } );\n"; $content .= '<a href="#" id="id-' . $row['id'] . '">' . $row['title'] . "</a><br>\n" ); } echo str_replace($buffer,$js,'<<HOLDER>>'); echo $content; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456779 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 Thanks a lot, I'll have a play around with the code and see what I can do with/learn from it. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456780 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 One question, must ob_start() come before or after session_start()? Or does it not matter? Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456781 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 It doesn't really matter. It will need to come before any output though. To be honest, my solution probably isn't the best. While it should work, and its probably more efficient than running two loops, it will make you code that much more difficult to read. While its not overly complex it is quite the dirty hack. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456784 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 Well I've had a little go at implementing it and, it's not happy. I'll keep playing with it though, if only to try understand how it works. Do you think if I just do the query in the head, then have two separate while loops (one to create the Javascript and the other to output the HTML) will it really matter? It's just I'm trying to keep this as efficient as possible. I have a pagination thing going, so the most results that will ever get pulled from `replies` is 20, I can't imagine it'd be too bad but I don't want to add any significant overhead when there's no need to. EDIT: Ah, I didn't read about the output buffering would be more efficient than two while loops. Well, buggar. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456791 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 Honestly, the difference (if any, for all I know output buffering may be very inifficient) would not be worth worrying about. its not like you need to run the query twice, just the loop. You can use mysql_data_seek to rewind the reource back to the start prior to executing your second while loop. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456812 Share on other sites More sharing options...
haku Posted February 3, 2008 Share Posted February 3, 2008 Output buffering is VERY inefficient, and is really recommended to not be used. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456816 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 Output buffering is VERY inefficient, and is really recommended to not be used. And this is based on what? There is nothing in the manual. I use it quite a bit in my framework without any issue. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456819 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 Well I can't really say if it's inefficient or not, I don't really know. But if you don't think two while loops for one query is overkill then I'll just do that. Thanks for all the help though. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456836 Share on other sites More sharing options...
resago Posted February 3, 2008 Share Posted February 3, 2008 why hasn't anyone suggested the obvious? <?php $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); while( $row = mysql_fetch_assoc( $result ) ) { $js .= "\$('#id-{$row['id']}').click(function() { $(this).css('color', 'red'); return false; } );\n"; $content .= '<a href="#" id="id-' . $row['id'] . '">' . $row['title'] . "</a><br>\n" ); } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <?php echo $js; ?> </script> </head> <body> <?php echo $content; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456847 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 why hasn't anyone suggested the obvious? <?php $query = "SELECT `id`, `title` FROM `something`"; $result = mysql_query( $query ); while( $row = mysql_fetch_assoc( $result ) ) { $js .= "\$('#id-{$row['id']}').click(function() { $(this).css('color', 'red'); return false; } );\n"; $content .= '<a href="#" id="id-' . $row['id'] . '">' . $row['title'] . "</a><br>\n" ); } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> <?php echo $js; ?> </script> </head> <body> <?php echo $content; ?> </body> </html> Good point. can't see the forest for the trees. Plus its 4.17AM here. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456852 Share on other sites More sharing options...
resago Posted February 3, 2008 Share Posted February 3, 2008 well go to bed man!, the superbowl is in 7 hours! Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456889 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 As I saw thorpe say in an earlier post, most people couldn't care less about the Super Bowl. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456890 Share on other sites More sharing options...
resago Posted February 3, 2008 Share Posted February 3, 2008 yeah yeah, bloody Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456893 Share on other sites More sharing options...
Aureole Posted February 3, 2008 Author Share Posted February 3, 2008 Thanks for "suggesting the obvious" anyway resago. Quote Link to comment https://forums.phpfreaks.com/topic/89207-solved-for-each-iteration-of-a-while-loop-create-some-javascript-in-the-head/#findComment-456896 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.