0

私のMYSQLデータベースには、この形式の列へのヘッダーがたくさんあり2345X32X12....... 2345=survey id, 32=page id, 12= question id.ます..

各ヘッダーの下には、その特定の ID セットに関する質問が含まれています。PHPを使用して、各IDセットの各質問のテキストを取得してcsvファイルに配置しようとしています。

現在、Web サーバーから csv ファイルを開く(2345X32X12)と、Excel ファイルの列のヘッダーとしてその形式が取得されます。

各列の下の質問を次のように表示したい - あなたの年齢は?........これが理にかなっていることを願っています

私のデータベースの別のテーブルでは、番号付きのヘッダーが質問を定義しています-A1: 2345X32X12 B1: あなたの年齢は?

コード:

<?php
// connection with the database 
//$sid =$_SESSION['sid'];
//echo $sid;
$id=$_GET['sid']; 
echo $id;

$dbhost= ""; //your MySQL Server 
$dbuser = ""; //your MySQL User Name 
$dbpass = ""; //your MySQL Password 
$dbname = "y";    
$link = mysql_connect($host, $user, $pass);
mysql_select_db($dbname); 
$tablename = "survey_".$id; // this is the tablename that you want to export to csv from mysql. 
$sql = "Select * from $tablename"; 
//create  code for connecting to mysql 
$Connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $Connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
//execute query 
$result = @mysql_query($sql,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 


exportMysqlToCsv($tablename);
function exportMysqlToCsv($tablename,$filename = 'Results.csv'){
    $sql_query = "select * from $tablename where id=2";
    // Gets the data from the database

    $result = mysql_query($sql_query);

    $f = fopen('php://temp', 'wt');
    $first = true;

    while ($row = mysql_fetch_assoc($result)) {

        if ($first) {

            fputcsv($f, array_keys($row));

            $first = false;
 }
  fputcsv($f, $row);

    } // end while

    $size = ftell($f);
    rewind($f);
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: $size");

 // Output to browser with appropriate mime type, you choose ;)
    header("Content-type: text/x-csv");
    header("Content-type: text/csv");
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");

    fpassthru($f);

    exit;
}

?> 
4

1 に答える 1

1

あなたの質問はあまり明確ではありません。最初に問題はこれらの面白い列名であると言いますが、次に質問をcsvファイルに入れようとしていると言います。片方がもう片方と何の関係があるのか​​わかりません。

とにかく、そのような見出しのある列には、次を使用します。

$ids = explode('X', $heading);

$ids次に、2345、32、12などの数値を取得するためにループできます。データベースの構造について詳しく知らなければ、これをコードのどこに配置するか、またはそれらをどのように処理するかを正確に知ることは困難です。

于 2012-10-16T00:51:00.353 に答える