Support » Developing with WordPress » Transpose table to column

  • hsysgrp

    (@hsysgrp)


    $SQL = "SELECT LastName, FirstName, HomePhone, CellPhone, Address1, City, State, Zip, Email, ID, College1, College2, College3 FROM AAUW_Members ORDER BY Lastname;
    How do I transpose the query results to output the following format to a text file? I can output results to a table, I need a column.
    ….
    Axxx, Gxxx
    (H) 845/xxx-7398
    (C) 914/xxx-5533
    15 Dxxx Run Road
    Poughkeepsie, NY 12603
    gxxxx24@verizon.net
    ID: 5xxxx
    SUNY New Paltz, BS, MS
    ……

    $result = mysqli_query($link, $SQL) ;
    
    // Download file header record
    fwrite($output, array(
    	'LastName',
    	'FirstName',
    	'HomePhone',
    	'CellPhone',
    	'Address1',
    	'City',
    	'State',
    	'Zip',
    	'Email',
    	'ID',
    	'College1',
    	'College2',
    	'College3',
    	));
    
    while ($row = mysqli_fetch_assoc($result)){
    	// first output the record for download file...
    	fputcsv($output, array(
    		$row['LastName'],
    		$row['FirstName'],
    		$row['HomePhone'],
    		$row['CellPhone'],
    		$row['Address1'],
    		$row['city'],
    		$row['State'],
    		$row['Zip'],
    		$row['Email'],
    		$row['College1'],
    		$row['College2'],
    		$row['College3'],
    		
    	));	
    		
    } //end while fetch_assoc
    
    fclose($output);
Viewing 6 replies - 1 through 6 (of 6 total)
  • Joy

    (@joyously)

    This is not a WordPress question. Try StackOverflow.

    Thread Starter hsysgrp

    (@hsysgrp)

    I am attempting to download data from a WordPress db..
    https://hsysgrp.com/download-data/directory-file/

    Moderator bcworkz

    (@bcworkz)

    fputcsv() is going to write in CSV structured format. You don’t want the header record that goes with it, either. Use a generic file writing function like fwrite(). Write "\n" (new line char) after each line’s worth of output. Use "\r\n" instead if writing for a Windows text file. For example:
    fwrite( $output, "{$row['LastName'], {$row['FirstName']}\n}");

    • This reply was modified 2 months ago by bcworkz.
    RossMitchell

    (@rossmitchell)

    I think that you are asking for an output which has one column for each member. Is this correct ?
    Easiest way is:
    – to create the CSV file that WordPress wants to write which has one row per member
    – Load this file into a spreadsheet.
    – Select the whole row + column range in the spreadsheet
    – perform a transpose on the data

    Thread Starter hsysgrp

    (@hsysgrp)

    fwrite( $output, “{$row[‘LastName’], {$row[‘FirstName’]}\n}”); elicits parse error…

    Thread Starter hsysgrp

    (@hsysgrp)

    It’s not elegant, but it works. Thank you, you got me started in the right direction.

    while ($row = mysqli_fetch_assoc($result)){
    	$LastName = $row['LastName'];
    	$FirstName = $row['FirstName']."\n";
    	$HomePhone = $row['HomePhone']."\n";
    	$CellPhone = $row['CellPhone']."\n";
    	$Address1 = $row['Address1']."\n";
    	$City = $row['City']."\n";
    	$State = $row['State']."\n";
    	$Zip = $row['Zip']."\n"; 
    	$Email = $row['Email']."\n";
    	$ID = $row['ID']."\n";
    	$College1 =$row['College1']."\n";
    	$College2 =$row['College2']."\n";
    	$College3 =$row['College3']."\n";
    	fwrite( $output, $LastName . ", " . $FirstName );
    	fwrite( $output, $HomePhone); 
    	fwrite( $output, $CellPhone); 
    	fwrite( $output, $Address1); 
    	fwrite( $output, $City);
    	fwrite ( $output, $State ); 
    	fwrite ( $output, $Zip ); 
    	fwrite ( $output, $Email ); 
    	fwrite ( $output, $ID ); 
    	fwrite ( $output, $College1 );
    	fwrite ( $output, $College2 );
    	fwrite ( $output, $College3 );
    
    		
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.