选中数据库查询生成的列表中的所有复选框

时间:2017-03-16 15:13:06

标签: php jquery

我的逻辑(显然)已关闭,但这就是我所拥有的......

当购买请求获得批准后,它会进入我们的“购买者”列表,这些人开车进城去获取物品(我住在阿拉斯加乡村,所以这是一个大问题)。无论购买请求如何,所有项目都会被转储到每个供应商的foreach循环中。换句话说,可能有10个购买请求,但所有Home Depot项目都在Home Depot表中。

我希望有一个盒子可以检查每个供应商“将所有商品标记为已购买”,因此当有人离开商店时,他们只能标记他们获得了所有Home Depot商品。

这是我的问题。 它并不总是有效。 2.当它工作时,你只能获得1次射击。单击它后,您必须刷新页面才能再次触发该功能。有人能指出我正确的方向吗?

<?php
$suppliers = [];
$suppliersQ = $db->query("SELECT supplier FROM purchase where status = ?  ORDER BY supplier",array('approved'));
$suppliersC = $suppliersQ->count();

//Did we find anything? Get a list of the vendors we need to visit
if($suppliersC > 0){
  $suppliersR = $db->results();
  foreach ($suppliersR as $key=>$value){

    if(!in_array($value,$suppliers)){
      array_push($suppliers,$value);
    }
  }
} else {
}

if(!empty($_POST['purchased'])){
  foreach($_POST['purchased'] as $k=>$v){
    $fields = array(
      'status' => 'purchased',
      'purchased_by' => $user->data()->fname." ".$user->data()->lname,
      'purchaser_id' => $user->data()->id,
    );

    $db->update('purchase',$v,$fields);
  }
}

if(!empty($_POST['removed'])){
  foreach($_POST['removed'] as $k=>$v){
    $fields = array(
      'status' => 'removed',
      'purchased_by' => $user->data()->fname." ".$user->data()->lname,
      'purchaser_id' => $user->data()->id,
    );

    $db->update('purchase',$v,$fields);
  }

}

?>

