简体   繁体   中英

formatting excel file issue

I have a program that turns a file into an excel file but for some reason my phone and email fields get switched. It looks like this -

firstname   lastname    email   phone
test                            bi@gmail.com
test            d               813-767 leighmimail.com
test            Pds         888-413 Bperotects.com

the email and phone should be switched

Code:

   <?php
session_start();

$num = $_SESSION['num'];
    $firstname =  $_SESSION['firstnameexport'];
    $lastname = $_SESSION['lastnameexport'];
    $email = $_SESSION['emailexport'];
    $phone = $_SESSION['phoneexport'];

    for($i =0; $i< $num; $i++){
    $data[$i] = array(
    "firstname" => $firstname[$i] , "lastname" => $lastname[$i], "email" => $email[$i], "phone" => $phone[$i]);
    }

function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

// filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }
     array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\r\n";
  }


unset($_SESSION['num'],$_SESSION['firstnameexport'],$_SESSION['lastnameexport'],$_SESSION['emailexport'],$_SESSION['phoneexport']);
?>

Thank you for your time!

Fixed it - all I had to do was switch some of the session variables out so it would give the correct value for the right column.

Thank you for your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM