繁体   English   中英

如何从表格中计算是/否并计算总数

[英]How to count Yes/no from table and count total

$con = mysql_connect("localhost","root","");
    $db = mysql_select_db("email-db2",$con);
    $query = "SELECT * FROM report";
    $run = mysql_query($query);
     echo "<table>
    <thead>
      <tr>
        <th>Contact Email</th>
        <th>email1</th>
        <th>email2</th>
        <th>email3</th>
        <th>email4</th>
        <th>email5</th>
        <th>email6</th>
        <th>email7</th>
        <th>email8</th>
        <th>email9</th>
        <th>email10</th>
        <th>email11</th>
        <th>email12</th>
        <th>email13</th>
        <th>email14</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>";
    while ($row = mysql_fetch_array($run)) {
        $COL1 = $row[0];
        $COL2 = $row[1];
        $COL3 = $row[2];
        $COL4 = $row[3];
        $COL5 = $row[4];
        $COL6 = $row[5];
        $COL7 = $row[6];
        $COL8 = $row[7];
        $COL9 = $row[8];
        $COL10 = $row[9];
        $COL11 = $row[10];
        $COL12 = $row[11];
        $COL13 = $row[12];
        $COL14 = $row[13];
        $COL15 = $row[14];
      echo "<tr>";
         echo "<td>"; echo $COL1;  echo "</td>";
         echo "<td>"; echo $COL2;  echo "</td>";
         echo "<td>"; echo $COL3;  echo "</td>";
         echo "<td>"; echo $COL4;  echo "</td>";
         echo "<td>"; echo $COL5;  echo "</td>";
         echo "<td>"; echo $COL6;  echo "</td>";
         echo "<td>"; echo $COL7;  echo "</td>";
         echo "<td>"; echo $COL8;  echo "</td>";
         echo "<td>"; echo $COL9;  echo "</td>";
         echo "<td>"; echo $COL10;  echo "</td>";
         echo "<td>"; echo $COL11;  echo "</td>";
         echo "<td>"; echo $COL12;  echo "</td>";
         echo "<td>"; echo $COL13;  echo "</td>";
         echo "<td>"; echo $COL14;  echo "</td>";
         echo "<td>"; echo $COL15;  echo "</td>";
         echo '<td>';
         $yesCount = 0;
        $noCount = 0;
        for ($i=1; $i<= 15; $i += 2){
            if (empty($row['email'.$i])) {
                $noCount++;
            } else {
                $yesCount++;
            }
        }
        echo $yesCount;
         echo "</td>"; 
       echo "</tr>";
   } 
  echo "</tbody>
</table>";  

这是我的代码
在总计的最后一栏中,我想要这个
我在奇数列中找到是,它添加了2
如果在偶数列中找到是,则加5
表示2x2 + 3x5 = 19,19是总输出这就是我想要的输出。 我怎样才能做到这一点?

这是数据输出

更换

$yesCount++; 

与:

$yesCount += $i % 2 == 0? 2: 5;

对每个列使用一个循环,并在其中根据列号进行计数(请注意,我假设它是db中的奇数和偶列列,而不是列,而不是列,而是交换了未出现的编号后必要-如果不只是在计数器设置行中交换2和5)。

<?php
    $con = mysql_connect("localhost","root","");
    $db = mysql_select_db("email-db2",$con);
    $query = "SELECT contact_email,
                    email1_opened,
                    email2_opened,
                    email3_opened, 
                    email4_opened, 
                    email5_opened, 
                    email6_opened, 
                    email7_opened, 
                    email8_opened, 
                    email9_opened, 
                    email10_opened, 
                    email11_opened, 
                    email12_opened, 
                    email13_opened, 
                    email14_opened 
            FROM report";
    $run = mysql_query($query);
    echo "<table>
    <thead>
      <tr>
        <th>Contact Email</th>
        <th>email1</th>
        <th>email2</th>
        <th>email3</th>
        <th>email4</th>
        <th>email5</th>
        <th>email6</th>
        <th>email7</th>
        <th>email8</th>
        <th>email9</th>
        <th>email10</th>
        <th>email11</th>
        <th>email12</th>
        <th>email13</th>
        <th>email14</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>";
    while ($row = mysql_fetch_array($run, MYSQL_NUM)) 
    {
        echo "<tr>";
        $counter = 0;
        foreach($row AS $key=>$value)
        {
            echo "<td>$value</td>";
            $counter += (($key > 0 and $value == 'yes') ? (($key % 2 == 0) ? 5 : 2 ) : 0);
        }
        echo "<td>$counter</td>";
        echo "</tr>";
    } 
    echo "</tbody>
</table>";

我同意上面的评论,您应该避免使用mysql_ *函数编写新代码。

相当于上述的Mysqli为:-

<?php
    $con = mysqli_connect('localhost', 'root', '', 'email');
    $query = "SELECT contact_email,
                    email1_opened,
                    email2_opened,
                    email3_opened, 
                    email4_opened, 
                    email5_opened, 
                    email6_opened, 
                    email7_opened, 
                    email8_opened, 
                    email9_opened, 
                    email10_opened, 
                    email11_opened, 
                    email12_opened, 
                    email13_opened, 
                    email14_opened 
            FROM report";
    $run = mysqli_query($con, $query);
    echo "<table>
    <thead>
      <tr>
        <th>Contact Email</th>
        <th>email1</th>
        <th>email2</th>
        <th>email3</th>
        <th>email4</th>
        <th>email5</th>
        <th>email6</th>
        <th>email7</th>
        <th>email8</th>
        <th>email9</th>
        <th>email10</th>
        <th>email11</th>
        <th>email12</th>
        <th>email13</th>
        <th>email14</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>";
    while ($row = mysqli_fetch_array($run, MYSQL_NUM)) 
    {
        echo "<tr>";
        $counter = 0;
        foreach($row AS $key=>$value)
        {
            echo "<td>$value</td>";
            $counter += (($key > 0 and $value == 'yes') ? (($key % 2 == 0) ? 5 : 2 ) : 0);
        }
        echo "<td>$counter</td>";
        echo "</tr>";
    } 
    echo "</tbody>
</table>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM