多年来,我一直在尝试让这段代码工作,并研究了其他问题,但它对我来说并不起作用。任何指导都是非常感谢的。
在上下文中,有一个sql查询来返回今天条目的所有结果。这些文件每天都会转换为CSV文件。要导入到软件中,则每行的末尾必须返回"“和一个回车,但最后一行不应添加回车。
代码如下:
$xo1 = "\"\"";
$xo2 = "\r\n";
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
if(++$i === $columns_total){
$xo1 = "";
$xo2 = "";}
}
$output .= $xo1;
$output .= $xo2;
}发布于 2014-01-06 20:26:59
如果我没答错的话,我想rtrim()应该来做这项工作。
while ( $row = mysql_fetch_array( $sql ) ) {
foreach( $row as $column ) {
$output .= sprintf( '"%s",""' . "\r\n" , $column );
}
}
rtrim( $output, '""' . "\r\n" );发布于 2014-01-06 20:32:40
你也可以使用循环的索引来解决这个问题。当索引大于零时,应用$xo1和$xo2。例如:
for ($i = 0; $i < $columns_total; $i++) {
if ( $i > 0 ) {
$output .= $xo1 . $xo2;
}
// now apply other workings
$output .='"'.$row["$i"].'",';
}https://stackoverflow.com/questions/20949900
复制相似问题