Results 1 to 4 of 4
-
8th Sep 2012, 02:49 AM #1OPMember
PHP - CSV query
Hi
Pls refer bellow mentioned code.. this code will prompt 2 hyperlinks when we click that it will export the result to CSV file..
My problem is i can set the conditions in where clause for 2 links individually.. but i need to select the different table columns.. example for these 2 csv export links, i can set the condition in
PHP Code:'where'=>"WHERE id=1",
PHP Code:$sql_query = "select * from $table ".$where;
for example i need something like this :
PHP Code:$sql_query1 = "select id,name from $table ".$where;
$sql_query2 = "select firstname,lastname from $table ".$where;
$sql_query3= "select location from $table ".$where;
This is the full code :
PHP Code:<?php
function exportMysqlToCsv($table,$where = '',$filename = 'Report.csv')
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "select * from $table ".$where;
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result))
{
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++)
{
if ($row[$j] == '0' || $row[$j] != '')
{
if ($csv_enclosed == '')
{
$schema_insert .= $row[$j];
} else
{
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else
{
$schema_insert .= '';
}
if ($j < $fields_cnt - 1)
{
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// 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");
echo $out;
exit;
}
// please assign more table with assoiciated name.
$lst_csv_req = array(
'All Employees Records'=>
array(
'table'=>'tbl1',
'where'=>"WHERE id=1",
'db'=>'testdb'),
'All Employees Records2'=>
array(
'table'=>'tbl2',
'where'=>"WHERE id=1",
'db'=>'testdb')
);
if(isset($_GET['action'])):
$host = 'localhost'; // MYSQL database adress
$db = 'testdb'; // MYSQL database name
$user = 'testdb'; // Mysql Datbase user
$pass = 'testdb'; // Mysql Datbase password
// Connect to the database
$link = mysql_connect($host, $user, $pass);
//mysql_select_db($db);
if(array_key_exists ( $_GET['action'] , $lst_csv_req)) {
$req_arr = $lst_csv_req[$_GET['action']];
if(!isset($req_arr['table']) || trim($req_arr['table']) == '')
{
echo 'Please check array. table name not entered';
exit;
}
if(!isset($req_arr['db']) || trim($req_arr['db']) == '')
{
echo 'Please check array. DB name not entered';
exit;
}
// $where = ' '.@$req_arr['table'].' ';
$where = ' '.@$req_arr['where'].' ';
mysql_select_db($req_arr['db']);
$table= $req_arr['table']; // this is the tablename that you want to export to csv from mysql.
exportMysqlToCsv($table, $where);
}
else
{
echo "requested report does not exist.";
}
endif;
// generate link:
echo "<strong>Report download link:</strong><br />
--------------------------------------------<br />";
foreach($lst_csv_req as $key=>$vale):
$link = $_SERVER['PHP_SELF']."?action={$key}";
echo "<a href='$link '>{$key} - [ Download Report ]<br /></a>";
endforeach;
?>
ThanksElitePirate Reviewed by ElitePirate on . PHP - CSV query Hi Pls refer bellow mentioned code.. this code will prompt 2 hyperlinks when we click that it will export the result to CSV file.. My problem is i can set the conditions in where clause for 2 links individually.. but i need to select the different table columns.. example for these 2 csv export links, i can set the condition in 'where'=>"WHERE id=1", But i got only 1 definition : Rating: 5
-
9th Sep 2012, 04:36 PM #2Respected Member
Sorry but I am really confused as to what you want and how you want to accomplish it.
How is the program getting the fields it wants from a form posting data???
the sql query is smple enough:
if ($table == 1) {$where = " where id =1 "; }
elseif ($table == 2) {$where = " where name = 'Long' "; }
else {$where = ""; }
$query = "select $fields from $table $where";
In your post data passed to the program would be table name , table fields (commas seperated) and the where statement.
I don't know how you used decided the id=1 in your example where but where could be based on table selection.
-
12th Sep 2012, 03:58 PM #3Member
Thanx
Thanx ... This post is really use full ...
-
14th Sep 2012, 02:35 PM #4Respected Member
Glad to help.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
OVH Query?
By BJ@008 in forum Hosting DiscussionReplies: 11Last Post: 17th Feb 2012, 03:33 PM -
SQL query help
By googleplus in forum Web Development AreaReplies: 8Last Post: 9th Feb 2012, 05:31 PM -
FTP Upload Query
By Ryan in forum File Host DiscussionReplies: 1Last Post: 16th Jan 2012, 03:03 PM -
ddl submission query
By avRo in forum Forum and DDL DiscussionReplies: 4Last Post: 19th Nov 2011, 05:39 PM -
php mod query
By Ryan in forum phpBBReplies: 3Last Post: 28th Mar 2009, 10:49 PM
themaManager - edit and manage...
Version 4.04 released. Open older version (or...