【问题标题】:How to count Yes/no from table and count total如何从表中计算是/否并计算总数
【发布时间】:2016-04-19 15:14:23
【问题描述】:
$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 是总输出 这就是我想要的输出。我该怎么做?

【问题讨论】:

  • 不要使用 mysql_* 扩展,因为它已在 PHP 5.5 版中被弃用并从 PHP 7.0 版中删除,请改用 mysqli_*PDO
  • 是的,我尝试过,但不起作用

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


【解决方案1】:

替换

$yesCount++; 

与:

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

【讨论】:

    【解决方案2】:

    对每个列使用一个循环,并根据列号在其中进行计数(注意我假设它是来自 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>";
    

    【讨论】:

    • 假设奇数列有 2 个是,偶数列有 3 个是,所以 2x2 + 3x5 = 19 我的意思是你的代码正在工作,但你能解决这个问题吗??
    • 看起来您在查询中带回的列比您感兴趣的多得多。从使用 SELECT * 更改为指定要返回的列名
    • 所有表格都是 email1_opened,email2_opened,email3_opened.. 等等我插入这个但显示错误
    • 编辑它,假设第一列(实际上有电子邮件地址)称为contact_email
    • “选择contact_email、email1_opened、email1_clicked、email2_opened、email2_clicked、email3_opened、email3_clicked、email4_opened、email4_clicked、email5_opened、email5_clicked、email6_opened、email6_clicked、email7_opened、email7_clicked FROM 报告”;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多