Hi all..

I would like to export data to excel.

I understand that I would need to create a csv file to do so. But I have no idea of how to go about creating the file. I've read up some tutorials but still don't know how to create the csv file for my php codes.

The following is my php codes:

<?php require_once('Connections/tsis.php'); ?>
<?php
mysql_select_db($database_tsis, $tsis);
$query_Recordset1 = "SELECT * FROM category";
$Recordset1 = mysql_query($query_Recordset1, $tsis) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<?php do { ?>
<fieldset>
	<legend class="heading"><?php echo $row_Recordset1['Category']; ?></legend>
	<table width="1500" border="1">
	  <tr>
          <?php 		 
		$attributes = explode(",",$row_Recordset1['Attributes']);
		foreach($attributes as $attribute){ 
			$query_Recordset2 = "SELECT * FROM attribute WHERE Attribute='".$attribute."'";
			$Recordset2 = mysql_query($query_Recordset2, $tsis) or die(mysql_error());
			$row_Recordset2 = mysql_fetch_assoc($Recordset2);
			$totalRows_Recordset2 = mysql_num_rows($Recordset2);				?>
            <th><?php echo $row_Recordset2['Name']; ?></th>
         	<?php } ?>
         </tr>
	<?php
	$query_Recordset3 = "SELECT * FROM hardware WHERE PartNo LIKE '%".$row_Recordset1['Code']."%' ORDER BY PartNo";
	if ($_SESSION['MM_USG'] != "all") 
		$query_Recordset3 = "SELECT * FROM hardware WHERE PartNo LIKE '%".$_SESSION['MM_USG']."-".$row_Recordset1['Code']."%' ORDER BY PartNo";
	$Recordset3 = mysql_query($query_Recordset3, $tsis) or die(mysql_error());
	$row_Recordset3 = mysql_fetch_assoc($Recordset3);
	$totalRows_Recordset3 = mysql_num_rows($Recordset3);		 
		do { ?>
       	 	<tr> 			
		<?php $attributes = explode(",",$row_Recordset1['Attributes']);
			foreach($attributes as $attribute) { 
				if ($attribute == "Location") {
					 if ($row_Recordset3['Location'] != ''){
						$query_Recordset5 = "SELECT * FROM hardware,location WHERE hardware.Location=location.Location AND hardware.Location='".$row_Recordset3['Location']."'";
						$Recordset5 = mysql_query($query_Recordset5, $tsis) or die(		mysql_error());
						$row_Recordset5 = mysql_fetch_assoc($Recordset5);
						$totalRows_Recordset5 = mysql_num_rows($Recordset5); ?>
						<td><?php echo "USG ".$row_Recordset5['USG']." ".$row_Recordset5['Details']; ?></td>
					<?php } else { ?>
						<td>&nbsp;</td>
					<?php }
				} else { ?>				
         				<td><?php echo $row_Recordset3[$attribute]; ?></td>
		 		<?php }
         		} ?>
       		</tr>
	   	<?php } while($row_Recordset3 = mysql_fetch_assoc($Recordset3))?>
	</table>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?></div>

The above generates hardware information of all the existing category. Each category of hardware is displayed in each table.

May I know how do I create a csv file for the above php codes? Thanks in advance

    This is the format of the .csv file:

    "val11","val12",....,"val1n"
    "val21","val22",....,"val2n"
    ...
    "valm1","valm2",....,"valmn"

    the delimiter could be comma or semicolum, after u create the content, u should write that content into a text file, with the extension .csv (ex. my_export.csv).

    For creating a file u need 3 steps:
    - open the file for writing, or creat it if doesnt exists [fopen()]
    - if the file is open for writing, then write the content u prepared above [fwrite()]
    - if u are done working with this file, u should close it [fclose()]

      Thanks for the replies..

      I've went to the site that obrienkev posted. I was wondering which is an easier method, pear or csv.

        Write a Reply...