SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    download records as tab delimited text file

    Can someone point me in the right direction? I need to have a listing of records available as a download. There are three fields and I would need the text document to be tab deliminited. Is this very involved? I have php and a postgresql database backend.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function writeFileList$target ) { // $target : parameter for filename to write to

        
    $handle fopen$target'w' ); // Open file and get handle

        
    $query "select * from avaliable_files_table"// Build query
        // Code goes here to connect to data base and choose data base
        
    $results mysql_query$query ); // Execute query
        
    if ( $results ) { // Check for valid resultset
            
    while ( $row mysql_fetch_array$results ) ) {
                
    $tmp[0] = $row[0]; // Get first field data and store in a tmp array
                
    $tmp[1] = $row[1]; // Get second field data and store in a tmp array
                
    $tmp[2] = $row[2]; // Get third field data and store in a tmp array
                
                
    $line implode"\t"$tmp ) ." \n"// Form the line by gluing fields together with tab and add a line break 
                
    if (fwrite($handle$line) === FALSE) { // Was write successfull ?
                    
    fclose$handle ); // No , close file
                    
    return FALSE
                }
            }
            
    fclose$handle ); // Close output file
            
    return TRUE;
        } else {
            
    fclose$handle );
            return 
    FALSE;
        }       

    You will have to do a little tweaking to get this to run right, but should give you an ideal of which way to head.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •