php - Faster counting and sum data faster in for loop -


i'm working on counting students' data database contains thousands of rows , have within loop, problem calculation slow. what's best way make faster? query command:

$sql_query = $con->prepare("select id_number,title,name,lastname,classroom students academic='".$config['info']['year']."' order id_number"); $sql_query->execute(); while($fetchar = $sql_query->fetch(pdo::fetch_obj)){     $ss_data[] = $fetchar->id_number.'-'.$fetchar->title.$fetchar->name.'&nbsp;'.$fetchar->lastname.'-'.$fetchar->classroom; } $row_counts = count($ss_data); for($i=0;$i<$row_counts;$i++){     $e = explode("-",$ss_data[$i]);     $id = $e[0]; //รหัสประจำตัวนักเรียน     $personal_data = $e[1]; //ข้อมูลนักเรียน     $room =  $e[2]; //ระดับชั้น      //คิวรี่จำนวนการร่วมกิจกรรมของนักเรียนแต่ละคน ทั้งโรงเรียน     $sql = $con->prepare("select count(std_id) present_count attendance std_id='$id' , att_attend='1' , att_mode='$mode' order std_id");     $sql->execute();     $arr = $sql->fetch(pdo::fetch_obj);     $presentdata = $arr->present_count;     //คิวรี่จำนวนการไม่ร่วมกิจกรรมของนักเรียนแต่ละคน ทั้งโรงเรียน     $query = $con->prepare("select count(std_id) absent_count attendance std_id='$id' , att_attend in(0,2,3) order std_id");     $query->execute();     $fchar = $query->fetch(pdo::fetch_obj);     $absentdata = $fchar->absent_count;     //คำนวณเปอร์เซ็นของนักเรียนแต่ละคน     $percents = (($presentdata-$absentdata)*100) / $presentdata;     //เก็บค่าต่างๆ ไว้ในอาเรย์เพื่อนำไปใช้งานต่อไป     $student_data[] = $presentdata.'|'.$id.'|'.$personal_data.'|'.$percents.'|'.$room; //ใช้เครื่องหมาย | แทน เครื่องหมาย (-) เนื่องจากเปอร์เซนต์นักเรียนบางคนติดลบ }   


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -