PHP SQL员工考勤数据库更好查询

时间:2013-07-30 03:49:47

标签: php sql sql-server-2008

嗨伙计们有点卡住我有一切按照我想要的方式工作,但它非常慢,我觉得效率不高我想知道是否有人可以帮助我改进我的代码来加快它我已经尝试过现在看了一个星期

所以我正在做的是保持员工的出勤记录以月历的形式显示为小时

但我要做的是循环所有雇员,然后我循环这个月的日子然后循环每个员工和每个月的每一天获取每天的记录

任何人都可以帮助我改变这一点以加快速度

这一切都以我必须的方式运作,现在已经有近100,000条记录并且变得非常慢

PHP显示日历

           <?php foreach ( $search_result as $display_recent_result ) {
if($display_recent_result->Photo) { $photo = $display_recent_result->Photo; } 
else { $photo = NULL; }  ?>

<tr>
<th scope="row" class="spec" ><div class="employee_fullname" id='<?php echo
htmlentities($photo); ?>'><?php echo htmlentities($display_recent_result->SURNAME)
 . ", " . htmlentities($display_recent_result->NAME); ?></div> </th>

<td class="table_prn_cell" ><?php echo htmlentities($display_recent_result->PRN);
?></td>
 </tr>
<?php } ?>
 </table>
 </div>
 <div id="attend">
 <table id="mytable" cellpadding="0" cellspacing="0" summary="Current Employees">
<caption> </caption>
<tr>
 <?php 
    $m= date('m');
    $y= date('y');
    if($year) {
        $m= date('m',mktime(0,0,0,$month,1,$year));
        $y= date('y',mktime(0,0,0,$month,1,$year));
    }
        $start_day = 1;
        $num = cal_days_in_month(CAL_GREGORIAN, $m, $y);

    for($i=0; $i<$num; $i++){
    echo '<th class="attdate" scope="col" abbr="">'.date('d/m',mktime(0,0,0,$m
($start_day+$i),$y)).'</th>';
    }
    echo ' </tr>';
    foreach ( $search_result as $display_recent_result ) {
        convert_date($display_recent_result->StartDate, 'StartDate');
        $start = strtotime($StartDate);

    $prn = $display_recent_result->PRN;
    echo '<tr>';            
    for($i=0; $i<$num; $i++) {      
    $date = date('Y-m-d',mktime(0,0,0,$m,($start_day+$i),$y));
    if($start <= strtotime($date)) {
    $state = attendance::find_by_PRN($prn, $date);      
        if($state) {        
            $cat = attendance_category::find_by_id($state->CategoryID);
            echo "<td height=\"32\" headers=\"$state->id\" class=\"$cat->Code\">".$cat->Code."</td>";
        } else {
            echo "<td>&nbsp;</td>";
        }       
    } else {
        echo "<td headers=\"NE\" class='NE'>NE</td>";
    }
    }
    echo '</tr>';
    } 
    echo "</table>";
 ?>

员工数据库的SQL

public static function display_attendance ( $sort=NULL ) {      
    if($sort==999){ $sortsql=NULL;  }elseif($sort>0){ $sortsql = 'AND AreaPosition = '. "'$sort'"; } else {$sortsql =NULL; }        


                $sql="(SELECT PRN, NAME, SURNAME, Photo, StartDate FROM ". self::$table_name ." WHERE Attendance = 1 AND CurrentEmployee = 1 $sortsql )
                        UNION
                    (SELECT PRN, NAME, SURNAME, Photo, StartDate FROM ". self::$table_name2 ." WHERE Attendance = 1 AND CurrentEmployee = 1 $sortsql )
                    ORDER BY SURNAME";

    return self::find_by_sql($sql);
}

获取记录数据库表的SQL

 public static function find_by_PRN($PRN, $date) {
    $result_array = self::find_by_sql("SELECT TOP 1 id, CategoryID FROM ".static::$table_name." WHERE PRN = $PRN AND '$date' BETWEEN Sdate AND Edate ORDER BY id DESC");
    return !empty($result_array) ? array_shift($result_array) : false;

}

SQL数据库考勤表

 id         int 
 Attend bit 
 DateMade   date    
 CategoryID int 
 NoteID int 
 PRN    int 
 Sdate  date    
 Edate  date    
 UserID nvarchar(50)    

0 个答案:

没有答案