Status
Not open for further replies.

ElitePirate

Active Member
136
2009
3
80
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:
'where'=>"WHERE id=1",

But i got only 1 definition :

PHP:
$sql_query = "select * from $table ".$where;

can anyone advise me how can i have different select query for these 2 links individually? so i can select any column from tbl1 or tbl2

for example i need something like this :

PHP:
$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:
<?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;
?>


Thanks
 
3 comments
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.
 
Status
Not open for further replies.
Back
Top