PHP计算值在结果中出现的次数

时间:2018-07-29 00:31:33

标签: php

我正在从数据库中选择数据:

$status = "Attended";
$query = $conn->prepare('SELECT * FROM jobstocomplete WHERE applicant_mid=:mID AND status=:status');

$query->execute(array(':mID' => $mID, ':status' => $status));

然后我使用while循环将结果放入表中,如下所示:

if ($query->rowCount() > 0) {

  echo "<div class='panel-body table-responsive'>";
  echo "<table class='table table-striped'>";
  echo "<thead>";
  echo "<tr>";
  echo "<th id=''>Job ID</th>";
  echo "<th id=''>Total</th>";
  echo "<th id=''>Company Name</th>";
  echo "<th id=''>Action</th>";
  echo "</tr>";
  echo "</thead>";
  echo "<tbody>";

  while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

    $companyname = $row['compname'];

    echo "<tr>";
    echo "<td>" . htmlspecialchars($row['jobid'], ENT_QUOTES, 'utf-8') . "</td>";
    echo "<td>" . htmlspecialchars($row['total'], ENT_QUOTES, 'utf-8') . "</td>";
    echo "<td>" . htmlspecialchars($companyname, ENT_QUOTES, 'utf-8') . "</td>";

    $completed = $row['compl'];

    if (empty($completed)) {
      echo  "<td>" . "<a href='#' data-toggle='tooltip' data-placement='top' title='Not yet complete' disabled>Mark as completed</button></a>" . "</td>";
    }else{
      echo  "<td>" . "<a class='btn custombtn' href='#invoiceModal' data-toggle='modal' data-jid='".htmlspecialchars($row["jobid"], ENT_QUOTES, 'utf-8')."' data-mid='".htmlspecialchars($row["applicant_mid"], ENT_QUOTES, 'utf-8')."' data-upid='".htmlspecialchars($row["up_id"], ENT_QUOTES, 'utf-8')."'> Mark as completed</a>" . "</td>";
    }
    echo "</tr>";
  }
}else{
  echo "<p>Looks like you've not completed any jobs</p>";
}

当我的用户单击“标记为完成”时,这将启动引导程序模态,然后触发jQuery / ajax使用fpdf生成发票。

我要实现的是计算公司名称出现的次数。例如,如果结果$companyname = "Heaven Autos"中多次出现,我希望我的代码启动一个不同的模式,该模式将提醒

“您是否要生成多张发票,因为您已经为该公司执行了多项工作”

这将随后使用jQuery / ajax触发不同的php文件并计算总计等,并基于此生成发票。

我打算通过运行查询和简单的if语句来执行此操作,但是不知道如何启动逻辑。

我希望这是有道理的,希望您能提供帮助。

p.s我已经尝试过诸如array_count等之类的东西,但运气不太好,任何建议都值得赞赏。

2 个答案:

答案 0 :(得分:0)

在我的评论之后,我对此做了一些修改:

if ($query->rowCount() > 0) {

          echo "<div class='panel-body table-responsive'>";
          echo "<table class='table table-striped'>";
          echo "<thead>";
          echo "<tr>";
          echo "<th id=''>Job ID</th>";
          echo "<th id=''>Total</th>";
          echo "<th id=''>Company Name</th>";
          echo "<th id=''>Action</th>";
          echo "</tr>";
          echo "</thead>";
          echo "<tbody>";

          while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

          $companyname = $row['compname'];

          echo "<tr>";
          echo "<td>" . htmlspecialchars($row['jobid'], ENT_QUOTES, 'utf-8') . "</td>";
          echo "<td>" . htmlspecialchars($row['total'], ENT_QUOTES, 'utf-8') . "</td>";
          echo "<td>" . htmlspecialchars($companyname, ENT_QUOTES, 'utf-8') . "</td>";

          $completed = $row['compl'];

          if (empty($completed)) {
            echo  "<td>" . "<a href='#' data-toggle='tooltip' data-placement='top' title='Not yet complete' disabled>Mark as completed</button></a>" . "</td>";
          }else{
            echo  "<td>" . "<a class='btn custombtn' href='#invoiceModal' data-toggle='modal' data-company='" . htmlspecialchars($companyname, ENT_QUOTES, 'utf-8') . "' data-jid='".htmlspecialchars($row["jobid"], ENT_QUOTES, 'utf-8')."' data-mid='".htmlspecialchars($row["applicant_mid"], ENT_QUOTES, 'utf-8')."' data-upid='".htmlspecialchars($row["up_id"], ENT_QUOTES, 'utf-8')."'> Mark as completed</a>" . "</td>";
          }

          echo "</tr>";

          }

}else{
              echo "<p>Looks like you've not completed any jobs</p>";
}

