Saturday, August 9, 2008

Export CSV (Excel) format of Data from PHP

require('database_connectivity.php');
Header('Content-type: application/csv');
Header("Content-disposition: attachment; filename=\"Competitions-".date('mdy-Hi').".csv\"");

//Fetching the Question for "exam_question" table
$sql_exam_question = "SELECT user.user_name, user.user_email, user.user_qno, user.optionid, optn.option_text,
user.user_date, ques.ques_text, ques.ques_prize_text
FROM competition_user AS user, exam_question AS ques, competition_options AS optn
WHERE user.user_qno = ques.question_id AND user.user_qno = optn.competition_qno
AND user.optionid = optn.optionid ";
$qry_exam_question = mysql_query($sql_exam_question) or die();

$column_limiter = ",";
$row_limiter = "\n";

//Heading
echo "Name,Email,Question No.,Answer,Question,Prize,Date\n";

while( $fetch_data = mysql_fetch_object($qry_exam_question) ) {

echo $fetch_data->user_name; echo $column_limiter;
echo $fetch_data->user_email; echo $column_limiter;
echo $fetch_data->user_qno; echo $column_limiter;
echo $fetch_data->option_text; echo $column_limiter;
echo $fetch_data->ques_text; echo $column_limiter;
echo $fetch_data->ques_prize_text; echo $column_limiter;
echo $fetch_data->user_date; echo $column_limiter;

echo $row_limiter;
}
?>

1 comment:

Ancy merina said...
This comment has been removed by the author.

Popular Posts