Jump to content

ady01

Members
  • Posts

    78
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ady01's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. REPOST CODE : <?php function xls_format_row( $field ) { $line = ''; foreach($field as $value){ if(!isset($value) || $value == "") $value = "\t"; else{ $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } return trim($line)."\n"; } function xls_send( $headers, $rows ) { header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Disposition: attachment; filename=excelfile.xls"); echo $headers."\n".$rows; } include("config.php"); mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); $headers = ("Account number"."\t"."Account"."\t"."Account Manager"."\t"."Buddy"."\t"."Buddys Email"."\t"."Buddy DDI"."\t"."Reseller Telephone"."\t"."Reseller Contact"."\t"."Reseller Email"."\t"."Notes"."\t"); $result = mysql_query('SELECT * FROM Addresses'); $count = mysql_num_fields($result); $data = array(); while($row = mysql_fetch_row($result)) { array_push($data, $row); } $xls_rows = ''; foreach( $data as $row ) $xls_rows .= xls_format_row( $row ); $xls_rows = str_replace(array("</p><p>", "</li><li>"), ", ", $xls_rows); $xls_rows = str_replace(" ", "", $xls_rows); $xls_rows = strip_tags($xls_rows); xls_send( $headers, $xls_rows ); ?> [code] [/code]
  2. Guys you have been a great help here i know have the below code but STILL ! outputting to the browser I just really dont know whats going on here ? <? function xls_format_row( $field ) { $line = ''; foreach($field as $value){ if(!isset($value) || $value == "") $value = "\t"; else{ $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } return trim($line)."\n"; } function xls_send( $headers, $rows ) { header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Disposition: attachment; filename=excelfile.xls"); echo $headers."\n".$rows; } include("config.php"); mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); $headers = ("Name"."\t"."DOB"."\t"."HouseNumber"."\t"."Street"."\t"."City"."\t"."Country"."\t"."Telephone"."\t"."Fax"."\t"."Email"."\t"."Remarks"."\t"); $result = mysql_query('SELECT * FROM Addresses'); $count = mysql_num_fields($result); $data = array(); while($row = mysql_fetch_row($result)) { array_push($data, $row); } $xls_rows = ''; foreach( $data as $row ) $xls_rows .= xls_format_row( $row ); $xls_rows = str_replace(array("</p><p>", "</li><li>"), ", ", $xls_rows); $xls_rows = str_replace(" ", "", $xls_rows); $xls_rows = strip_tags($xls_rows); xls_send( $headers, $xls_rows ); ?>
  3. Amended my code guys on some advise from a mate, this seems to have stopped it displaying in the browser it now displays the following error when run : Parse error: syntax error, unexpected $end in /data/members/paid/a/d/******/htdocs/smb1buddylist/download.php on line 43 <?php include("config.php"); mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); $CSV = "ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks\n"; $file ="report_". date("Y-m-d"). ".TXT"; $query = "SELECT * FROM Addresses ORDER by ID ASC"; $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); while($myrow = mysql_fetch_array($result)) { $CSV.= $myrow[iD]. "\t"; $CSV.= $myrow[Name]. "\t"; $CSV.= $myrow[DOB]. "\t"; $CSV.= $myrow[HouseNumber]. "\t"; $CSV.= $myrow[street]. "\t"; $CSV.= $myrow[City]. "\t"; $CSV.= $myrow[Country]. "\t"; $CSV.= $myrow[Telephone]. "\t"; $CSV.= $myrow[Fax]. "\t"; $CSV.= $myrow[Email]. "\t"; $CSV.= $myrow[Remarks]. "\n"; header("Content-Type: application/vnd.ms-excel"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download");; header("Content-Disposition: attachment;filename=download.xls ") ?>
  4. Well Im now thinking the problem is here somewhere : header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=excelfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data; ?> [code] My reason for this is (and i could be wrong) is I can change the extension to what ever I want (XLS, RFT , TXT etc) & it makes no diffence to how the browser responds, Im thinking its almost paying no attention to this last part of the code... I Think for some reason its almost stopping before it gets to this part of the PHP, and just generating the result as text in a window... EG I could remove most of the code and get the same output E.g i could run the below and propabley get the same result ? am i making sence here. ? [code] <?php include('config.php'); mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); $result = mysql_query('select * from Addresses'); $count = mysql_num_fields($result); ?> [/code][/code]
  5. Its only a thought here but my hosting provider run linux servers, would this be a problem as I know Excel is not a linux app ? I realise the excel is not directley running on their end and would only have to be on the clients end but would this be a problem ? They also have a list of functions that are disabled but from the below this would not be one of them : # highlight_file # diskfreespace # exec # passthru # system # popen, # pfsockpen # show_source # php_uname # ini_alter # ini_restore # ini_set # getrusage # mysql_list_dbs # get_current_user # set_time_limit # getmyuid # getmypid # dl # leak # listen # chgrp
  6. Andy, really weird why its not working for me, it should work on all browsers really (using firefox 2.0.0.14 and XP Pro OS) - There was an example code on http://www.php-mysql-tutorial.com/php-mysql-select.php which i started to create the code with - the example one they did works fine on http://www.php-mysql-tutorial.com/examples/convert.php - just wont work chen i change it for my needs !
  7. No, tabs didnt work either sorry, I have no experience with pearl and am using a hosted linux solution so i dont think this will be possible.... it must be possible with just php to download to excel ?
  8. Sorry about this but still outputting to the browser !!!! I have changed my code as per last post to the below and my output is STILL in web browser not in excel !!! ahhh going mad now ! (sorry) <?php include("config.php"); mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); $CSV = "ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks\n"; $file ="report_". date("Y-m-d"). ".txt"; $query = "SELECT * FROM Addresses ORDER by id ASC"; $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); while($myrow = mysql_fetch_array($result)) { $CSV.= $myrow[iD]. ","; $CSV.= $myrow[Name]. ","; $CSV.= $myrow[DOB]. ","; $CSV.= $myrow[HouseNumber]. ","; $CSV.= $myrow[street]. ","; $CSV.= $myrow[City]. ","; $CSV.= $myrow[Country]. ","; $CSV.= $myrow[Telephone]. ","; $CSV.= $myrow[Fax]. ","; $CSV.= $myrow[Email]. ","; $CSV.= $myrow[Remarks]. "\n"; } header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$file\""); header("Content-Transfer-Encoding: binary"); if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){ header('Cache-Control: public'); } echo $CSV; exit; ?> My output from this is :_ (FYI the datat is just test data from MYSQL so that part is right, its taking the data... just not diplaying in excel) ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks 50,522380,Ptechnology group,alivemore,tgoater,tgoater@example.com,example,example,Joanne A,jahern@example.com,None 51,358225,eservices,person a,Person 1,example@example.com,example,example,Robert,Rd@example.com,Place order Via EDI link 54,test,test,test,test,test,test,test,test,test,testing server 57,new,new,new,new,new,new,new,new,new,new testing
  9. well im testing in firefox not IE (hate IE) & yes its just displaying in the browser window, not downloading to excel like i wanted it to....
  10. OK i have now got the below but now its still outputting to html again ? well confused now ! <?php require_once("config.php"); $db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password); mysql_select_db($AddressBook_DatabaseName,$db); $query = "SELECT * FROM Addresses"; $result = mysql_query($query) or die('Error, query failed: '.mysql_error()); $tsv = array(); while($row = mysql_fetch_array($result, MYSQL_NUM)) { $tsv[] = implode("\t", $row); } $tsv = implode("\r\n", $tsv); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$filename"); header("Content-Transfer-Encoding: binary"); if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){ // IE cannot download from sessions without a cache header('Cache-Control: public'); } echo $tsv; ?>
  11. Well i changed the code to the below as you said but now i just get a blank page ? have i done something wrong with altering the code ? <?php require_once("config.php"); $db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password); mysql_select_db($AddressBook_DatabaseName,$db); $query = "SELECT * FROM Addresses"; $result = mysql_query($query) or die('Error, query failed: '.mysql_error()); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$filename"); header("Content-Transfer-Encoding: binary"); if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){ header('Cache-Control: public'); } echo $tsv; ?>
  12. got my code 50% working here but its displaying the data in the HTML page not in excel like im asking my code to do, any idears what im doing wrong in the below code ? <?php require_once("config.php"); $db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password); mysql_select_db($AddressBook_DatabaseName,$db); $query = "SELECT * FROM Addresses"; $result = mysql_query($query) or die('Error, query failed: '.mysql_error()); $tsv = array(); while($row = mysql_fetch_array($result, MYSQL_NUM)) { $tsv[] = implode("\t", $row); } $tsv = implode("\r\n", $tsv); $fileName = 'mysql-to-excel.xls'; header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$fileName"); echo $tsv; ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.