🔒 Closed Import excel to database problem

Status
Not open for further replies.

loktoy

Established
anu po kaya mali o kulang sa code ko..

kasi pag nag iimport ako ng excel pati yun walang laman sa dulo nasasama ..
dapat po kung hanggan saan lang po yun laman ng excel dun lang po sana mag papakita po..
ito code ko po

<?php

include "../config/config.php";



?>
<!DOCTYPE html>
<HTMl:5>
<head>
<title>sample Design</title>
<link rel="stylesheet" href="../Innerjoin/css/bootstrap.css">
<link rel="stylesheet" href="../Innerjoin/css/style.css">
<script src="../Newproject/js/bootstrap.bundle.js"></script>


</head>
<body>

<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" value="">
<br>
<input type="submit" name="btnUpload" value="Upload LDDAP_ADA">
</form>

<?php

if(isset($_POST['btnUpload'])){

echo "<hr>";

echo "<table border='1' width='40%'>";

echo "<tr>

<td width='70%'><b>Payee Name</b></td>
<td width='70%'><b>Particular</b></td>
<td width='70%'><b>Amount</b></td>
<td width='70%'><b>LDDAP-ADA</b></td>
<td width='70%'><b>Date</b></td>
<td width='70%'><b>Status</b></td>
</tr>
<tr>
<td colspan='2'><hr></td>
</tr>";

$btnStatus = "ENABLED";

$filename = $_FILES['file']['tmp_name'];

if($_FILES['file']['size'] > 0) {

$file = fopen($filename, "r");

$row = 1;

while (($data = fgetcsv($file, 10000, ",")) !== false){
$payeename = $particular = $amount = $lddapada = $date = $status = "";

$payeenameErr = $particularErr = $amountErr = $lddapadaErr = $dateErr = $statusErr = "";

if($row == 1){
$row++;
continue;
}

if(empty($data[0])){
$payeenameErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$payeename = $data[0];
}
if(empty($data[1])){
$particularErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$particular = $data[1];
}
if(empty($data[2])){
$amountErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$amount = $data[2];
}
if(empty($data[3])){
$lddapadaErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$lddapada = $data[3];
}
if(empty($data[4])){
$dateErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$date = $data[4];
}
if(empty($data[5])){
$statusErr = "Product name is empty";

$btnStatus = "DISABLED";

}else {

$status = $data[5];
}


echo "<tr>

<td>$payeename</td>
<td>$particular</td>
<td>$amount</td>
<td>$lddapada</td>
<td>$date</td>
<td>$status</td>
</tr>";
}
}


echo "</table>";

}

?>

</body>
</HTMl:5>
 
Based on your code, it seems that you are importing an Excel file and displaying its contents in a table. However, you mentioned that even the empty cells are included in the table. To fix this issue, you can modify your code as follows:

1. Add a check to skip empty rows:
PHP:
while (($data = fgetcsv($file, 10000, ",")) !== false){
    if (empty(array_filter($data))) {
        continue;
    }
    // Rest of your code
}
This code checks if all cells in a row are empty. If they are, it skips that row and moves on to the next one.

2. Add a check to skip empty cells in each row:
PHP:
foreach ($data as $cell) {
    if (empty($cell)) {
        continue 2; // Skip to the next row
    }
    // Rest of your code
}
This code checks if a cell is empty. If it is, it skips that cell and moves on to the next one.

By adding these checks, the empty rows and cells will be skipped, and only the non-empty cells will be displayed in the table.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 457
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,142
Guests online
1,659
Total visitors
2,801

Forum statistics

Threads
2,292,066
Posts
29,074,445
Members
1,210,163
Latest member
VullGregory_46
Back
Top