How to remove top 50 rows in excel after exporting using php

Here is my code, It work after export but it seems, some of my html code are exporting also, So I decided to delete the top 50 rows. Thank you,

  $file_name = "DTR_" .$filename." ". date('Y-m-d') . ".csv";
  header("Content-Description: File Transfer");
  header("Content-Disposition: attachment; 

  filename=$file_name");
  header("Content-Type: application/csv;");

  $file = fopen('php://output', 'w');
  $header = array("DATE","DAY","TIME IN","TIME OUT","REMARKS");

  fputcsv($file, $header);

  $exportstmt = $conn->prepare("SELECT * FROM Time_temp2");   
}
  
$exportstmt->execute();
$result = $exportstmt->fetchAll(PDO::FETCH_ASSOC);

foreach($result as $row)
{
  $dt  = strtotime($row["cc"]);
  $day = date("D", $dt);
  $data = array();
  $data[]  = $row["cc"];
  $data[]  = $day;
  $data[]  = $row["TimeIN"];
  $data[]  = $row["TimeOut"];
  $data[]  = $row["Remarks"];
   
  fputcsv($file, $data);
}

fclose($file);
exit;

  • 3

    You should fix the actual issue, not just randomly discard lines of the result. “some of my html code are exporting also” – then that’s presumably HTML you output prior to this, which you simply should avoid in the first place.

    – 

  • 1

    but it seems, some of my html code are exporting also” It seems? You do have the resulting .csv file, does it contain HTML in the first 50 rows?

    – 

  • 1

    “It just happened that I’m tweaking some of the code for my new project” – so tweak it properly then, instead of trying to implement sub-par “workarounds.” The issue is most likely that you placed this code in a context where HTML output was already generated before – so why not move it to somewhere else then?

    – 




  • 1

    @joyce_ann As far as I can see, CBroe and brombeer (it’s a bit unclear who your remarks are directed at?) just posted some useful information and asked you some questions about your code, and made sensible suggestions about how to approach the scenario. If I was reviewing this I’d be struggling to see which content violates the code of conduct. Which part specifically do you think is problematic, and why?

    – 




  • 1

    Anyway, if you are seeing HTML content in the output then either a) that’s actually coming from what’s in the database data, or b) It’s coming from somewhere else in the script which you haven’t shown here, because the code is not written or structured in such a way that it prevents that output from occurring during a request which is intended to generate the CSV file. If you can provide us with a minimal reproducible example of the issue it will be much easier to help you resolve it. I don’t think this code, by itself, is the cause of the problem.

    – 




Leave a Comment