0

Hi!

I am developing a php site and required to export the .php files to HTML, PDF, EXCEL, TEXT, CSV, ODS and SXC files. The problem i'm having here is that i don't know anything about exporting files. How should i start? Could anyone provide some steps to help me start up this task? Thanks in advance. Have a good day~:)

3
Contributors
8
Replies
12
Views
8 Years
Discussion Span
Last Post by lcyew
0

Hi lcyew and welcome to DaniWeb :)

Do you mean that you want to provide an export facility, or that you need to export your source code to those formats?

To provide an export facility you need to:

- read the contents of the file to be exported using file_get_contents and change format if necessary for the new file format (might not need to parse)
- open destination file using fopen('w') -> the 'w' means write and create new rather than append
- use the returned resource to write the contents of the file to be exported using fwrite

For more info on these functions (and some good examples of file manipulation) check the documentation at www.php.net.

0

Hi, thanks for your reply.

May i know what is the difference between "to provide an export facility" and "to export your source code to those formats"?

I need it like....i have a table displaying information. Then by clicking (maybe a link), i can get a document in either of the formats mentioned above.

0

Ok, that's what I meant from providing an export facility. From your original description I thought maybe you meant you want to convert your php source code to html or something.

So, your function should do the following:
- get table data
- open a destination file using fopen('w')
- write to the file using fwrite
- close the file using fclose

Hope this gives you a start. Have a try and repost with your code if you get stuck.

0

I've go through some tutorial on fopen, fwrite and fclose. Sorry, but i still don't quite get what should i do.

Let's say i have a file named report.php, with all data on it.

According to what you've said,

- get table data - what if part of my data is directly retrieved from
the database, while the other part is produced by calculation
made on the page?
Will this affect anything?

- open a destination file using fopen('w') - is it if i want to get a .xls
file, and i create the file using fopen?

- write to the file using fwrite - what exactly should i write to the file? All data i want is already there in report.php.

Sorry for bothering you again. Thanks for your help. It's greatly appreciated. :)

0

Creating output files in most of these formats isn't that difficult. To make it even easier, I created a small include module that you can use. You can download it at:
Desktop_Write

0

For example, with CSV file format, each row of data is a row in the csv file and each column is separated by a comma. So what you need to do is this:

// let's say the report.php file has a table of data called $table
// and we want to export that data to the file export.csv
// first we open the file that we want to write to
$export_handle = fopen("export.csv", "w"); // note the "w" format here
// make sure that we opened it
if(! $export_handle)
{
   echo "Error opening file.";
   die;
}
// we need to get each row in the table and write it to the file, so...
foreach($table as $row)
{
   // in each row there are several columns that we need to output
   // note you could also use the implode function here, but for illustration purposes...
   foreach($row as $column)
   {
      $result = fwrite($export_handle, $column . ",");
      // check the write succeeded - $result = number of bytes written
      if($result === false)
      {
         echo "Error in column write.";
         die;
      }
       // each row needs to have a carriage return appended
       $endLine = fwrite($export_handle, "\r\n");
       // check write was ok
       if($endLine === false)
       {
            echo "Error in row write.";
            die;
       }
   }
}
// here we need to cleanup after ourself by closing the file
fclose($export_handle);

Hope this illustration helps. Other formats will be different in what gets written, but the basic algorithm is the same for any export.

0

I attach along my report.php file:

dt_summaryReport.php

Any help is much appreciated:) Thanks again

Attachments
<?php
session_start();

