如何在数据库到Excel的条目中添加新行

时间:2013-12-13 13:12:57

标签: php html excel

问题在于$ fetch == 29的最后一个条目。如果我没有在我的选择页面中选择最后一个选项,则所有其他条目不会出现在下一行中。有没有办法把\ n放在其他地方,以便我选择哪个条目并不重要,它总是为每一行提供一个新行。

以下是代码:

<?php

header("Content-type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename=Mr Lucky - Selected Backcheck data.csv');
//connection

$dbhost = 'xxx.xxx.xxx.xxx';
$dbuser = 'xxxxx';
$dbpass = 'xxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) {
    echo "Error connection";
}
//select db
$select_db = mysql_select_db('xxxxxxxxx_ssi', $conn);
if (!$select_db) {
    echo "Error to select database";
}

mysql_set_charset("utf8", $conn);

//Mysql query to get records from database
$user_query = mysql_query('SELECT details_ID, details_Interviewer, details_Name,        details_Phone, details_Address, details_City, details_Age, details_Gender, details_SEC,      details_Education, details_Occupation, details_time, details_TomMQ1, details_TomMQ4, details_L1M, details_favSalty, details_Q11Top, details_Q11Top2, details_ST1, details_ST2, details_ST3, details_ST4, details_ST5, details_Lays, details_KK, details_Bingo, details_Balaji, details_Q15, details_AdSeen FROM lucky44_data5 WHERE details_last=1');

//Code to show headings
$contents = "Password\t Interviewer code\t Name\t Phone\t Address\t City\t Age\t Gender\t SEC\t Education\t Occupation\t Time taken(mins)\t TOM MQ1(Macro)\t TOM Q4a (Salty)\t L1M Q7c\t Favorite salty Q5a\t Consideration top Box (Q11a)\t C Top 2 Box (Q11a)\t Brand I love\t Tastes better than other brands\t Have flavors you like\t Offers flavors which are different from others\t Have great advertising\t No. of statements endorsed for Lays\t No. of statements endorsed for KK\t No. of statements endorsed for Bingo\t No. of statements endorsed for Balaji\t Q15-Most Popular brand\t No. of seen ads (out of 5)\n";


//While loop to fetch the records

while ($row = mysql_fetch_array($user_query)) {

    $fetch = $_POST['name1'];
    if ($fetch == 1) {
        $contents .= $row['details_ID'] . "\t";
    }

    $fetch = $_POST['name2'];
    if ($fetch == 2) {
        $contents .= $row['details_Interviewer'] . "\t";
    }

    $fetch = $_POST['name3'];
    if ($fetch == 3) {
        $contents .= $row['details_Name'] . "\t";
    }

    $fetch = $_POST['name4'];
    if ($fetch == 4) {
        $contents .= $row['details_Phone'] . "\t";
    }

    $fetch = $_POST['name5'];
    if ($fetch == 5) {
        $contents .= $row['details_Address'] . "\t";
    }

    $fetch = $_POST['name6'];
    if ($fetch == 6) {
        $contents .= $row['details_City'] . "\t";
    }

    $fetch = $_POST['name7'];
    if ($fetch == 7) {
        $contents .= $row['details_Age'] . "\t";
    }

    $fetch = $_POST['name8'];
    if ($fetch == 8) {
        $contents .= $row['details_Gender'] . "\t";
    }

    $fetch = $_POST['name9'];
    if ($fetch == 9) {
        $contents .= $row['details_SEC'] . "\t";
    }

    $fetch = $_POST['name10'];
    if ($fetch == 10) {
        $contents .= $row['details_Education'] . "\t";
    }

    $fetch = $_POST['name11'];
    if ($fetch == 11) {
        $contents .= $row['details_Occupation'] . "\t";
    }

    $fetch = $_POST['name12'];
    if ($fetch == 12) {
        $contents .= $row['details_time'] . "\t";
    }

    $fetch = $_POST['name13'];
    if ($fetch == 13) {
        $contents .= $row['details_TomMQ1'] . "\t";
    }

    $fetch = $_POST['name14'];
    if ($fetch == 14) {
        $contents .= $row['details_TomMQ4'] . "\t";
    }

    $fetch = $_POST['name15'];
    if ($fetch == 15) {
        $contents .= $row['details_L1M'] . "\t";
    }

    $fetch = $_POST['name16'];
    if ($fetch == 16) {
        $contents .= $row['details_favSalty'] . "\t";
    }

    $fetch = $_POST['name17'];
    if ($fetch == 17) {
        $contents .= $row['details_Q11Top'] . "\t";
    }

    $fetch = $_POST['name18'];
    if ($fetch == 18) {
        $contents .= $row['details_Q11Top2'] . "\t";
    }

    $fetch = $_POST['name19'];
    if ($fetch == 19) {
        $contents .= $row['details_ST1'] . "\t";
    }

    $fetch = $_POST['name20'];
    if ($fetch == 20) {
        $contents .= $row['details_ST2'] . "\t";
    }

    $fetch = $_POST['name21'];
    if ($fetch == 21) {
        $contents .= $row['details_ST3'] . "\t";
    }

    $fetch = $_POST['name22'];
    if ($fetch == 22) {
        $contents .= $row['details_ST4'] . "\t";
    }

    $fetch = $_POST['name23'];
    if ($fetch == 23) {
        $contents .= $row['details_ST5'] . "\t";
    }

    $fetch = $_POST['name24'];
    if ($fetch == 24) {
        $contents .= $row['details_Lays'] . "\t";
    }

    $fetch = $_POST['name25'];
    if ($fetch == 25) {
        $contents .= $row['details_KK'] . "\t";
    }

    $fetch = $_POST['name26'];
    if ($fetch == 26) {
        $contents .= $row['details_Bingo'] . "\t";
    }

    $fetch = $_POST['name27'];
    if ($fetch == 27) {
        $contents .= $row['details_Balaji'] . "\t";
    }

    $fetch = $_POST['name28'];
    if ($fetch == 28) {
        $contents .= $row['details_Q15'] . "\t";
    }

    $fetch = $_POST['name29'];
    if ($fetch == 29) {
        $contents .= $row['details_AdSeen'] . "\n";
    }
}

$contents_final = chr(255) . chr(254) . mb_convert_encoding($contents, "UTF-16LE", "UTF-8");

print $contents_final;

2 个答案:

答案 0 :(得分:1)

您可以尝试将值放入数组,然后将它们放入其中:

$tmpRow = array();
$tmpRow[] = $row['data_1'];
$tmpRow[] = $row['data_2'];
...
$tmpRow[] = $row['data_n'];

$contents .= implode("\t", $tmpRow) . "\n";

这样,你有多少列无关紧要。它们都将被\ t字符加入,并且该行最后会有一个\ n。

答案 1 :(得分:0)

$tmp = array();
$tmp[] = $row['data_1'];


// your code here


$tmp[] = $row['data_n'];

$contents .= implode("\t", $tmp) . "\n";