run jquery function with each php iteration

run jquery function with each php iteration

Hi,

I'm loading multiple items from a database using php. Each item has a 'category' attribute. I'm trying to run a jquery function, which loads a php script, for each iteration of an item. The script fetches all category options from the databse and populates a <select> object.

The reason for this roundabout method is that there is an option at the top of the page to add a new category. If a new category is added, I want to reload the above function for every item, so the new category is added to the <select>.

  1. My php script 'loadCats.php' has the following:
  2. select id="category" name="category">
  3. <option value="">Select Category</option>
  4. <?
  5. while ($row = mysql_fetch_array($sql))
  6. {
  7. $val = $row['category'];
  8. if ($cat != "")
  9. $selected = $cat;
  10. else
  11. $selected = $_SESSION['category'];
  12. ?>
  13. <option value= "<? print $val ?>" <? if($selected == $val) print "selected" ?> ><? print $val; ?></option>
  14. <?
  15. }
  16. ?>
  17. </select>
the jquery function calling loadCats.php is as follows:
  1.  <td align="left" valign="middle" id="displayCats">

  2. <script>
  3. $("#displayCats").each(function() {
  4. loadCats();
  5. });
  6. function loadCats()
  7. {
  8. $('#displayCats').load('loadCats.php',{"cat":"<? print $row['category'] ?>"});
  9. };
  10. });
  11. </script>
  12. </td>
Only the last item has a drop down menu. The rest of the items have nothing.

Thanks