此处的更改是,我向您的模式触发器中添加了data-company属性,这样,在生成公司时,每家公司的名称都存储在该模式触发器的属性中。

现在是单击该按钮时要使用的JS:

<script>
    // clicking the button begins here
    $('.custombtn').click(function() {
        // set the count variable to zero, we'll check how many companies exist and add it to the counter 
        var count = 0;
        // grab the clicked button's company name in the data attribute
        var xid = $(this).attr('data-company');
        // loop through all the buttons by class and check the data-company attribute for our count
        $(".custombtn").each(function(index) {
            // store each button's data company attribute for our logic
            var co = $(this).attr('data-company');
            // we check if the currently clicked button's company matches any of the companies that exists in our table
            if (co == xid) {
                // if we have a match, we add one to our counter
                count = count + 1;
            }
        });
        // at the end of the loop, we now check the results of our count
        if (count > 1) {
            // count is greater than 1, so we can trigger a multi-company action
            console.log('we got two or more matches');
        } else {
            // count is  1, so we can trigger a single company action
            console.log('we got one match');
        }
    });
</script>

我评论了每个步骤,以便您可以轻松地看到其逻辑和功能;

基本上,我们要做的是单击按钮时,将按钮的data-company属性值与我们在PHP中设置的公司名称一起使用;我们通过类标识符遍历所有按钮,并检查每个按钮的data-company属性;现在,对于每个结果,我们都有一个计数器变量,该变量随每次匹配项而递增,因此我们可以检查计数器的结果是否为1或更大;

获得结果时,可以修改此代码以存储其他数据属性,以触发所需的任何自定义操作,例如触发两个单独的模态,进行两个ajax调用,显示不同的警报或通知;无论您想做什么。

答案 1 :(得分:0)

我遇到了麻烦,实现了这一点,将以下查询添加到了while循环中,以计算条件中表中公司名称存在的次数:

$sqll = $conn->prepare("SELECT count(*) FROM jobstocomplete WHERE compname=:companyname AND applicant_mid=:mID AND status=:status"); 
          $sqll->execute(array(':companyname' => $companyname,':mID' => $mID, ':status' => $status2)); 
          $number_of_rows = $sqll->fetchColumn(); 

然后我做了一个if语句,并相应地启动了适当的模式:

if (empty($completed)) {
            echo  "<td>" . "<a href='#' data-toggle='tooltip' data-placement='top' title='Job not yet completed'><button class='btn custombtn' disabled>Mark as completed</button></a>" . "</td>";
          }else if(!empty($completed) && $number_of_rows > 1){
            echo  "<td>" . "<a id='genInvoiceBtn' class='btn custombtn' href='#multiinvoiceModal' data-toggle='modal' data-company='".htmlspecialchars($companyname, ENT_QUOTES, 'utf-8')."' data-jid='".htmlspecialchars($row["jobid"], ENT_QUOTES, 'utf-8')."' data-mid='".htmlspecialchars($row["applicant_mid"], ENT_QUOTES, 'utf-8')."' data-upid='".htmlspecialchars($row["up_id"], ENT_QUOTES, 'utf-8')."'> Mark as completed</a>" . "</td>";
          }else if(!empty($completed) && $number_of_rows < 1){
            echo  "<td>" . "<a id='genInvoiceBtn' class='btn custombtn' href='#invoiceModal' data-toggle='modal' data-company='".htmlspecialchars($companyname, ENT_QUOTES, 'utf-8')."' data-jid='".htmlspecialchars($row["jobid"], ENT_QUOTES, 'utf-8')."' data-mid='".htmlspecialchars($row["applicant_mid"], ENT_QUOTES, 'utf-8')."' data-upid='".htmlspecialchars($row["up_id"], ENT_QUOTES, 'utf-8')."'> Mark as completed</a>" . "</td>";
          }