HIi, all of you........

I have site in which i have put a link to download a database table in a excel format.

visit this link and download

http://genat.com/anesthesia/index.php?option=com_content&view=article&id=54&Itemid=74

My script is working but when i download the file.

It displays the data in a single row

I want to display each record in a different row.

like this

  1. NAME PLACE CITY
  2. NAME PLACE CITY
  3. NAME PLACE CITY

anybody plz solve the problem. its urgent ..I reallly need the solution.

The code is :

<?php
$TableName = "city";
$select = "SELECT * FROM ".$TableName."";
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
$csv_output .= mysql_field_name($export, $i) . "t";
}

while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "t";
}
$line .= $value;
}
$data .= trim($line)."n";
}
$data = str_replace("r","",$data);

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=database_dump.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output."n".$data;
exit;
?>

Thanks in advance

    Your "t", "n" and "r" should be "\t", "\n", "\r"

      HIi ,

      Thanks for the reply,

      I did this changes in the script.

      BUt still not working.

      Plz anybody check it out ang give me the solution...

      Waiting for your response.

        does PHP_EOL instead of \n do the trick? this would add the system-specific newline operators...

          Did you get it fixed? The file at the link you posted loaded just fine for me (in Excel 2003).

            It's a predefined constant

            $data .= trim($line) . "\n";		// your old code
            
            $data .= trim($line) . PHP_EOL;		// using PHP_EOL instead of \n
            

            "\n" is the new_line character (form feed) and \r is the carriage_return character. Depending on the OS, end of line might be "\n", "\n\r" or "\r".

              hiii

              visit this link and download

              http://www.genat.com/anesthesia/index.php?option=com_jumi&fileid=1&Itemid=73

              The data is in a different row. this is fine

              But all columns are mixed up in a single column.

              i want a different column according to the fields.

              PLz give me rectified code.

              here is the code i have used :

              <?php
              $TableName = "city";
              $select = "SELECT * FROM ".$TableName."";
              $export = mysql_query($select);
              $fields = mysql_num_fields($export);

              for ($i = 0; $i < $fields; $i++) {
              $csv_output .= mysql_field_name($export, $i) . "t";
              }

              while($row = mysql_fetch_row($export)) {
              $line = '';
              foreach($row as $value) {
              if ((!isset($value)) OR ($value == "")) {
              $value = "t";
              } else {
              $value = str_replace('"', '""', $value);
              $value = '"' . $value . '"' . "t";
              }
              $line .= $value;
              }
              $data .= trim($line).PHP_EOL;
              }
              $data = str_replace("r","",$data);

              header("Content-Type: application/vnd.ms-excel");
              header("Content-Disposition: attachment; filename=database_dump.csv");
              header("Pragma: no-cache");
              header("Expires: 0");
              print $csv_output."n".$data;
              exit;
              ?>

                Your "t" and "r" still are not "\r" and "\t". There is a BIG difference.

                "t" is the character "t", not surprisingly. That is, nothing but the plain old letter t.

                "\t" is NOT two characters. It is a tab character.

                '\t' on the other hand are two characters, '\' and '\t', that is, a backslash followed by the letter t.

                The same goes for 'r'. It's the letter r, not the carriage return character.

                What screws up your column issue is the lack of tabs.

                  I have made all the "t" and "r" like this "\t" and "\r".

                  How to make the tabs so that my data will be adjusted in different columns according to the fields.........

                  can u just help me out in this issue also......

                    read this

                    and do some basic experimenting by trying different strings and their output to the browser/console BEFORE you do more complex operations (with browser beware: \n will create a newline in the html source, NOT in the browser display, same goes for \t etc....so have a look at both output AND the HTML source....

                      also read:

                      Zend coding standards

                      and please use [php ] [/php ]tags - explore the icons around the text input area when writing a new post

                      Answer to your question:
                      Does using commas instead of tabs between data fields help?

                      happy reading!

                      Bjom

                        Write a Reply...