ok i fixed it myself. if i use a button in the form it submits the post variable to the csv.php script and it inserts only the record that i want in the excel file. yeah!!!!!
code here if anyone else would like to know how to do it.
this code will pop up a dialog box for you to save the file where you want. it has an auto name that uses the date/time stamp to name the file but i guess you could change that to what ever when the dialog box pops up.
this is the code from the main page.
CODE
<form id="form1" name="form1" method="post" action="mycsv.php">
<label>Order ID<br />
<input type="text" name="orderID" id="orderID" />
</label>
<input name="Submit" type="submit" />
</form>
<?php
$orderID = $_POST['orderID'];
$_SESSION['orderID'] = $orderID;
?>
mycsv.php file
CODE
<?php
$host = 'insert your host here'; // MYSQL database host adress
$db = 'insert your database here'; // MYSQL database name
$user = 'insert your database user here'; // Mysql Datbase user
$pass = 'insert database password'; // Mysql Datbase password
// Connect to the database
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
require 'exportcsv.inc.php';
$table="insert your table here"; // this is the tablename that you want to export to csv from mysql.
exportMysqlToCsv($table);
?>
exportcsv.inc.php file
CODE
<?php
function exportMysqlToCsv($table,$filename = 'export.csv')
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "select * from $table WHERE orderID =" .$_POST['orderID']."";
// 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");
$filename = $filename."_".date("Y-m-d_H-i",time());
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
//header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
?>
if you want it to export a single row this will work just change the line below to your variable
CODE
$sql_query = "select * from $table WHERE orderID =" .$_POST['orderID']."";
or if you want it to download the entire contents of the table change it to
CODE
$sql_query = "select * from $table";
This post has been edited by NeekWorld: 2 Jul, 2009 - 09:51 PM