简体   繁体   中英

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>";  

This is my code
In the last column of Total I want this
I finds a yes in odd columns it adds 2
if it finds yes in even columns it adds 5
means 2x2 + 3x5 = 19 , 19 is total output That is the output I would like to have. How can I do this?

这是数据输出

Replace

$yesCount++; 

with:

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

Using a loop for each of the cols, and having a count in there depending on the column number (note I am assuming it is odd and even column numbers from the db rather than the cols after you swap the numbering around which doesn't appear necessary - if not just swap the 2 and 5 around in the counter setting row).

<?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>";

I would agree with the comment above that you should avoid the mysql_* functions for new code.

Mysqli equivalent of the above would be:-

<?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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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