Jump to content

Newbie csv problem


emilyfrazier

Recommended Posts

I am trying to export a csv file. There is a data display list from mySQL, and based on a drop-down the user can select to export CSV file. When this is chosen, here is my code:

[code]

if( $listAction && $listAction == "csv" ){ ?>
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=CMD_Members.csv");



print "Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status". "\n";

$row = 0;

foreach($_POST["all"] as $value){

$sql = "SELECT mbrID, fName, lName, org, email, mbr_pkg, payType, active_date, status FROM members where mbrID = '".$value."'";

// die($sql);
$rval = mysql_query($sql);
while ($data = mysql_fetch_assoc ($rval) ) {
++$row;

if($data["status"] == '0'){
$status = "Pending";
}elseif($data["status"] == '1'){
$status = "Activated";
}elseif($data["status"] == '2'){
$status = "Deactivated";}

print $data["mbrID"] . ",".$data["fName"] . ",". $data["lName"].",".$data["org"] . ",".$data["email"]. ",".$data["mbr_pkg"]. ",".$data["payType"].",".$data["active_date"].",".$status."\n";
}
}
}[/code]

Where the $_POST["all"] is referring to all of the records in the list. So when I do this, the file exports, opens in Excel with an error about my style sheet. It then brings in ALL of the source code into a small box at the top of the page, and then formats my results like they look in the browser display.

Can you tell me what I am doing wrong?

Thanks,
Emily
Link to comment
https://forums.phpfreaks.com/topic/30559-newbie-csv-problem/
Share on other sites

Try removing the "?>" after "if( $listAction && $listAction == "csv" ){", see if that helps.

Also, if you are still having problems, remove the attachment header and change the content type header to be text/plain, then check the output to ensure that it is what you want.

