title: Select all checkboxes in a list generated by a database query
body: <p>My logic is (obviously) off, but here's what I have...</p>
  
  <p>When a purchase request is approved, it goes into a list for our "purchaser" person who drives into town to get the items (I live in rural Alaska, so this is a big deal).  All of the items, regardless of purchase request, are dumped into foreach loop per vendor. In other words, there could be 10 purchase requests, but all Home Depot items go in a Home Depot table. </p>
  
  <p>I'd like to have a box that checks "mark all as purchased" per vendor, so when someone leaves the store, they can just mark that they got all the Home Depot items.  </p>
  
  <p>Here are my problems. 
  1. It doesn't always work.
  2. When it does work, you only get 1 shot at it. Once you click it, you have to refresh the page to get that function to fire again.  Can someone point me in the right direction?</p>
  
  <pre><code>&lt;?php
  $suppliers = [];
  $suppliersQ = $db-&gt;query("SELECT supplier FROM purchase where status = ?  ORDER BY supplier",array('approved'));
  $suppliersC = $suppliersQ-&gt;count();
  
  //Did we find anything? Get a list of the vendors we need to visit
  if($suppliersC &gt; 0){
    $suppliersR = $db-&gt;results();
    foreach ($suppliersR as $key=&gt;$value){
  
      if(!in_array($value,$suppliers)){
        array_push($suppliers,$value);
      }
    }
  } else {
  }
  
  if(!empty($_POST['purchased'])){
    foreach($_POST['purchased'] as $k=&gt;$v){
      $fields = array(
        'status' =&gt; 'purchased',
        'purchased_by' =&gt; $user-&gt;data()-&gt;fname." ".$user-&gt;data()-&gt;lname,
        'purchaser_id' =&gt; $user-&gt;data()-&gt;id,
      );
  
      $db-&gt;update('purchase',$v,$fields);
    }
  }
  
  if(!empty($_POST['removed'])){
    foreach($_POST['removed'] as $k=&gt;$v){
      $fields = array(
        'status' =&gt; 'removed',
        'purchased_by' =&gt; $user-&gt;data()-&gt;fname." ".$user-&gt;data()-&gt;lname,
        'purchaser_id' =&gt; $user-&gt;data()-&gt;id,
      );
  
      $db-&gt;update('purchase',$v,$fields);
    }
  
  }
  
  ?&gt;
  
  &lt;div id="page-wrapper"&gt;
    &lt;div class="container-fluid"&gt;
      &lt;!-- Page Heading --&gt;
      &lt;div class="row"&gt;
        &lt;div class="col-sm-12"&gt;
          &lt;!-- Content goes here --&gt;
  
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
      &lt;div class="col-sm-12"&gt;
        &lt;h3 align="center"&gt;&lt;a href="purchaser_all_pr.php?closed=0"&gt;View All PRs&lt;/a&gt;&lt;/h3&gt;
  
        &lt;form class="" action="purchaser.php" name="buildings" method="post"&gt;
          &lt;input class='btn btn-danger' name="submit" type='submit' value='Submit' class='submit' /&gt;
          &lt;?php
          if($suppliersC &gt; 0){
            foreach ($suppliers as $k=&gt;$v){
  
              $purchaseQ = $db-&gt;query("SELECT * FROM purchase WHERE status = ? AND supplier = ? ORDER BY will_call,payment",(array('approved',$v-&gt;supplier)));
              $purchases = $purchaseQ-&gt;results();
              ?&gt;
              &lt;div class="table table-responsive"&gt;
              &lt;table class='table table-hover' &gt;
                &lt;h4&gt;&lt;?=$v-&gt;supplier?&gt;(&lt;a href="purchaser_print.php?vendor=&lt;?=$v-&gt;supplier?&gt;"&gt;Print&lt;/a&gt;)&lt;/h4&gt;
                &lt;p align="right"&gt;&lt;input type="checkbox" id="&lt;?=$v-&gt;supplier?&gt;"/&gt; Mark all purchased&lt;/p&gt;
  
                &lt;tr&gt;
                  &lt;th&gt;WC?&lt;/th&gt;
                  &lt;th&gt;Method&lt;/th&gt;
                  &lt;th&gt;Item #&lt;/th&gt;
                  &lt;th&gt;Description&lt;/th&gt;
                  &lt;th&gt;Link&lt;/th&gt;
                  &lt;th&gt;Qty&lt;/th&gt;
                  &lt;th&gt;QtyMin&lt;/th&gt;
                  &lt;th&gt;Price&lt;/th&gt;
                  &lt;th&gt;Est Total&lt;/th&gt;
                  &lt;!-- &lt;th&gt;Actual Total&lt;/th&gt; --&gt;
                  &lt;th&gt;Purchased?&lt;/th&gt;
                  &lt;th&gt;Remove&lt;/th&gt;
                  &lt;th&gt;PR&lt;/th&gt;
  
                &lt;/tr&gt;
                &lt;?php
  
                foreach ($purchases as $purchase){
                  ?&gt;
                  &lt;tr&gt;
                    &lt;td&gt;&lt;a href="vendor.php?id=&lt;?=$purchase-&gt;supplier_id?&gt;"&gt;&lt;?=$purchase-&gt;will_call?&gt;&lt;/a&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;?=$purchase-&gt;payment?&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;?=$purchase-&gt;item_num?&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;?=$purchase-&gt;item_desc?&gt;&lt;/td&gt;
                    &lt;?php
                    //Costco show description instead of link
                    if ($purchase-&gt;supplier_id == 10){ ?&gt;
                    &lt;td&gt;&lt;?=$purchase-&gt;link?&gt;&lt;/td&gt;
                  &lt;?php }else { ?&gt;
                      &lt;td&gt;&lt;a href="&lt;?=$purchase-&gt;link?&gt;"&gt;Link&lt;/td&gt;
                  &lt;?php } ?&gt;
                      &lt;td&gt;&lt;?=$purchase-&gt;qty?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?=$purchase-&gt;qty_min?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?=$purchase-&gt;price?&gt;&lt;/td&gt;
                      &lt;td&gt;$&lt;?=$purchase-&gt;est_tot?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="checkbox" class="&lt;?=$purchase-&gt;supplier_id?&gt;" name="purchased[&lt;?=$purchase-&gt;id?&gt;]" value="&lt;?=$purchase-&gt;id?&gt;"&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="checkbox" name="removed[&lt;?=$purchase-&gt;id?&gt;]" value="&lt;?=$purchase-&gt;id?&gt;"&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;a href="view_pr.php?prid=&lt;?=$purchase-&gt;pr?&gt;"&gt;PR-&lt;?=$purchase-&gt;pr?&gt;&lt;/a&gt;&lt;/td&gt;
  
                    &lt;/tr&gt;
                    &lt;SCRIPT language="javascript"&gt;
  $(function(){
  
      // add multiple select / deselect functionality
      $("#&lt;?=$v-&gt;supplier?&gt;").click(function () {
            $('.&lt;?=$purchase-&gt;supplier_id?&gt;').attr('checked', this.checked);
      });
  
      // if all checkbox are selected, check the selectall checkbox
      // and viceversa
      $(".&lt;?=$purchase-&gt;supplier_id?&gt;").click(function(){
  
          if($(".&lt;?=$purchase-&gt;supplier_id?&gt;").length == $(".&lt;?=$purchase-&gt;supplier_id?&gt;:checked").length) {
              $("#&lt;?=$v-&gt;supplier?&gt;").attr("checked", "checked");
          } else {
              $("#&lt;?=$v-&gt;supplier?&gt;").removeAttr("checked");
          }
      });
  });
  &lt;/SCRIPT&gt;
                    &lt;?php
                  }
                }
              }
              ?&gt;
  
            &lt;/table&gt;
          &lt;/div&gt;
          &lt;/form&gt;
  
          &lt;!-- Content Ends Here --&gt;
        &lt;/div&gt; &lt;!-- /.col --&gt;
      &lt;/div&gt; &lt;!-- /.row --&gt;
    &lt;/div&gt; &lt;!-- /.container --&gt;
  &lt;/div&gt; &lt;!-- /.wrapper --&gt;
  
  
  &lt;?php require_once $abs_us_root.$us_url_root.'users/includes/page_footer.php'; // the final html footer copyright row + the external js calls ?&gt;
  
  &lt;!-- Place any per-page javascript here --&gt;
  
  
  &lt;?php require_once $abs_us_root.$us_url_root.'users/includes/html_footer.php'; // currently just the closing /body and /html ?&gt;
  </code></pre>