Write to CSV from PHP

Discussion in 'PHP' started by PoPSiCLe, Mar 27, 2009.

  1. #1
    Hey. I have been trying to get a memberlist going, and have succeded in that - however, I was planning to have the memberlist print to file, if the user wants to have it - I don't have to have anything fancy, and I can just recreate the file if needed (no need for separate filenames or anything like that), so what I am wondering is: how can I turn the script I already have working into also creating the CSV-file?

    The current script is as follows:
    
    <?php
    if(!session_is_registered('user_name') || (session_is_registered('user_name') && (!$ismoderator && !$isadmin))){
    	echo $access_warning;
    } else { 
    $fields = array('e_mail','phone','birth_year');
    $fields_to_select = array();
    	if (isset($_POST["mem_address"])) {
    		$fields_to_select[] = "mdl_street_address, mdl_post_num, mdl_post_area";
    	}
    	if (isset($_POST["mem_name"])) {
    		$fields_to_select[] = "mdl_first_name, mdl_sur_name";
    	}
    	foreach ($fields as $f)
    		if (isset($_POST["pm_$f"])) {
    			$fields_to_select[] = "mdl_$f";
    		}
    	$members_total = mysql_query("SELECT ". JOIN(', ', $fields_to_select) . " FROM $ppm ORDER BY mdl_sur_name");
    	$member_num=mysql_numrows($members_total);
    ?>
    
    <h3>Antall påmeldte: <?php echo $member_num; ?></h3>
    <p class="warning">Denne siden viser en liste over alle som har puljepåmeldt seg. Dette er ikke nødvendigvis en komplett liste over besøkende på RegnCon!</p>
    
    <table id="members">
    	<tr>
    <?php
    if (isset($_POST["mem_address"])) {
    		$address_heading = "<th>Adresse:</th>";
    	}
    if (isset($_POST["mem_name"])) {
    		$name_heading = "<th>Navn:</th>";
    	}
    if (isset($_POST["pm_e_mail"])) {
    		$email_heading = "<th>Epost:</th>";
    	}
    if (isset($_POST["pm_phone"])) {
    		$phone_heading = "<th>Telefon:</th>";
    	}
    if (isset($_POST["pm_birth_year"])) {
    		$date_heading = "<th>Født:</th>";
    	}
    		echo "$name_heading$address_heading$email_heading$phone_heading$date_heading";
    ?>
    	</tr>
    <?php 
    $i=0;
    while ($i < $member_num) {
    $members=mysql_fetch_array($members_total,MYSQL_BOTH);
    
    $street_address = ucwords(strtolower(str_replace("-","- ",$members[mdl_street_address])));
    $street_address = str_replace("- ","-",$street_address);
    $post_area = ucfirst(strtolower($members[mdl_post_area]));
    $first_name = ucwords(strtolower(str_replace("-","- ",$members[mdl_first_name])));
    $first_name = str_replace("- ","-",$first_name);
    $last_name = ucwords(strtolower(str_replace("-","- ",$members[mdl_sur_name])));
    $last_name = str_replace("- ","-",$last_name);
    $mdl_name = "$first_name $last_name";
    $email = strtolower($members[mdl_e_mail]);
    if (isset($_POST["pm_birth_year"])) {
    $birthdate = explode("-", $members[mdl_birth_year]);
    $birthdate[1] = ucfirst(str_replace($month_num,$month_alpha,$birthdate[1]));
    $birthdate[1] = substr($birthdate[1],0,3); }
    
    if (isset($_POST["mem_address"])) {
    		$address = "<td>$street_address, $members[mdl_post_num] $post_area</td>";
    	}
    if (isset($_POST["mem_name"])) {
    		$name = "<td>$mdl_name</td>";
    	}
    if (isset($_POST["pm_e_mail"])) {
    		$mail = "<td><a href='mailto:$email'>$email</a></td>";
    	}
    if (isset($_POST["pm_phone"])) {
    		$phone = "<td class='a_center'>$members[mdl_phone]</td>";
    	}
    if (isset($_POST["pm_birth_year"])) {
    		$birthdate = "<td class='a_right'>$birthdate[2]-$birthdate[1]-$birthdate[0]</td>";
    	}
    	echo "<tr>
    		$name$address$mail$phone$birthdate
    		</tr>";
    	
    $i++;
    }
    
    ?>
    </table>
    
    <?php
    }
    ?>
    
    PHP:
    it gets its $_POST-variables from a form submitted from the admin-page, where the user can select which fields s/he wants to have on the list. I would like the same choices to be printed in the csv-file. If the CSV-file is automatically generated when the list is being populated, that is fine.

    I was looking into the fputcsv() function, and I got it working somewhat, but I couldn't get the script to generate the file the correct way.

    Anyone able to help with some examples?
     
    PoPSiCLe, Mar 27, 2009 IP
  2. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #2
    Why dont you use the fwrite function to write these values in to a text file and simply name that file with a csv extension. You can accomplish this by opening the file in append mode in writing data in the while loop and separating the values with commas

    please ask back if i was unclear
     
    bhagwant.banger, Mar 27, 2009 IP
    PoPSiCLe likes this.
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Duh... I feel stupid now. I was thinking "okay, there is a function for it, lets use it" and didn't think of the simple approach. Thanks for setting me straight :)
     
    PoPSiCLe, Mar 27, 2009 IP