if(!$_SESSION['id'] && !$_SESSION['pass']){ // if session variable "username" does not exist.
header("location:index.php"); // Re-direct to user_registration_form.php
}
else{

include("connect.php");

$query = "SELECT DISTINCT(company_code) FROM company_details_setup ORDER BY company_code";
$result = mysql_query($query) or die("couldn't execute query");

$company_code = $_POST['company_code']; 
$year_select = $_POST['year_select']; //echo $year_select;

$a = 0;
if($company_code!=NULL){
foreach ($company_code as $company_code_ticked)
{
	//echo "$company_code_ticked";
	
	$query1 = "SELECT * FROM employee WHERE company_code = '$company_code_ticked' ORDER BY name";
	$result1 = mysql_query($query1) or die("couldn't execute query");
	$num1 = mysql_num_rows($result1);
	
	if($num1 != 0){$a=$a+1;}
	
	//while($row1 = mysql_fetch_array($result1)){echo $row1['name'];}
}}
//echo $num1;
//echo $a;



//query 2 to 9 is to display leave code of different leaves	
$query100 = "SELECT * FROM annual_leave";										//AL
$result100 = mysql_query($query100) or die("couldn't execute query");	
$row100 = mysql_fetch_array($result100);

$query101 = "SELECT * FROM medical_leave";									//MC
$result101 = mysql_query($query101) or die("couldn't execute query");
$row101 = mysql_fetch_array($result101);

$query102 = "SELECT * FROM study_leave";										//SL
$result102 = mysql_query($query102) or die("couldn't execute query");
$row102 = mysql_fetch_array($result102);

$query103 = "SELECT * FROM compassionate_leave";								//CL
$result103 = mysql_query($query103) or die("couldn't execute query");
$row103 = mysql_fetch_array($result103);

$query104 = "SELECT * FROM marriage_leave";									//MR
$result104 = mysql_query($query104) or die("couldn't execute query");
$row104 = mysql_fetch_array($result104);

$query105 = "SELECT * FROM maternity_leave";									//ML
$result105 = mysql_query($query105) or die("couldn't execute query");
$row105 = mysql_fetch_array($result105);

$query106 = "SELECT * FROM paternity_leave";									//PL
$result106 = mysql_query($query106) or die("couldn't execute query");
$row106 = mysql_fetch_array($result106);

$query107 = "SELECT * FROM nopaid_leave";										//NP
$result107 = mysql_query($query107) or die("couldn't execute query");
$row107 = mysql_fetch_array($result107);

function dateDiff($dformat, $endDate, $beginDate)
{
       $date_parts1=explode($dformat, $beginDate);
       $date_parts2=explode($dformat, $endDate);
       $start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);    // adjust date parts according to date format 102 for d-m-y and 012 for m-d-y
       $end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
       return $end_date - $start_date +1;
}
}									
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>CHJ eLeave Application System</title>
<link rel="stylesheet" type="text/css" href="phpddm.css">
<link href="etr/calendar-win2k-cold-2.css" rel="stylesheet" type="text/css" media="screen,print">
<script language="JavaScript" src="etr/calendar.js"></script>
<script language="JavaScript" src="etr/calendar-en.js"></script>
<script language="JavaScript" src="etr/common.js"></script>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

 
function checkAllSwitchTop() {
var checkVal = false;
if(document.form.check_all_top.checked == true) {
checkVal = true;
}
count = document.form.elements.length;
for (i=0; i < count; i++) {
document.form.elements[i].checked = checkVal;
}
}
	  
function validateCB(theName){
   var counter=0;
   var cb=document.getElementsByName(theName)
   for (i=0; i<cb.length; i++) {
        if((cb[i].tagName=='INPUT')&&(cb[i].type=='checkbox')){
          if (cb[i].checked)
             counter++;
        }
   }
   if (counter==0) {  
   return false;
  }
return true;
}

function check_form(){						
					var number = /^[0-9]+$/;	
				    
					
					if (!validateCB('company_code[]')) {
   					alert("Please select company code");
   					return false;
					 }

					
					else if ( form.year_select.value == "")
					{
						alert("Please fill in the year.");
						document.form.year_select.focus();
						return false;
					}
					
					
					else if ((form.year_select.value.length < 4) || (form.year_select.value.length > 4))
					{
					   	alert("Please insert valid year in 4 digits 'YYYY' format. For example: 2009 ");
						document.form.year_select.focus();
						return false;
					
					}
					
					
					else if (form.year_select.value.match(number))
					{
						return true;
					}
						else{
						alert("Please insert valid year in 'YYYY' format. For example: 2009 ");
						document.form.year_select.focus();
						return false;
					}
					
				  return true;
				  }
