JezK
Edit File: nonteach_upload.php
<?php session_start(); include('session_check.php'); include('adm_operations.php'); //ini_set("display_errors",1); $operations = new adm_operations(); $school_id = $_SESSION['child']['log_school_id']; $acdid = $_SESSION['child']['choosen_academic_id']; $me = $_SESSION['child']['auth_id']; include("header.php"); require_once 'excel_reader2.php'; if (isset($_POST['submit'])) { $html = ""; $smobile = array(); if (isset($_FILES['exl']['name'])) { $target_dir = "excel_uploads/"; $target_file = $target_dir . date('his') . basename($_FILES["exl"]["name"]); $FileType = pathinfo($target_file, PATHINFO_EXTENSION); if ($FileType != 'xls') { $errors = 'Please upload a valid Excel file and please do not upload any file other than the excel template provided under "Download Excel"'; } else { if (move_uploaded_file($_FILES["exl"]["tmp_name"], $target_file)) { $data = new Spreadsheet_Excel_Reader($target_file); $i = 1; $stafftype = array("Teacher", "Driver", "Admin Staff", "Maid", "Others"); if (count($data->sheets[$i][cells]) > 0) // checking sheet not empty { for ($j = 1; $j <= count($data->sheets[$i][cells]); $j++) // loop used to get each row of the sheet { if ($j >= 3) { if (trim($data->sheets[$i][cells][$j][1]) == '') { $html .= "Row " . $j . "- Please select Staff Type<br>"; } else if (!in_array($data->sheets[$i][cells][$j][1], $stafftype)) { $html .= "Row " . $j . "- Please enter a valid Staff Type<br>"; } if (trim($data->sheets[$i][cells][$j][2]) == '') { $html .= "Row " . $j . "- Please enter Staff Name<br>"; } else if (strlen(trim($data->sheets[$i][cells][$j][2])) > 50) { $html .= "Row " . $j . "- Staff Name should have maximum 50 characters<br>"; } else if (!preg_match('/^[a-zA-Z0-9 ]*$/', trim($data->sheets[$i][cells][$j][2]))) { $html .= "Row " . $j . "- Please enter a valid Staff Name<br>"; } if (trim($data->sheets[$i][cells][$j][3]) == '' && $data->sheets[$i][cells][$j][1] != 'Teacher') { $html .= "Row " . $j . "- Please enter Father/Spouse Name<br>"; } else if (strlen(trim($data->sheets[$i][cells][$j][3])) > 50 && $data->sheets[$i][cells][$j][1] != 'Teacher') { $html .= "Row " . $j . "- Father/Spouse Name should have maximum 50 characters<br>"; } else if (!preg_match('/^[a-zA-Z0-9 ]*$/', trim($data->sheets[$i][cells][$j][3])) && $data->sheets[$i][cells][$j][1] != 'Teacher') { $html .= "Row " . $j . "- Please enter a valid Father/Spouse Name<br>"; } if (trim($data->sheets[$i][cells][$j][4]) == '') { $html .= "Row " . $j . "- Please enter Staff Email Address<br>"; } else if (strlen($data->sheets[$i][cells][$j][4]) > 50) { $html .= "Row " . $j . "- Email should have maximum 50 characters<br>"; } else if (!filter_var($data->sheets[$i][cells][$j][4], FILTER_VALIDATE_EMAIL)) { $html .= "Row " . $j . "- Please enter a valid Email Address<br>"; } if (trim($data->sheets[$i][cells][$j][5]) == '') { $html .= "Row " . $j . "- Please enter Mobile Number<br>"; } else if (!preg_match('/^[0-9]*$/', $data->sheets[$i][cells][$j][5])) { $html .= "Row " . $j . "- Please enter a valid Mobile Number<br>"; } else if (strlen($data->sheets[$i][cells][$j][5]) != 10) { $html .= "Row " . $j . "- Please enter a valid Mobile Number<br>"; } else { $smobile[] = trim($data->sheets[$i][cells][$j][5]); } if (trim($data->sheets[$i][cells][$j][6]) == '') { $html .= "Row " . $j . "- Please enter Address<br>"; } else if (strlen($data->sheets[$i][cells][$j][6]) > 100) { $html .= "Row " . $j . "- Address should have maximum 100 characters<br>"; } else if (!preg_match('/^[A-Za-z0-9\n_@.,#;:\/\- ]+$/', $data->sheets[$i][cells][$j][6])) { $html .= "Row " . $j . "- Please enter a valid Address<br>"; } if ($data->sheets[$i][cells][$j][7] == '' && $data->sheets[$i][cells][$j][1] == 'Teacher') { $html .= "Row " . $j . "- Please enter Date of Birth<br>"; } else if ($data->sheets[$i][cells][$j][7] != '' && $data->sheets[$i][cells][$j][1] == 'Teacher') { $date1 = explode('/', $data->sheets[$i][cells][$j][7]); $admdate = $date1[2] . '-' . $date1[1] . '-' . $date1[0]; if (!strpos($data->sheets[$i][cells][$j][7], '/')) { $html .= "Row " . $j . "- Please enter a valid date of birth in dd/mm/yyyy format. eg:24/04/2015 <br>"; } else if (count($date1) < 3) { $html .= "Row " . $j . "- Please enter a valid Date of Birth<br>"; } else if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $admdate)) { $html .= "Row " . $j . "- Please enter a valid Date of Birth<br>"; } } if (trim($data->sheets[$i][cells][$j][8]) == '' && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- Please enter Driving License Number<br>"; } else if (strlen($data->sheets[$i][cells][$j][8]) > 15 && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- License Number should have maximum 15 characters<br>"; } else if (!preg_match('/^[A-Za-z0-9\n_@.,#;:\/\- ]+$/', $data->sheets[$i][cells][$j][8]) && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- Please enter a valid License Number<br>"; } if (trim($data->sheets[$i][cells][$j][9]) == '' && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- Please enter Issuing Authority<br>"; } else if (strlen(trim($data->sheets[$i][cells][$j][9])) > 50 && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- Issuing Authority should have maximum of 50 characters<br>"; } else if (!preg_match('/^[a-zA-Z0-9 ]*$/', $data->sheets[$i][cells][$j][9]) && $data->sheets[$i][cells][$j][1] == 'Driver') { $html .= "Row " . $j . "- Please enter a valid Issuing Authority Name<br>"; } $logincheck = "select school_id from ced_t_school where school_phone='" . trim($data->sheets[$i][cells][$j][5]) . "'"; $lexe = mysql_query($logincheck); $check = "select staff_id from ced_t_school_staff where staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "'"; //and academic_year_id='$acdid'"; $exe = mysql_query($check); if (mysql_num_rows($lexe) || mysql_num_rows($exe)) { $html .= "Row " . $j . "- Mobile Number already exists<br>"; } } } $dups = array(); foreach (array_count_values($smobile) as $val => $c) if ($c > 1) $dups[] = $val; if (count($dups) > 0) $html .= "Mobile Number(s) have been found repeated, Please correct and upload again"; if ($j == 3) $html = "errfound"; } /*INSERTING DATA INTO STUDENTS TABLE IF ALL THE FIELD ARE CORRECT AND SATISFIED ALL CONDITIONS*/ if ($html == '') { $i = 1; if (count($data->sheets[$i][cells]) > 0) // checking sheet not empty { for ($j = 1; $j <= count($data->sheets[$i][cells]); $j++) // loop used to get each row of the sheet { if ($j >= 3) { $admdate = explode('/', $data->sheets[$i][cells][$j][7]); $dtfm = $admdate[2] . '-' . $admdate[1] . '-' . $admdate[0]; switch ($data->sheets[$i][cells][$j][1]) { case 'Teacher': $qry = "INSERT INTO ced_t_school_staff set staff_name='" . trim($data->sheets[$i][cells][$j][2]) . "',staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "',staff_address='" . trim($data->sheets[$i][cells][$j][6]) . "', staff_email='" . trim($data->sheets[$i][cells][$j][4]) . "',status=1,school_id='" . $_SESSION['child']['log_school_id'] . "',staff_type=1,academic_year_id='" . $_SESSION['child']['choosen_academic_id'] . "',created_by='" . $_SESSION['child']['auth_id'] . "',created_on='" . date('Y-m-d') . "',date_of_birth='" . $dtfm . "'"; break; case 'Driver': $qry = "INSERT INTO ced_t_school_staff set staff_name='" . trim($data->sheets[$i][cells][$j][2]) . "',staff_otherperson='" . trim($data->sheets[$i][cells][$j][3]) . "',staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "',staff_address='" . trim($data->sheets[$i][cells][$j][6]) . "', staff_email='" . trim($data->sheets[$i][cells][$j][4]) . "',status=1,school_id='" . $_SESSION['child']['log_school_id'] . "',staff_driving_license='" . trim($data->sheets[$i][cells][$j][8]) . "',issuing_authority='" . trim($data->sheets[$i][cells][$j][9]) . "',staff_type=2,academic_year_id='" . $_SESSION['child']['choosen_academic_id'] . "', created_by='" . $_SESSION['child']['auth_id'] . "',created_on='" . date('Y-m-d') . "'"; break; case 'Admin Staff': $qry = "INSERT INTO ced_t_school_staff set staff_name='" . trim($data->sheets[$i][cells][$j][2]) . "',staff_otherperson='" . trim($data->sheets[$i][cells][$j][3]) . "',staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "',staff_address='" . trim($data->sheets[$i][cells][$j][6]) . "', staff_email='" . trim($data->sheets[$i][cells][$j][4]) . "',status=1,school_id='" . $_SESSION['child']['log_school_id'] . "',staff_type=3,academic_year_id='" . $_SESSION['child']['choosen_academic_id'] . "', created_by='" . $_SESSION['child']['auth_id'] . "',created_on='" . date('Y-m-d') . "'"; break; case 'Maid': $qry = "INSERT INTO ced_t_school_staff set staff_name='" . trim($data->sheets[$i][cells][$j][2]) . "',staff_otherperson='" . trim($data->sheets[$i][cells][$j][3]) . "',staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "',staff_address='" . trim($data->sheets[$i][cells][$j][6]) . "', staff_email='" . trim($data->sheets[$i][cells][$j][4]) . "',status=1,school_id='" . $_SESSION['child']['log_school_id'] . "',staff_type=4,academic_year_id='" . $_SESSION['child']['choosen_academic_id'] . "', created_by='" . $_SESSION['child']['auth_id'] . "',created_on='" . date('Y-m-d') . "'"; break; case 'Others': $qry = "INSERT INTO ced_t_school_staff set staff_name='" . trim($data->sheets[$i][cells][$j][2]) . "',staff_otherperson='" . trim($data->sheets[$i][cells][$j][3]) . "',staff_mobile='" . trim($data->sheets[$i][cells][$j][5]) . "',staff_address='" . trim($data->sheets[$i][cells][$j][6]) . "', staff_email='" . trim($data->sheets[$i][cells][$j][4]) . "',status=1,school_id='" . $_SESSION['child']['log_school_id'] . "',staff_type=5,academic_year_id='" . $_SESSION['child']['choosen_academic_id'] . "', created_by='" . $_SESSION['child']['auth_id'] . "',created_on='" . date('Y-m-d') . "'"; break; } $result = mysql_query($qry); } } } } unlink($target_file); if ($html == '') $errors = "Data uploaded successfully"; else { if ($html == 'errfound') $errors = "Please enter data for atleast one Staff"; else $errors = "Following errors have been found in the excel sheet. Please correct these errors and upload again:<br>" . $html; } } } } } ?> <body style=" overflow-x: hidden;"> <div class="row" style="margin-top: 30px;margin-left:15px;"> <div class="col-md-9 text-left" style="font:Arial, Helvetica, sans-serif;font-style:normal;font-size:24px; "> <i class="fa-solid fa-cloud-arrow-up" title="Upload Student Data" style="font-size: 20px;margin: 0px 0px 0px -15px;"></i> Upload Staff Data </div> </div> <form class="form-horizontal" method="post" enctype="multipart/form-data"> <div class="box-body" style="background-color:#fff; padding: 10px; font-family:Arial, Helvetica, sans-serif; font-size:13px; "> <div class="row"> <div class="form-group row col-md-6" style="margin-bottom:1.0%;"> <div class="col-md-4 offset-md-1 text-md-end"> <b style="color:#666666; ">Choose File</b><span style="color:#FF0000; margin:2px;">*</span> </div> <div class="col-md-7"> <input type="file" id="brw" name="exl" class="form-control" style=" font-size:12px;width:300px!important;" tabindex="3"> <!-- if there is an error--> <div class="col-md-10" id="err_brw" style="color:#990000;"></div> <!--- end error--> </div> </div> </div> </form> <div class="form-group my-3" > <div class="col-md-8" align="center"> <button class="btn btn-outline-secondary buttons btnclr" type="submit" name="submit" tabindex="4" value="Upload" onclick="return validations();">Upload</button> <button style="margin-left:2%;" type="button" onClick="location.href='nonteaching.php'" class="btn btn-outline-secondary buttons btnclr" tabindex="5">Cancel</button> </div> </div> <!--errors--> <div class="col-md-10 col-md-offset-2"> <span style="color:#ff0000;"><? if (isset($_POST['submit'])) echo $errors; ?></span> </div> <!--errors--> </div> <script> function validations() { var fil = document.getElementById('brw').value; $("#err_brw").text(""); if (fil == '') { $("#err_brw").text("Please select the excel file to upload"); $("#err_brw").show(); return false; } } function classchange() { var x = $('#classes').val(); $("#err_classes").text(""); if (x == 0) { $("#err_classes").text("Please select Class"); $("#err_classes").show(); section_option = '<option value="0">--Select Section--</option>'; $("#sections").html(section_option); return false; } else { $.ajax({ url: "ajaxcalls_attn.php", type: "POST", data: "mode=" + "choose_class" + "&data=" + x, success: function(result) { var result2 = eval("(" + result + ")"); var section_option = '<option value="0">--Select Section--</option>'; for (var i = 0; i < result2.length; i++) { section_option += '<option value="' + result2[i].section_id + '">' + result2[i].section_name + '</option>'; } $("#sections").html(section_option); }, error: function(e) { //alert(e); } }); } } </script>