Support » Developing with WordPress » Transpose table to column

  • $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
    ID: 5xxxx
    SUNY New Paltz, BS, MS

    $result = mysqli_query($link, $SQL) ;
    // Download file header record
    fwrite($output, array(
    while ($row = mysqli_fetch_assoc($result)){
    	// first output the record for download file...
    	fputcsv($output, array(
    } //end while fetch_assoc
Viewing 6 replies - 1 through 6 (of 6 total)
  • Joy


    This is not a WordPress question. Try StackOverflow.

    Thread Starter hsysgrp


    I am attempting to download data from a WordPress db..

    Moderator 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 1 year, 1 month ago by bcworkz.


    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


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

    Thread Starter 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)
  • The topic ‘Transpose table to column’ is closed to new replies.