downloadsingledata_csv.php

<?php 
/*mysql_connect("mysql1408.opentransfer.com", "A894703_Allspa13", "Beauty_13") or die(mysql_error());
mysql_select_db("A894703_beautyspa") or die(mysql_error());*/
$id = $_GET['id'];
include('db.php');
$count = 0;

$sqlquery = "SELECT s.Companyname,s.ContactPerson,s.EmailId,s.Url,cp.`city_id`, rc.`RelatedCatagory`,ck.MainCatagory,st.state  FROM `spa` s left join relatedcatagories rc on rc.spaid=s.id
left join catagorykeywords ck on ck.id=rc.mainCatagoryid
left join subcatagorykeywords sub on sub.id=rc.RelatedCatagory
left join city_place cp on cp.spaid=s.id
left join states st on st.state_id=cp.state_id
left join cities c on c.city_id=cp.city_id where s.id='$id'" ;
$result = mysql_query($sqlquery) or die(mysql_error()); 
$row1 = mysql_fetch_array($result); 

$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++)  {
    $header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result))  {
  $line = '';
  foreach($row as $value)   {
    if(!isset($value) || $value == "")  {
      $value = "\t";
    }   else  {
# important to escape any quotes to preserve them in the data.
      $value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
      $value = '"' . $value . '"' . "\t";
    }
    $line .= $value;
  }
  $data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
  $data = str_replace("\r", "", $data);


# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
  $data = "\nno matching records found\n";
}

$count = mysql_num_fields($result);



# This line will stream the file to the user rather than spray it across the screen
 header("Content-type: application/octet-stream");
//header("Content-type: text/plain");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=reg_user1.xls");

header("Pragma: no-cache");
header("Expires: 0");

//echo $header."\n".$data;
echo $header."\n".$data."\n";
?>

edit.php

<td><a href="downloadsingledata_csv.php?id='.$spaid.'">Download</a></td>

if i click download it showing (no matches found) empty sheet. if anyone field is empty then remaining data should display in excel.
please someone help me

Member Avatar for diafol

Not sure what your question is regarding what works and what doesn't. Could you clarify please?

i want to download some data in excel sheet from edit.php page, in that i have some fields like Companyname and etc, these data's i was shown in table format. and one more field download. here i want to download each data seperate based on s.id

Thank you urtrivedi. it works fine. and i want to change the field names instead of database field names like city_id as City Name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.