简体   繁体   English

如何将具有相同名称的行合并为一个,其中使用两个 foreach 获取数据

[英]How to merge rows having same name into one where data taken using two foreach

I want to merge rows with same name to one row in below table, also the count of leave should show it's sum while merging, i have used two forach for listing table since its taking two kinda data.tried something didn't work.sending as a message body for email tried some method saw , but it didnt work我想将具有相同名称的行合并到下表中的一行,并且休假计数应该在合并时显示它的总和,我已经使用了两个 forach 来列出表,因为它采用了两种数据。尝试了一些没有用的东西。发送作为电子邮件的消息正文尝试了一些方法,但没有奏效

在此处输入图片说明 Name Leave Emp1 0 Emp2 0.5 Emp3 1 Emp2 1 Emp4 4 Emp5 1 Emp3 1名称 Leave Emp1 0 Emp2 0.5 Emp3 1 Emp2 1 Emp4 4 Emp5 1 Emp3 1

code:代码:

$message2="<html>
               <body>

            <table ;> 
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Leave</th>

                    </tr>    
                </thead>
                <tbody>";


                        foreach($leave2 as $val) {

                           if($val->hourcount>2)
                           {
                            $newcount=.5;
                           }
                           else
                           {
                            $newcount=0;
                           }
                           if($val->hourcount>=6)
                           {
                            $newcount=1;
                           }
                        $message2 .="<tr>
                            <td>" . $val->resource ."</td>
                            <td>".$newcount."</td>

                        </tr>";

                     } 

                    foreach($leave as $value) { 
 $message2 .="<tr>
                            <td>" . $value->resource ."</td>
                            <td>".$value->Days."</td>

                        </tr>";
                     } 
if (count($value->resource) > 0) {

    $html = '';

    foreach($val->resource as $key => $val) {
       $html .= "<tr>\r\n";    

       $html .= "<td rowspan='".count($val)."'>{$key}</td>\r\n";

       foreach($val as $key => $td) {
           if($key>0) {
              $html.= "<tr>";
           }
           $html .= "<td>{$td}</td>\r\n";

           $html .= "</tr>\r\n";
       }        
    }
}


                 "</tbody>
            </table>

            <p>Leave report</p> 
            <hr />     
        </body>
        </html>";

You can try something like this你可以试试这样的

1 ) Create an array which holds employee name and their total leaves count 1 ) 创建一个数组,其中包含员工姓名和他们的总叶数

2 ) Display desired table format from that array 2 ) 从该数组中显示所需的表格格式

<?php
    $sql = "SELECT * FROM table_name";
    $result = $db->query($sql);
     if ($result->num_rows > 0) { 
          while ($row = $result->fetch_assoc()) {

            $name = $row['Name'];
            $level = $row['Level'];
            if(isset($leaveArr[$name])){
                $leaveArr[$name] = $leaveArr[$name]+ $level;
            }else{
                $leaveArr[$name] = $level;
            }

        }  } 
    if(count($leaveArr)>0){
        echo "<table>";
        echo "<tr><td>Name</td><td>Leave</td></tr>";
        foreach ($leaveArr as $key => $value) {
            echo "<tr>";
            echo "<td>" . $key . "</td>";
            echo "<td>" . $value . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

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

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