如何计算表中的是/否和计数总数

时间:2016-01-14 10:27:25

标签: php mysql while-loop count html-table

$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是总输出 那是我想要的输出。我怎么能这样做?

this is data output

2 个答案:

答案 0 :(得分:0)

替换

$yesCount++; 

使用:

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

答案 1 :(得分:0)

为每个cols使用一个循环,并在那里根据列号进行计数(注意我假设它是奇数和偶数列数来自db而不是cols后你交换编号周围没有似乎没必要 - 如果不是只在计数器设置行中交换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>";