<div id="page-wrapper">
  <div class="container-fluid">
    <!-- Page Heading -->
    <div class="row">
      <div class="col-sm-12">
        <!-- Content goes here -->

      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-sm-12">
      <h3 align="center"><a href="purchaser_all_pr.php?closed=0">View All PRs</a></h3>

      <form class="" action="purchaser.php" name="buildings" method="post">
        <input class='btn btn-danger' name="submit" type='submit' value='Submit' class='submit' />
        <?php
        if($suppliersC > 0){
          foreach ($suppliers as $k=>$v){

            $purchaseQ = $db->query("SELECT * FROM purchase WHERE status = ? AND supplier = ? ORDER BY will_call,payment",(array('approved',$v->supplier)));
            $purchases = $purchaseQ->results();
            ?>
            <div class="table table-responsive">
            <table class='table table-hover' >
              <h4><?=$v->supplier?>(<a href="purchaser_print.php?vendor=<?=$v->supplier?>">Print</a>)</h4>
              <p align="right"><input type="checkbox" id="<?=$v->supplier?>"/> Mark all purchased</p>

              <tr>
                <th>WC?</th>
                <th>Method</th>
                <th>Item #</th>
                <th>Description</th>
                <th>Link</th>
                <th>Qty</th>
                <th>QtyMin</th>
                <th>Price</th>
                <th>Est Total</th>
                <!-- <th>Actual Total</th> -->
                <th>Purchased?</th>
                <th>Remove</th>
                <th>PR</th>

              </tr>
              <?php

              foreach ($purchases as $purchase){
                ?>
                <tr>
                  <td><a href="vendor.php?id=<?=$purchase->supplier_id?>"><?=$purchase->will_call?></a></td>
                  <td><?=$purchase->payment?></td>
                  <td><?=$purchase->item_num?></td>
                  <td><?=$purchase->item_desc?></td>
                  <?php
                  //Costco show description instead of link
                  if ($purchase->supplier_id == 10){ ?>
                  <td><?=$purchase->link?></td>
                <?php }else { ?>
                    <td><a href="<?=$purchase->link?>">Link</td>
                <?php } ?>
                    <td><?=$purchase->qty?></td>
                    <td><?=$purchase->qty_min?></td>
                    <td><?=$purchase->price?></td>
                    <td>$<?=$purchase->est_tot?></td>
                    <td><input type="checkbox" class="<?=$purchase->supplier_id?>" name="purchased[<?=$purchase->id?>]" value="<?=$purchase->id?>"></td>
                    <td><input type="checkbox" name="removed[<?=$purchase->id?>]" value="<?=$purchase->id?>"></td>
                    <td><a href="view_pr.php?prid=<?=$purchase->pr?>">PR-<?=$purchase->pr?></a></td>

                  </tr>
                  <SCRIPT language="javascript">
$(function(){

    // add multiple select / deselect functionality
    $("#<?=$v->supplier?>").click(function () {
          $('.<?=$purchase->supplier_id?>').attr('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".<?=$purchase->supplier_id?>").click(function(){

        if($(".<?=$purchase->supplier_id?>").length == $(".<?=$purchase->supplier_id?>:checked").length) {
            $("#<?=$v->supplier?>").attr("checked", "checked");
        } else {
            $("#<?=$v->supplier?>").removeAttr("checked");
        }
    });
});
</SCRIPT>
                  <?php
                }
              }
            }
            ?>

          </table>
        </div>
        </form>

        <!-- Content Ends Here -->
      </div> <!-- /.col -->
    </div> <!-- /.row -->
  </div> <!-- /.container -->
</div> <!-- /.wrapper -->


<?php require_once $abs_us_root.$us_url_root.'users/includes/page_footer.php'; // the final html footer copyright row + the external js calls ?>

<!-- Place any per-page javascript here -->


<?php require_once $abs_us_root.$us_url_root.'users/includes/html_footer.php'; // currently just the closing /body and /html ?>

2 个答案:

答案 0 :(得分:0)

我不被评论。我通过循环中的复选框列表实现了类似的.Jquery。像这样:

$(checkboxlist).each(function (){
  $(this).prop('checked',true)
});

答案 1 :(得分:0)

您可以采取以下措施:

$(function() {
  // first select all
  $('#first .select-all').click(function() {
    if ($('#first .select-all:checked').length == 0) {
      $('#first input[type="checkbox"].option').prop('checked', false);
    }
    else
    {
    $('#first input[type="checkbox"].option').prop('checked', true);
    }
  });
  
  // second select all
  $('#second .select-all').click(function() {
    if ($('#second .select-all:checked').length == 0) {
      $('#second input[type="checkbox"].option').prop('checked', false);
    }
    else
    {
    $('#second input[type="checkbox"].option').prop('checked', true);
    }
  });
  
  // for your purposes you can remove this timeout
  // I've only added it so you know that it works
  setTimeout(function() {
    var options = $('#second .option');
    var totalCheckboxes = options.length;
    var checked = 0;
    
    options.each(function(index) {
      if ($(this).is(':checked')) 
      {
        checked++;
      }
    });
    
    if (totalCheckboxes == checked)
    {
      $('#second .select-all').prop('checked', true);
    }
  }, 2000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Here I'll show you how to select and deselect all checkboxes-->
<div id="first">
  <input type="checkbox" value="all" class="select-all"> Select all <br>
  <input type="checkbox" value="1" class="option"> Option 1 <br>
  <input type="checkbox" value="2" class="option"> Option 2 <br>
  <input type="checkbox" value="3" class="option"> Option 3 <br>
  <input type="checkbox" value="4" class="option"> Option 4 <br>
</div>

<hr>

<!-- In this I'll show you how to select the "Select all" checkbox if all the other checkboxes are selected -->
<div id="second">
  <input type="checkbox" value="all" class="select-all"> Select all <br>
  <input type="checkbox" value="1" class="option" checked> Option 1 <br>
  <input type="checkbox" value="2" class="option" checked> Option 2 <br>
  <input type="checkbox" value="3" class="option" checked> Option 3 <br>
  <input type="checkbox" value="4" class="option" checked> Option 4 <br>
</div>

没有冒犯,但你的代码太可怕了,你应该考虑使用像Laravel这样的MVC框架。