简体   繁体   中英

Write a CSV using PHP

I have three questions in hand

  1. i have no problem in export the data from php to csv. But i have struggled to put the
    in of csv coloumn. Here the detail

    1. PostId PostTitle Reply Ip
      1. 1 test reply 1 127.0.0.1
    2. reply 2
    3. reply 3
    4. 2 test2 response 1 127.0.0.1
    5. response 2

In the above example i have export the all the data's into csv. But in "Reply" column i can not give the
between the reply/response. How i can separate each reply's? if am using $csv_output1 .= strip_tags("\\"".$response."\\""); i getting junk characters in csv

  1. While i write a csv file, i need to wrap the top 3 columns to show the CSV title example "Ticketing Management system", after three rows only it show the data with column title

  2. Is any body can help to use fputcsv()

Here my code

$table = 'ost_ticket'; $file = 'TicketsOpen';

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$csv_output = "SNO,Type,Date/Time Stamp,Subject,Response";//Header start here $csv_output .= "\\n"; $sql='SELECT dept_name,ost_ticket.created,subject,ticket_id FROM ost_ticket, ost_department WHERE ost_ticket.dept_id = ost_department.dept_id AND ost_ticket.created LIKE "'.$viewMonth.'-%" AND ('; $f=$_REQUEST['f']; $k=0; while($k while ($rowr = mysql_fetch_row($values)) { $csv_output.=$sno++.","; for ($j=0;$j // echo $rowr[3]."
"; //echo "
". $sql1='SELECT response FROM ost_ticket_response WHERE ticket_id ="'.$rowr[3].'"'; // Get Multiple response for a single ticket $values1=mysql_query($sql1); while (list($response) = mysql_fetch_row($values1)) { //$response=strip_tags($response,"
"); //echo "
".addslashes($response); $csv_output1 .= strip_tags("\\"".$response."\\"");
} $csv_output .= $csv_output1; //Get multiple response for a single column called //ticket response unset($csv_output1); $csv_output .= "\\n"; }

$filename = $file."_".date("Ym-d_H-i",time()); header("Content-type: application/vnd.ms-excel"); header("Content-disposition: csv" . date("Ym") . ".csv"); header( "Content-disposition: filename=".$filename.".csv"); print $csv_output; exit; ?>

\nI want the csv output as like below \n\n   c1 c2 c3 c4 c5 \n\nR1   \n       -------------- Ticketing Management System ----------------- \n\nR2 \n\nR3 SNO Type Date/Time Stamp Subject Response \n\n\nR4 1. Server Date comes Title of ticket Response 1  
\n Response 2 \n\nR5 2 Tech Date come Title of ticket2 Response 1
\n Response 2
\n Response 3 \n

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