I am new in PHP & MySQL. I know other languages but for this project it is being done in PHP. I found some code online that I was tweaking so it would accomplish my task. I am trying to export data from MySQL database, display it on screen and give an option to download to excel. The code runs and works, however it is not placing my data from the SQL database on the lines, only the table columns.
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'hcap');
$data = mysqli_query($conn,"SELECT `Org_ID`,'Org_Name', 'Org_Address', 'Org_Address2', 'Org_City', 'Org_State', 'Org_Zip', 'Org_County',
'Org_Website', 'Org_Phone', 'Org_fax', 'Org_Email'
FROM `organization`");
if(isset($_POST["ExportType"])) {
switch($_POST["ExportType"]) {
case "export-to-excel" :
// Submission from
$filename = $_POST["ExportType"] . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
ExportFile($data);
//$_POST["ExportType"] = '';
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}
}
function ExportFile($records) {
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;
}
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<title>HCAP Organization Report</title>
<div><h3>HCAP Organization Report</h1></div>
<div>
<div id="container" >
<div class="col-sm-6 pull-left">
<div class="well well-sm col-sm-12">
<b id='project-capacity-count-lable'><?php echo count($data);?></b> records found.
<div class="btn-group pull-right">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu" id="export-menu">
<li id="export-to-excel"><a href="#">Export to excel</a></li>
<li class="divider"></li>
<li><a href="#">Other</a></li>
</ul>
</div>
</div>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
<input type="hidden" value='' id='hidden-type' name='ExportType'/>
</form>
<table id="" class="table table-striped table-bordered">
<tr>
<th>Organization ID</th>
<th>Organization Name</th>
<th>Organization Address</th>
<th>Organization Address 2</th>
<th>City </th>
<th>State </th>
<th>Zip Code </th>
<th>County </th>
<th>Website </th>
<th>Phone Number </th>
<th>Fax Number </th>
<th>E-Mail Address </th>
</tr>
<tbody>
<?php foreach($data as $row):?>
<tr>
<td><?php echo $row ['Org_ID']?></td>
<td><?php echo $row ['Org_Name']?></td>
<td><?php echo $row ['Org_Address']?></td>
<td><?php echo $row ['Org_Address2']?></td>
<td><?php echo $row ['Org_City']?></td>
<td><?php echo $row ['Org_State']?></td>
<td><?php echo $row ['Org_Zip']?></td>
<td><?php echo $row ['Org_County']?></td>
<td><?php echo $row ['Org_Website']?></td>
<td><?php echo $row ['Org_Phone']?></td>
<td><?php echo $row ['Org_fax']?></td>
<td><?php echo $row ['Org_Email']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
jQuery('#export-menu li').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
case 'export-to-excel' :
$('#hidden-type').val(target);
//alert($('#hidden-type').val());
$('#export-form').submit();
$('#hidden-type').val('');
break
}
});
});
</script>