//-->
</script>
<style type="text/css">
<!--
.style10 {	font-size: 12px;
	font-weight: bold;
	font-family: Georgia, "Times New Roman", Times, serif;
}
.style12 {	font-size: 10px;
	font-weight: bold;
	color: #0000FF;
}
.style14 {font-size: 12px}
.style17 {font-size: 12px; font-weight: bold; }
.style18 {color: #FFFFFF}
.style20 {color: #0000FF}
.style23 {
	color: #000000;
	font-weight: bold;
}


-->
</style>

</head>

<body>
<table width="1022" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="1022" height="353" bgcolor="#FFFFFF" rowspan="3"><div align="center">
        <table width="822" height="483" align="center">
          <tr>
            <td width="816" height="477"><table width="1011" height="557" align="center">
                <tr>
                  <td height="117" colspan="2"><div align="center"><img src="Banner/Banner.JPG"></div></td>
                </tr>
                <tr>
                  <td height="20" colspan="2"><table width="1007">
                      <tr>
                        <td width="47%" height="26"><div align="left"><span class="style10">
                            <?php include("tarikh.php"); ?>
                        </span></div></td>
                        <td width="47%"><div align="right"><span class="style10"><?php echo "Welcome $_SESSION[name1]"; ?></span></div></td>
                        <?php if($_SESSION[id] && $_SESSION[pass]) {?>
                        <form name="form1" action="logout.php" method="post">
                          <td width="6%"><input type="submit" name="Submit" value="Logout" onClick="Javascript: if(confirm('Logout?')){return true} else return false;"></td>
                        </form>
                        <?php } ?>
                      </tr>
                  </table></td>
                </tr>
				<form name="form" action="dt_summaryReport.php" method="post" onSubmit="return check_form()">
                <tr>
                  <td height="20" colspan="4" background="../../image/lightblue.gif"><div align="center" id="Layer1" style="position:relative; width:1000px; height:20px; z-index:1; overflow: visible; visibility: visible;"> 
                         <?php if($_SESSION['position'] == "Admin"){include('admin_phpddm.php');} elseif($_SESSION['position'] == "Staff"){include('staff_phpddm.php');} elseif($_SESSION['position'] == "Superior"){include('superior_phpddm.php');} elseif($_SESSION['position'] == "Unit Head"){include('unithead_phpddm.php');} elseif($_SESSION['position'] == "Management"){include('management_phpddm.php');} ?>
                      </div></td>
                </tr>
                <tr>
                  <td height="20" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td height="20" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td height="20" colspan="2">
</span></span></p>

  </p>
  <div align="center">
    <p class="MENU style14 style20">Document Tracking </p>
    <p class="MENU">Summary Report </p>
  </div>
  </td>
                </tr>
              <tr>
                  <td height="20" colspan="2"></td>
                </tr>
               <tr>
                  <td width="492" height="20" bgcolor="#DAECFE"><div align="right" class="style23">Company Code : </div></td>
                   
				  <td width="511" bgcolor="#E6E6E6"><?php while($row = mysql_fetch_array($result)){ ?><input type="checkbox" name="company_code[]" value="<?php echo "".$row['company_code'].""; ?>"<?php if($company_code!=NULL){foreach ($company_code as $company_code_ticked){if($company_code_ticked == $row['company_code']){ echo "checked";}}}?>><?php echo $row['company_code']; ?><br>
				    <?php  } ?>
					<input type="checkbox" name="check_all_top"  onClick="checkAllSwitchTop()">



<br>
</td>
                </tr>
                <tr>
                  <td height="20" bgcolor="#DAECFE"class="style10"><div align="right" class="style23">Year : </div></td>
                  <td height="20" bgcolor="#E6E6E6" class="style10"><input type="text" name="year_select" maxlength="4" size="4" value="<?php if($year_select != NULL){echo $year_select;} ?>"> </td>
                </tr>
                <tr>
                  <td height="20" colspan="2"class="style10">&nbsp;</td>
                </tr>
                <tr>
                  <td height="20" colspan="2"><div align="center">
				
                    <input type="Submit" name="Submit" value="Search">
                  </div></td>
                </tr>
                <tr>
                  <td height="20" colspan="2">&nbsp;</td>
                </tr>
				
                <tr>
                  <td height="20" colspan="2"><div align="center">
                    <?php if($
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.