Jump to content

[SOLVED] For each iteration of a while loop, create some Javascript in the HEAD...


Aureole

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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"; ?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.