[英]Creating csv output with PHP from MySQL table using semicolon as delimiter
I've encounter the problem of creating an csv output with php and mysql. 我遇到了用php和mysql创建csv输出的问题。 The problem is that I cannot show different output based on the number of filled elements on my page.
问题是我无法根据页面上填充元素的数量显示不同的输出。
INTRODUCTION : I have several textareas, users fill in their text or number about something. 简介 :我有几个文本区域,用户填写他们的文字或编号。 Users answers of one question are split in database by semicolon.
用户在数据库中用分号分隔了一个问题的答案。
MySQL table: finish
Columns(fields): pt1, pt2, pt3
GOAL : I want to generate csv that will show users answers in separate columns. 目标 :我想生成一个将在单独的栏中显示用户答案的csv。 If user answers only 1 textarea of each question, the result should be like this:
如果用户仅回答每个问题1个文本区域,则结果应如下所示:
If user fill in all of the textareas for example with number from 1 to 8 - my code below gives the result in csv file like this: 如果用户使用数字(例如1到8)填写所有文本区域,则我的下面代码在CSV文件中给出如下结果:
PROBLEM : Is there something I can do, in order to display result like this: 问题 :有什么我可以做的,以便显示如下结果:
I was wondering if I can do it, to make it somehow add additional columns in excel output and use delimiter (;) to separate the values of the answers from mysql table. 我想知道是否可以做到,以某种方式使其在excel输出中添加其他列,并使用定界符(;)从mysql表中分离出答案的值。
Here is my code. 这是我的代码。
INDEX.PHP 的index.php
<form action="process.php" method="post">
First point: <br>
<textarea rows="1" cols="50" name="point11"></textarea><br>
<textarea rows="1" cols="50" name="point12"></textarea><br>
<textarea rows="1" cols="50" name="point13"></textarea><br>
<textarea rows="1" cols="50" name="point14"></textarea><br><br>
Second point: <br>
<textarea rows="1" cols="50" name="point21"></textarea><br>
<textarea rows="1" cols="50" name="point22"></textarea><br>
<textarea rows="1" cols="50" name="point23"></textarea><br><br>
Third point: <br>
<textarea rows="1" cols="50" name="point31"></textarea><br>
<input type="submit">
</form>
PROCESS.PHP PROCESS.PHP
<?php
for($j=1;$j<101;$j++) {
$point[$j] = "";
}
for($i=1;$i<4;$i++) {
for($k=1;$k<10;$k++) {
if(isset($_POST['point'.$i.$k]) && !empty($_POST['point'.$i.$k])) {
$point[$i] .= $_POST['point'.$i.$k].'; ';
}}}
for ($i=1;$i<4;$i++) {
$pt[] = "pt".$i;
}
$pt_list = implode(',', $pt);
for ($i=1;$i<4;$i++) {
$point_ids[] = $point[$i];
}
$point_list = implode("','", $point_ids);
?>
<?php
$host = 'localhost';
$user = 'root123';
$pass = '123';
$db = 'exampledb';
$table = 'finish';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("INSERT into ".$table."({$pt_list}) VALUES ('{$point_list}')");
echo "The database has been updated";
?>
<form action="export.php" method="post"><input type="submit" value="Export as CSV"></form>
EXPORT.PHP EXPORT.PHP
<?php
$host = 'localhost';
$user = 'root123';
$pass = '123';
$db = 'exampledb';
$table = 'finish';
$file = 'export_';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Please replace the content of your export.php file with the following code hope this helps 请使用以下代码替换export.php文件的内容,希望对您有所帮助
<?php
$host = 'localhost';
$user = 'root123';
$pass = '123';
$db = 'exampledb';
$table = 'finish';
$file = 'export_';
$i = 0;
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$columnName = array();
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
$columnName[] = $row['Field'];
$i++;
}
}
$needle = ';';
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values))
{
for ($j=0;$j<$i;$j++)
{
$colName = $columnName[$j];
$count = strlen($rowr[$j]) - strlen(str_replace(str_split($needle), '', $rowr[$j]));
if ($count > 1)
{
for($p=0;$p<$count;$p++)
{
$colName .= ",";
}
$columnName[$j] = $colName;
$csv_output_column_names .= $columnName[$j].", ";
$csv_output_column_values .= str_replace(';',',',$rowr[$j]).", ";
}
else
{
$csv_output_column_names .= $columnName[$j].", ";
$csv_output_column_values .= $rowr[$j] .", ";
}
}
}
$csv_output = $csv_output_column_names."\n".$csv_output_column_values;
$filename = $file."_".date("Y-m-d");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.