How to give a new line in the entries from database to Excel

The problem is with the last entry where $fetch==29. If I am not choosing this last option in my select page, the all other entries are not coming in next line. Is there a way to put \n somewhere else so that it doesn't matter which entries I select, it always gives a new line for every row.

Here's the code:

<?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;

Answers


You can try putting the values to array and then imploding them:

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

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

This way it doesn't matter how many columns you'll have. They'll all be joined by \t character and the line will have a \n at the end.


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


// your code here


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

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

Need Your Help

Flask/WTF/SQLAlchemy: using QuerySelectMultipleField with Form.populate_obj()

python flask flask-sqlalchemy flask-wtforms

I have a form (WTForms via Flask-WTF) that includes a QuerySelectMultipleField, something like this:

How to keep PHP opening and closing tags in ckeditor source as html entity?

php html ckeditor entity

In ckeditor, I'm sharing code snippets such as php, html, etc. So for example I would share this: