I have already successfully executed the inserting of data, additionally if it has existing data then it will not be inserted.
But the problem is, even though the data is duplicate and not inserted, the alert box still says that the data has been successfully imported, so how can I fix this and change its statement?
How can I return the statement false and show alert once the data is duplicate and return true to show alert once it has been inserted without existing data.
Controller:
function import()
{
if(isset($_FILES["file"]["name"]))
{
$path = $_FILES["file"]["tmp_name"];
$object = PHPExcel_IOFactory::load($path);
foreach($object->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=16; $row<=$highestRow; $row++)
{
$studentID = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$name = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$grade = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
$subject = $worksheet->getCellByColumnAndRow(2, 8)->getValue();
$slug = url_title($subject);
if($name != null){
$data[$row] = array(
'studentID' => $studentID,
'name' => $name,
'grade' => $grade,
'subject' => $slug
);
}
}
}
$subj = array('subject' => $slug);
// This is where I tried to condition the proper statement of the alert
if($data == true){
$this->loading_model->insert($data,$subj);
echo 'Data Imported successfully';
}else{
echo 'Invalid: Data has already been imported';
}
}
}
Model:
function insert($data,$subj)
{
$query = $this->db->get_where('tbl_college_grades', array('subject' => $subj['subject']));
if ($query->num_rows() == 0)
{
return $this->db->insert_batch('tbl_college_grades', $data);
}
else
{
return false;
}
}
Ajax:
$('#import_form').on('submit', function(event){
event.preventDefault();
$.ajax({
url:"<?php echo base_url(); ?>loadings/import",
method:"POST",
data:new FormData(this),
contentType:false,
cache:false,
processData:false,
success:function(data){
$('#file').val('');
load_data();
alert(data);
}
})
});
My Answer:
To fix the issue with the alert message showing "Data Imported successfully" even when the data is not inserted due to duplication, you can modify your controller code to return a specific message based on the result of the insertion operation. Here's how you can do it:
Controller:
php
function import()
{
if(isset($_FILES["file"]["name"]))
{
$path = $_FILES["file"]["tmp_name"];
$object = PHPExcel_IOFactory::load($path);
$data = array();
$subj = array();
foreach($object->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=16; $row<=$highestRow; $row++)
{
$studentID = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$name = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$grade = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
$subject = $worksheet->getCellByColumnAndRow(2, 8)->getValue();
$slug = url_title($subject);
if($name != null){
$data[] = array(
'studentID' => $studentID,
'name' => $name,
'grade' => $grade,
'subject' => $slug
);
}
$subj = array('subject' => $slug);
}
}
$result = $this->loading_model->insert($data, $subj);
if($result === true){
echo 'Data Imported successfully';
}else{
echo 'Invalid: Data has already been imported';
}
}
}
In the controller code above, I have modified the logic to check the result of the insertion operation and return a specific message based on whether the data was successfully inserted or not.
Additionally, in your Ajax success function, you can check the response data and show the alert message accordingly:
Ajax:
javascript
$('#import_form').on('submit', function(event){
event.preventDefault();
$.ajax({
url: "loadings/import",
method: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data){
$('#file').val('');
load_data();
alert(data);
}
});
});
With these changes, the alert message will now correctly reflect whether the data was successfully imported or not based on the result of the insertion operation.
Rate this post
3 of 5 based on 2475 votesComments