1

I have name,address and images stored in my database.on clicking "GENERATE EXCEL" button a .csv file will be generated.I am using comma as a separator.

my actual o/p should be like this:

name      address           images

peter   "house no#2",uk   img1.png,img2.png,img3.png

but currently i am getting the values with comma in different columns.how can i do this? please help.

this is my code fetching the values from the database:

 $sep = ",";
            $csv_hdr = "Name".$sep."Address".$sep."Images";
            $csv_output="";     
            for($i=$start;$i<$match;$i++)
            {
                $csv_output .=$ARRAY[$i]['source_name'].$sep;
                $i_add=str_replace('"',"",$ARRAY[$i]['source_address']);
                $csv_output.=$ARRAY[$i]['source_address'].$sep;
                $csv_output.=$ARRAY[$i]['source_img']."\n";
            }

this is my 'GENERATE_EXCEL' button with 2 hidden fields csv header and data.

<div id="btnexcel">
        <input type="submit" name="Generate Excel" value="Generate Excel" id="btnreport" formaction="export_to_excel.php"/></div>
        <input type="hidden" value="<?php echo $csv_hdr; ?>" name="csv_hdr">
        <input type="hidden" value="<?php echo $csv_output; ?>" name="csv_output">

this is my export_to_excel.php:

<?php

$out = '';
        $filename_prefix = 'csv';
        if (isset($_GET['csv_hdr'])) {
        $out .= $_GET['csv_hdr'];
        $out .= "\n";
}

if (isset($_GET['csv_output'])) {
$out .= $_GET['csv_output'];
}
$filename = $filename_prefix."_".date("Y-m-d_H-i",time());

//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-Encoding: UTF-8");
header("Content-type: text/csv; charset=UTF-8");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
echo "\xEF\xBB\xBF"; // UTF-8 BOM
//Print the contents of out to the generated file.
print $out;

//Exit the script
exit;
?> 
jermina
  • 101
  • 2
  • 4
  • 13
  • Consider escaping the values. Might be useful: http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file – Ofir Baruch Apr 21 '15 at 06:05
  • I already tried with that double quotes around the ."$ARRAY[$i]['source_name']" but it doesnt seem to work.can u please tell me how to do this?@OfirBaruch – jermina Apr 21 '15 at 06:33
  • Can you add the code that you've used with the double quotes to your question? – Ofir Baruch Apr 21 '15 at 06:39
  • $csv_output.=" ' ".$ARRAY[$i]['source_address']." ' "; I used it this way. – jermina Apr 21 '15 at 06:46
  • You don't really escaping it with double quotes if that's your code. Try this: `$csv_output.='"'.$ARRAY[$i]['source_address'].'"';` . Please notice that you have to also escape that variable's content from `'` before that line. – Ofir Baruch Apr 21 '15 at 06:48
  • i dint get the last line which u said...i did the changes as u said but now it is not showing the address itself. – jermina Apr 21 '15 at 06:56
  • Ok, let's try a different approach. Take a look at the following comment and integrate your code with it: http://php.net/manual/en/function.fputcsv.php#104980 – Ofir Baruch Apr 21 '15 at 07:00
  • Update if it's going good for you – Ofir Baruch Apr 21 '15 at 07:17
  • i saw the code there it seems we are directly fetching all the values from the database.but i want only selected values which i am taking in the form of array as well as i need that for loop bcoz there are few conditions to select for displaying the data.so i cant make out how this approach can be used in such cases... – jermina Apr 21 '15 at 07:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75785/discussion-between-jermina-and-ofir-baruch). – jermina Apr 21 '15 at 09:35

0 Answers0