[code]<?php
if( $listAction && $listAction == "csv" ) {
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=CMD_Members.csv");

print "Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status". "\n";
$row = 0;

foreach($_POST["all"] as $value){

$sql = "SELECT mbrID, fName, lName, org, email, mbr_pkg, payType, active_date, status FROM members where mbrID = '".$value."'";

$rval = mysql_query($sql);
while ($data = mysql_fetch_assoc($rval) ) {
++$row;

if($data["status"] == '0'){
$status = "Pending";
}elseif($data["status"] == '1'){
$status = "Activated";
}elseif($data["status"] == '2'){
$status = "Deactivated";
}

print $data["mbrID"] . ",".$data["fName"] . ",". $data["lName"].",".$data["org"] . ",".$data["email"]. ",".$data["mbr_pkg"]. ",".$data["payType"].",".$data["active_date"].",".$status."\n";
}
}
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/30559-newbie-csv-problem/#findComment-140678
Share on other sites

Hi,

Thanks for the help. The bad ?> tag was just a bad copying by me; sorry about that.

So I tried all the options you gave with no luck. When I remove the attachement header and change it to plain text it just prints the code to my screen.Just to be sure I'm doing this right, here is the code I ended up with:
[code]<?php if( $listAction && $listAction == "csv" ){ 
header("Content-type: text/plain");

print "Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status". "\n";

$row = 0;

foreach($_POST["all"] as $value){

$sql = "SELECT mbrID, fName, lName, org, email, mbr_pkg, payType, active_date, status FROM members where mbrID = '".$value."'";

// die($sql);
$rval = mysql_query($sql);
while ($data = mysql_fetch_assoc ($rval) ) {
++$row;

if($data["status"] == '0'){
$status = "Pending";
}elseif($data["status"] == '1'){
$status = "Activated";
}elseif($data["status"] == '2'){
$status = "Deactivated";}

print $data["mbrID"] . ",".$data["fName"] . ",". $data["lName"].",".$data["org"] . ",".$data["email"]. ",".$data["mbr_pkg"]. ",".$data["payType"].",".$data["active_date"].",".$status."\n";
}
}
}
?>[/code]

Thanks again for helping.
Link to comment
https://forums.phpfreaks.com/topic/30559-newbie-csv-problem/#findComment-140690
Share on other sites

try this:

[code]if( $listAction && $listAction == "csv" ) {
header("Content-type: text/plain");

$query = "SELECT * FROM members WHERE mbrID IN('" . implode("', '", $_POST['all']) . "')";

$results = mysql_query($query) or die(mysql_error());

echo "Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status\n";

while ($data = mysql_fetch_array($results, MYSQL_ASSOC)) {
if ($data["status"] == '0') {
$status = "Pending";
} else if ($data["status"] == '1') {
$status = "Activated";
} else if ($data["status"] == '2') {
$status = "Deactivated";
}

echo $data['mbrID'] . "," .
$data['fName'] . "," .
$data['lName'] . "," .
$data['org'] . "," .
$data['email'] . "," .
$data['mbr_pkg'] . "," .
$data['payType'] . "," .
$data['active_date'] . "," .
$status . "\n";
}

exit;
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/30559-newbie-csv-problem/#findComment-140696
Share on other sites

Sorry I took a little while to respond...I thought I had been given up on! Thanks for sticking with me. Since my full code is hundreds of lines, I will just include the pertinent areas.

First, here is the code that gets printed out when I choose the select option for "Export File". This gets printed in the browser directly; does not prompt for download:

[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CMD Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../schedule/style.css" rel="stylesheet" type="text/css">
<script language="javascript">
function doSelect( obj ){
var frm = obj.form;
var val = obj.value;
// alert( val );
if( val == "reset" ){ frm.reset(); }
else{
frm.submit();
}
}

function checkField( id ){
if( document.getElementById && document.getElementById(id) ){
obj = document.getElementById(id);
if( obj.checked ){ obj.checked = false; }
else{ obj.checked = true; }
}
}

<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

</script>


Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status
1,Emily,lastnamehere,org,[email protected],IND25,chk,2006-11-03,Deactivated[/code]

So, in the actual page, here is what I have that is pertinent:[code]<?php require_once( "../schedule/config.php");
$listAction = ( isset( $_POST["resultsAction"] ) && $_POST["resultsAction"] != "")?$_POST["resultsAction"]:false;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CMD Members</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../schedule/style.css" rel="stylesheet" type="text/css">
<script language="javascript">
function doSelect( obj ){
var frm = obj.form;
var val = obj.value;
// alert( val );
if( val == "reset" ){ frm.reset(); }
else{
frm.submit();
}
}

function checkField( id ){
if( document.getElementById && document.getElementById(id) ){
obj = document.getElementById(id);
if( obj.checked ){ obj.checked = false; }
else{ obj.checked = true; }
}
}

<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

</script>
<?php
$myAction = 'onClick="doSelect(this);"';

$date = date("Y-m-d");
$nextmonth = date('Ymd', mktime(0, 0, 0, date("m"), date("d")+30,  date("Y")));
$lastmonth = date('Ymd', mktime(0, 0, 0, date("m"), date("d")-30,  date("Y")));

if( $listAction && $listAction == "csv" ) {
header("Content-type: text/plain");

$query = "SELECT * FROM members WHERE mbrID IN('" . implode("', '", $_POST['all']) . "')";
$results = mysql_query($query) or die(mysql_error());

echo "Id, First Name, Last Name, Organization, Email Address, Package, Pay By, Date Modified, Status\n";

while ($data = mysql_fetch_array($results, MYSQL_ASSOC)) {
if ($data["status"] == '0') {
$status = "Pending";
} else if ($data["status"] == '1') {
$status = "Activated";
} else if ($data["status"] == '2') {
$status = "Deactivated";
}

echo $data['mbrID'] . "," .
$data['fName'] . "," .
$data['lName'] . "," .
$data['org'] . "," .
$data['email'] . "," .
$data['mbr_pkg'] . "," .
$data['payType'] . "," .
$data['active_date'] . "," .
$status . "\n";
}

exit;
} // End If $listAction == csv ?>
</head>
<body>
<?php
$sql = "SELECT * FROM members ORDER BY status";
$rval = mysql_query( $sql );
$records_found = mysql_num_rows ($rval);
?>
<hr />
<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" name="members" method="post">
<div align="center"><span class="textbold">Records Found: <?php echo $records_found; ?><br><br></span></div>
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="4">
<tr>
<td colspan="8" align="center">
<select name="resultsAction" id="resultsAction">
<option value="">:: SELECT ACTION HERE ::</option>
<option value="csv" <?php echo $myAction; ?>>Export File</option>
<option value="reset" <?php echo $myAction; ?>>Reset Check Boxes</option>
</select><br><br>
</td>
</tr>

<tr bgcolor="#CCCCCC" class="textbold">
<td>&nbsp;<br /> </td>
<td>Id</td>
<td>Name</td>
<td>Organization</td>
<td>Email</td>
<td>Package</td>
<td>Pay By</td>
<td>Date Modified</td>
<td>Status</td>
<td width="1%">&nbsp;</td>
</tr>

<?php

$row = '0';
while($data = mysql_fetch_assoc ($rval) ){
++$row;
$bColor = $row % 2 == 0?"#CCCCCC":"#E0E0E0";
$link = "member_detail.php?id=".$data["mbrID"];
$datemysql = date ( "Y m d H:i:s" , strtotime ( $data [ 'date_joined' ]));

if($data["status"] == '0'){ ?>

<tr class="red" style="background:<?php echo $bColor; ?>;">
<td><input tabindex="<?php echo $row; ?>" type="checkbox" name="title[]" id="<?php echo $data["mbrID"]; ?>" value="<?php echo $data["mbrID"]; ?>" /></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php  if( $data["mbrID"] != "" ){ echo $data["mbrID"] ; }else{ echo "&nbsp;"; } ?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php  if( $data["fName"] && $data["fName"] != "" ){ echo $data["fName"] ." ". $data["lName"]; }else{ echo "&nbsp;"; } ?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if( $data["org"] && $data["org"] != "" ){ echo $data["org"]; }else{ echo "-";}?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if( $data["email"] && $data["email"] != "" ){ echo $data["email"];}else{echo "-";}?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if($data["mbr_pkg"] && $data["mbr_pkg"] != "" ){echo $data["mbr_pkg"];}else{echo "&nbsp;";}?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if($data["payType"] && $data["payType"] != "" ){echo strtoupper($data["payType"]);}else{echo "&nbsp;";}?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if($data["active_date"] && $data["active_date"] != ""){echo $data["active_date"];}else{echo "&nbsp;";}?></a></td>
<td><a href="<?php echo $link; ?>" target="_blank" class="red"><?php if($data["status"] == '1' ) {echo 'Activated';}elseif($data["status"] == '2'){echo 'Deactivated!';}else{echo 'Pending';}?></a></td>
<td width="1%"><input type="hidden" tabindex="<?php echo $row;?>" name="all[]" id="<?php echo $data["mbrID"];?>" value="<?php echo $data["mbrID"];?>" /></td>
</tr>

<?php
} // End If Status == 0
} // End While $data loop
?>

</table>
</body>
</html>[/code]

I also tried removing the if( $listAction && $listAction == "csv" ) area out of the head section and into the body, but this did not make any difference...Also, while I understand most of this code, I did not write the javascript function doSelect. I took this on from someone else who had written those functions using Radio boxes (which I then changed to a select box) and who I was collaborating with. So I am still a newbie; don't assume I know too too much! Thanks,
Emily
Link to comment
https://forums.phpfreaks.com/topic/30559-newbie-csv-problem/#findComment-145284
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.