Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

Download tables into separate sheets

I have a bunch of tables in one page, but here is an example code of two, the tables show when I click a button called "Generate report"
<?php 
require "connection.php";
//error_reporting(E_ALL);
//ini_set('display_errors',1);
$date= date("m/d/Y");
?>

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css">

<style>
.container{
	margin-top: 50px;
}
 table, th,td {
    border: 1px solid;
}
.panel-primary{
	width:200%;
}
.table{
	width: auto !important;
}
.panel-danger{
	width: 60%;
}
.hbp{
	width: 100% !important;
}
.rsvp{
	width: 50% !important;
}
</style>
</head>
<body>
<title>Admin Reports</title>
<div class="container">
<h1 style= "padding-bottom:10px;">Daily Reports</h1>
<!-- BEGIN COLD -->
<div class="row">
				<div class="panel-primary">
					<div class="panel-heading">
						<h3 class="panel-title">Cold</h3>
					</div>
<table class="table table-hover" id="dev-table">
  <thead>
  <tr>
<?php 
$query = mysqli_query($conn,"SHOW columns FROM `call`"); 
// Loop over all result rows
while($row = mysqli_fetch_array($query))
{
	if($row["Field"] == 'id')
	{
		continue;
	}
	echo '<th>';
	echo $row["Field"];
	echo '</th>';
	}
	
  
?>
</tr>
</thead>

  <tbody>
<?php 
$query2= "SELECT * from `call` WHERE `Date` = '$date'";
$result= mysqli_query($conn, $query2);
while($row = mysqli_fetch_assoc($result))
{
		echo '<tr>';
		foreach($row as $fieldname => $values)
		{
			if($fieldname == 'id')
			{
				continue;
			}
			else
			{
				echo '<td>';
				echo $values;
				echo '</td>';
			}
		}
		echo '</tr>';
}

?>    
  </tbody>
</table>

<!-- END COLD -->

<!-- BEGIN COMM -->

				<div class="panel-info">
					<div class="panel-heading">
						<h3 class="panel-title">Com</h3>
					</div>
<table class="table table-hover" id="dev-table">
  <thead>
  <tr>
<?php 
$query = mysqli_query($conn,"SHOW columns FROM `com`"); 
// Loop over all result rows
while($row = mysqli_fetch_array($query))
{
	if($row["Field"] == 'id')
	{
		continue;
	}
	echo '<th>';
	echo $row["Field"];
	echo '</th>';
	}
	
  
?>
</tr>
</thead>

  <tbody>
<?php 
$query2= "SELECT * from `com` WHERE `Date` = '$date'";
$result= mysqli_query($conn, $query2);
while($row = mysqli_fetch_assoc($result))
{
		echo '<tr>';
		foreach($row as $fieldname => $values)
		{
			if($fieldname == 'id')
			{
				continue;
			}
			else
			{
				echo '<td>';
				echo $values;
				echo '</td>';
			}
		}
		echo '</tr>';
}

?>    
  </tbody>
</table>

<!-- END COM -->

Open in new window

I want that instead of them displaying, when I click on "generate report" an excel file downloads with each table on a separate sheets? Is this possible?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

It's possible (and fairly easy) to generate a CSV file from a database table.  In my experience, the best results for the client come from generating the CSV file, storing it on the server, and giving the client a clickable link to open the CSV.

CSV files are almost universally associated with Excel, and do not require much "special" processing in PHP.

I expect that you could use something like PHPExcel to create a workbook with separate worksheets, but I have never tried that, so it would be a research project.  If you can live with the CSV concept, instead of needing the Excel workbook, I can show you how to get it running immediately.
Avatar of Jazzy 1012
Jazzy 1012

ASKER

I'll be needing excel with different sheets, should I research for PHPExcel?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial