简体   繁体   中英

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

在此处输入图片说明 Name 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

2 ) Display desired table format from that array

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

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