Location: PHPKode > projects > CoreAsset > coreasset/exporttoxls.php
<?php
require_once("lib/inc.php");
require_once("lib/security.php");
$status   = $_SESSION['status'];
$rrr=$database->Execute("select category_id from asset_category where category_name like'book%';");
$aa= mysql_fetch_array($rrr);
$cat_id_book= $aa[0];
if($status < 4)
{
 if(isset($_GET['cat_id']))
 {
	$cat_id			=$_REQUEST['cat_id'];
	if($cat_id == $cat_id_book) // For Bok
	{
	$select			= "SELECT * FROM asset_assets where category_id=$cat_id_book";
	$export 		= $database->Execute($select);

		$header="\tAsset Tag\tPO Nummber\tPrice\tBook\tDescription";
		
		$data="";
		while($row = mysql_fetch_array($export))
		 {
			$data.="\n\t";	 
			$data.=$row['asset_tag']."\t".$row['po_no']."\t".$row['asset_price']."\t";
			// Book name populated
			$book_ids=explode(',',$row['equipment_id']);
			//print_r($book_ids);
			for($i=0;$i<count($book_ids);$i++)
			  {
				$name	= $database->get_from_database("SELECT book_name,isbn_no,issued_user from asset_books where book_id='$book_ids[$i]';");
				//echo $name;
				$data.=$i+1;
				$data.=" .";
				$data.="Book Name : ";
				$data.=$name['book_name']."  ";
				$data.="ISBN Number= ";
				$data.=$name['isbn_no']." ";
				// User name add
				if($name['issued_user'] == 0)
				{
					$data.="Assigned To : None";
				}
				else
				{
					$usr_data=$database->get_from_database("SELECT full_name from asset_users where user_id ='$name[issued_user]'");
					$data.="Assigned To : ".$usr_data['full_name'];
				}
				$data.=", ";
			  }
			  //echo $data;
			$data.="\t";
			// Book name done
			$data.=$row['asset_description']."\t";
			//$data.=$row['receive_date'];
			
		 }
		
		// Creating EXCEL file
		$fname	= "file/export_data.xls";
		$handle = fopen ($fname, 'w+');
		fwrite($handle,"\n");
		fwrite($handle, $header);
		fwrite($handle,"\n");
		fwrite($handle, $data);
		//fwrite($handle, $data1);
		fclose($handle);
		
		// Autometic donload section
		header("Content-type: application/vnd.ms-excel");
		header("Content-Disposition: attachment; filename=your_export_data.xls");
		readfile("$fname");
}
else
{
	$select			= "SELECT * FROM asset_assets where category_id=$cat_id";
	$export 		= $database->Execute($select);
	$count 			= mysql_num_fields($export);
		for ($i = 0; $i < $count; $i++)
			{
				if($i!=1)
				{
					$head 		= mysql_field_name($export, $i);
					$head 		= str_replace('_',' ',$head);
					$head 		= ucwords($head);
					$header.="\t".$head;
				}
			}
		$header				=str_replace('equipment_id','Equipment',$header);
		$header				=str_replace('Id','Name',$header);
		$header				=str_replace('id','Name',$header);
		$data="";
		while($row = mysql_fetch_array($export)) 
		{
		
		$serialno = array();
		$maf = array();
		$expr_dt = array();
		$issued_nm = array();

		$data.="\n";
		// Equipment related string manipulation
		$asst_tag = $row['asset_tag'];
		$rrrw = $database->Execute("select * from asset_serial_number where asset_id='$asst_tag'");
		while($rwrw = mysql_fetch_array($rrrw))
		{
			$serialno[]= $rwrw['serial_no'];
			$maf[]= $rwrw['manufacturer'];
			$expr_dt[]= $rwrw['expiry_date'];
			$issuduserid = $rwrw['issud_userid'];
			if($issuduserid > 0)
			{
				$iss_nm = $database->get_from_database("select * from asset_users where user_id='$issuduserid'");
				$issued_nm[] = $iss_nm['full_name'];
			}
			else
			{
				$issued_nm[] = "None";
			}
		}
		
		
		$equipment_id       	    = $row[6];

		$list_eq					= explode(",",$equipment_id);
		$equip						="";
		 $n=1;
		 $z = 0;
		foreach($list_eq as $n=>$v)
				{
				
					if($v!="")
					{    
						$qry					= "select * from asset_equipment where equipment_id='$v' ";
						$qry2                   = "select * from asset_serial_number where  equipment_id='$v' and asset_id='$row[0]'"; 
						
						$rrw					= $database->get_from_database($qry);
						$rrw2					= $database->get_from_database($qry2);
						$eq_details				= $rrw['equipment_name']." (".$rrw['equipment_model'].")"."  [ Manufacturer : ";
		
						$eq_details.= $maf[$z].", Serial : ";
						$eq_details.= $serialno[$z].", Warranty Date : ";
						$eq_details.= $expr_dt[$z]." ], [ Issued To : ".$issued_nm[$z]." ]";
						$equip.="   ";
						$equip.=$n+1.  ;
						$equip.=" .";
						$equip.=$eq_details.",";
					
					}
					$z++;
				
				}
		$n++;
		// Vendor name populated
		
		$qry			= "select * from asset_vendor where  vendor_id='$row[4]'";
		$rrw			= $database->get_from_database($qry);
		$vendor_name	= $rrw['vendor_name'];
		
		
		// User name populated
		/*
		$qrz			= "select * from asset_users where  user_id='$row[10]'";
		$rrz			= $database->get_from_database($qrz);
		if($rrz['full_name']=="")
		{
		$user_name      ="None";
		}
		else
		{
		$user_name		= $rrz['full_name'];
		}
		*/
		// The data string is populated
		
		  for($i=0; $i<=$count; $i++)
		   {
			if($i!=1)
			{
				if($i==6)
					$data.="\t".$equip;
				else if ($i==4)
						 $data.="\t".$vendor_name;
					// else if($i==10)
					//		 $data.="\t".$user_name;
						  else
							 $data.="\t".$row[$i];
			}
		   }
		}
		
		// Creating EXCEL file
		$fname	= "file/export_data.xls";
		$handle = fopen ($fname, 'w+');
		fwrite($handle,"\n");
		fwrite($handle, $header);
		fwrite($handle,"\n");
		fwrite($handle, $data);
		//fwrite($handle, $data1);
		fclose($handle);
		
		// Autometic donload section
		header("Content-type: application/vnd.ms-excel");
		header("Content-Disposition: attachment; filename=your_export_data.xls");
		readfile("$fname");
		}
	}
}
else
{
	redirect ("export.php","");
}
//$smarty->assign('name',$name);
$smarty->assign('status',$status);
?>
Return current item: CoreAsset