0

I am wanting to create a PHP loop to get all of the ids in a database table and them utilize them in an .append function. Basically what I want to happen is for every loop through the database, I want the jquery.append to run and change its #div labeling to be from the field div-id in the database

For example;

$('#div-id').append('HELLO CONTENT');

I know I can use an .each, but that doesn't take care of the PHP part. I can do a while on the PHP, but that doesn't take care of the Jquery part. Basically how I see it, they are creating their own loops and not working together.

2
  • I'm not really clear on what you're trying to do, but as PHP runs on the server side, it's going to execute before any JS, which runs on the client. Commented Oct 13, 2011 at 11:41
  • I am attempting to target specific divs on my page, and the id's for those divs are stored in the database. Commented Oct 13, 2011 at 11:47

2 Answers 2

3

You can do it in a <script> tag output by the PHP page. I am assuming standard MySQL, should be fairly easy to adapt this to another database.

<!-- rest of page -->
<script type="text/javascript">
<?php

  // Get all the results from the DB
  $result = mysql_query("SELECT `div-id`, `content` FROM `table` WHERE `some_field` = 'some value'");
  // loop the db results
  while ($row = mysql_fecth_assoc($result)) echo "$('#{$row['div-id']}').append('{$row['content']}');\n";

?>
</script>
<!-- rest of page -->

Should output something like:

<!-- rest of page -->
<script type="text/javascript">
  $('#div-id1').append('HELLO CONTENT1');
  $('#div-id2').append('HELLO CONTENT2');
  $('#div-id3').append('HELLO CONTENT3');
</script>
<!-- rest of page -->

...so you're doing the loop with PHP, and just outputting lines of JS, which will execute one after the other.

Alternatively, you could loop $result in PHP and create one large array, then json_encode() it and loop it in JS. Your choice.

Sign up to request clarification or add additional context in comments.

1 Comment

I did try think about doing the array with json_encode, but when I actually did it, I just got more confused with myself lol Thanks for your code, I never thought of doing PHP inside a <script>...i've always tried <script> inside PHP and that never works lol
0

You cannot combine PHP and JavaScript like this. PHP is run on the server and once it's finished is has generated a (most likely) HTML page that it sends back to the requesting browser. Over there the HTML is then rendered into the webpage you see and every JavaScript is executed afterwards. At this point, PHP has long finished executing.

3 Comments

ha, i knew someone would say this, but someone has suggested to me using arrays, but I can never figure out how those work with the jquery
You can of course just output the ids as JavaScript array (just output var myIds = new Array(1, 2, 3); somwhere on your page) and then use that one to display something via jQuery. But you could just as well simply output the ids directly.
I would love to simply loop through the ids using php, but then I still need to have some sort of loop on the jquery to change the #div tag to target specified divs on the page from the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.