Activity Stream
48,167 MEMBERS
6775 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 4 of 4
  1.     
    #1
    Member

    Default 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"
    But i got only 1 definition :

    PHP Code: 
    $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 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_insert0, -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
    ElitePirate 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




  2.   Sponsored Links

  3.     
    #2
    Respected 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.

  4.     
    #3
    Member

    Default Thanx

    Thanx ... This post is really use full ...

  5.     
    #4
    Respected Member
    Glad to help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. OVH Query?
    By BJ@008 in forum Hosting Discussion
    Replies: 11
    Last Post: 17th Feb 2012, 03:33 PM
  2. SQL query help
    By googleplus in forum Web Development Area
    Replies: 8
    Last Post: 9th Feb 2012, 05:31 PM
  3. FTP Upload Query
    By Ryan in forum File Host Discussion
    Replies: 1
    Last Post: 16th Jan 2012, 03:03 PM
  4. ddl submission query
    By avRo in forum Forum and DDL Discussion
    Replies: 4
    Last Post: 19th Nov 2011, 05:39 PM
  5. php mod query
    By Ryan in forum phpBB
    Replies: 3
    Last Post: 28th Mar 2009, 10:49 PM

Tags for this Thread

BE SOCIAL