I have a bootstrap table where I get the values from my database using select:
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Studentnamn</th>
<th>Student ID</th>
<th>Registrerade kurser</th>
<th>Betyg</th>
</tr>
</thead>
<tbody>
<?php include 'studentDB.php'; ?>
</tbody>
</table>
I want to make my table collapse and then display an inputform where the values on onclicked row can be updated. I have used following code:
$sql = "SELECT student.studName, grades.studId, grades.courseId, grades.grade FROM student
INNER JOIN grades ON student.studId=grades.studId";
try {
foreach ($pdo->query($sql) as $row) {
echo '<tr data-toggle="collapse" data-target="#collapseUpdate">';
echo '<td>' . $row['studName'] . '</td>';
echo '<td>' . $row['studId'] . '</td>';
echo '<td>' . $row['courseId'] . '</td>';
echo '<td>' . $row['grade'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate">
<div class="card card-block">
<form class="form-horizontal" action="update.php" method="post" id="newGrade">
<div class="control-group">
<label class="control-label">Nytt betyg:</label>
<div class="controls">
<p><input id="cbetyg" name="grade" type="text"></p>
<input type="hidden" name="studId" value="'. $row['studId'] .'">
<input type="hidden" name="courseId" value="'. $row['courseId'] .'">
</div>
</div>
<div class="form-actions">
<input type="submit" name="submit" value="Ă„ndra">
</div>
</form>
My problem here is now that, only the first row collapses and can use update. When I click on the other rows, the first row collapses... Can somebody tell me what I can do? And also, will my update still work for each individual row if I make the collapse work for all the rows?
My table looks like this (click here)
Thanks!
You are using the same collapseUpdate target in any field. Try modify these lines:
instead
echo '<tr data-toggle="collapse" data-target="#collapseUpdate">';
use
echo '<tr data-toggle="collapse" data-target="#collapseUpdate'.$row['studId'].'">';
and instead
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate">
use
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate'.$row['studId'].'">
I suspect that studId is unique in this table.
Related
I have a modal with select value and when I post it I want to submit that values together with other values outside of the modal.
Here is the entire code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="myForm">
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th><input name="select_all" value="1" id="example-select-all" type="checkbox" /></th>
<th>Client</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo '<input type=hidden name=fetched_devices_array[] value=' . $row['device_id'] . '>' ?>
<?php echo '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' ?>
</td>
<td>
<?php echo $row["name"]; ?>
</td>
</tr>
<div class="modal" id="acceptModal">
<div class="modal-header">
<h4 class="modal-title" id="exampleModalLabel">Select the return to manufacturer</h4>
</div>
<div class="modal-body">
<p>
<?php
$sql_return_to = $db->prepare("SELECT
users.id as user_id,
users.full_name,
users.role,
companies.company_name
FROM users
INNER JOIN companies ON users.company_id=companies.id
WHERE (users.role=? OR users.role=? OR users.role=? AND users.active=?)");
$sql_return_to->bind_param("iiii", $usr_type1, $usr_type2, $usr_type3, $active);
$sql_return_to->execute();
$result_ = $sql_return_to->get_result();
$result = [];
if ($result_->num_rows > 0) {
$result = $result_->fetch_all(MYSQLI_ASSOC);
}
$sql_return_to->close();
echo "<select class='device_manufacturer' name='device_manufacturer'>";
$nothing = "Nothing selected";
echo "<option value='" . $nothing . "'>" . $nothing . "</option>";
foreach ($result as $row) {
echo "<option value='" . $row['user_id'] . "'>" . $row['company_name'] . ' (' . explode(' ', trim($row['full_name']))[0] . ')' . "</option>";
}
echo "</select>"; ?>
</p>
<div id="wrapper">
<button type="button" id="buttonConfirm" onclick="SubmitFormData()" name="goback" class="btn">Confirm</button>
</div>
</div>
</div>
</tbody>
</table>
</form>
When I call :
function SubmitFormData() {
$("#myForm").submit(); // Submit the form
}
I am able to receive in PHP all the values outside the modal but not the one from the dropdown device_manufacturer . Why does that happen?
P.S: There are values in the dropdown in case you are wondering if it might be empty.
I am creating the form to select all option to show in the below table, I want to search when I am selected the option show in the table. My part to do is let me to onchange "Transaction Type" then show the below table. How to function can let me to select all option to show in the below table? Hope anyone can give me example or edit in my coding to guide me how to do it. I want the output can follow my select date range and transaction type to show the table.
transaction_history file to show the frontend create the form and pass the data to the backend.
<?php
$system_user_type = $user_type;
$lang = $_COOKIE["Language"];
$new_cur_date = date("d-m-Y", strtotime($cur_date));
require_once("language/lang_transaction_" . $lang . ".php");
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="box form-group">
<header>
<h5><?php echo $language["LIST_TITLE1"]; ?></h5>
<!-- .toolbar -->
<div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div><!-- /.toolbar -->
</header>
<section class="content">
<div class="col-lg-12 form-group" >
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-2">
<select id="select_type" class="form-group form-control required" onchange="show_table();">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register">Register</option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
<div class="col-lg-8"></div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="" id="table_result">
</div>
</section>
</div>
</div>
</div>
</body>
</html>
<script>
$(function() {
show_table();
$("#select_type").on("change", function() {
show_table();
});
});
function show_table() {
//alert(123);
var select_type = $("#select_type").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type
},
before_send: function() {
show_overLay();
//$('#patient_result').html('');
},
success: function(data) {
hide_overLay('');
//alert(data);
if (data) {
$("#table_result").html("");
$("#table_result").append(data);
//
$('.dataTable').dataTable();
} else {
alert("Please fill in the field.");
}
}
});
}
</script>
<style>
.myClass
{
display: none;
}
</style>
transaction_table file to show the backend function send to frontend page.Below is my coding:
<?php
$select_type = $_POST['select_type'];
if ($select_type == "withdraw") {
echo '<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
$i = 1;
$select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
echo '</tr>';
}
echo " </tbody>
</table>";
}elseif ($select_type == "transfer") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>To Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
if($rs_transfer['point_type']=="2"){
$to_type="Register Point";
}elseif($rs_transfer['point_type']=="3"){
$to_type="Entertainment Point";
}elseif($rs_transfer['point_type']=="4"){
$to_type="Business Point";
}
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $to_type . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "upgrade") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "register") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
Below is my output, this output just can follow what I select the transaction type to onchange to show in the table:
Below is my database information:
If I understand your question correctly, then you need to include these additional parameters in your request as you already to with select_type.
First, get the additional values in theshowTable function, also add them to the request data:
function show_table() {
var select_type = $("#select_type").val();
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type,
start_date: start_date,
end_date: end_date
},
// ...
}
then, receive the values in your PHP file like you already do with select_type:
<?php
$select_type = $_POST['select_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// ...
Finally, you have to adjust your queries accordingly. As you already have and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" in your queries, it's just a matter of replacing the hardcoded dates with the ones you received. Do not forget to sanitize your input dates! If possible use prepared statements.
Note 1: Your script and style tags are outside of the html part of your site. This is not valid. These tags belong into either the head or the body tag.
Note 2: There are whitespaces before your opening html tag. You may wan't to avoid this too, since it can trigger IE into quirks mode. Simply change this
?>
<html>
to this
?><html>
Here's the thing, I need to update a selected row from a table, so I'm putting a form for each row of it (Every single row has an update button) and when I click update, it doesn't submit, actually, doesn't do anything.
Here's my code, I'll be grateful with the solution.
<div class="table-responsive">
<table class="table table-condensed">
<thead class="">
<tr>
<th>ID</th>
<th>Project</th>
<th>Type</th>
<th>Kick-Off Date</th>
<th>Deadline Date</th>
<th>Current Ninja</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$q = $_GET['q'];
$sql="SELECT pr.project_name, pr.project_type, pr.project_start_date, pr.project_end_date, us.nombre, st.id_status, pr.id_project FROM NT_Projects as pr LEFT JOIN Project_Status as st on st.id_status = pr.id_status LEFT JOIN NT_Clients as cl on cl.id_client = pr.id_client LEFT JOIN usuarios as us on us.username = pr.username WHERE cl.id_client = $q";
$result = mysql_query($sql) or die(mysql_error());
$upt = 1;
while($row = mysql_fetch_array($result)) {
echo '
<div id="update-project">
<form method="post" action="sistema/_actions/updateProject.php" id="res-update-proj-'.$upt.'">';
$kickoff = date('m/d/Y', strtotime($row[2]));
$deadline = date('m/d/Y', strtotime($row[3]));
echo '<tr>';
echo '<td width="95px">
<input class="form-control" name="id_Proj" type="text" value="'.$row[6].'" readonly>
</td>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $kickoff . "</td>";
echo "<td>" . $deadline . "</td>";
echo "<td>" . $row[4] . "</td>";
echo '<td width="225px">';
echo '<select class="form-control" name="proj_Status">';
$qStatus = "SELECT * FROM Project_Status;";
$exStatus = mysql_query($qStatus);
while($rStatus = mysql_fetch_array($exStatus))
{
if($row[5] == $rStatus[0])
echo '<option value="'.$rStatus[0].'" selected>'.$rStatus[1].'</option>';
else
echo '<option value="'.$rStatus[0].'">'.$rStatus[1].'</option>';
}
echo '</select>
</td>
<td class="text-center">
<button type="submit" class="btn btn-sm btn-primary btn-UProj" value="res-update-proj-'.$upt.'">Update</button>
<div id="res-update-proj-'.$upt.'" style="width: 100%; margin:0px; padding:0px;"></div>
</td>
</tr>
</form>
</div>';
$upt = $upt + 1;
}
?>
</tbody>
</table>
</div>
That code is being called from another HTML with ajax
You cannot mix table tags with divs and forms according to the web standards. If you do, browser's HTML-parser will mix your tags in unpredictable way.
The only solution I know is to utilize CSS:
<div style="display: table">
<form style="display: table-row">
<div> style="display: table-cell"></div>
</form>
</div>
I am trying to make a table in PHP.
There is form where I am taking in Value - Market, Production Date, Sales Date and sending it in POST to Table to get it populated.
At the POST I am calling a set of data from production table from MySQL and populating in the same table.
I am able to populate the table from SQL and Form POST Action but I am not able to align them as per the required output format.
Please help in finding fix for the below Code:
<?php
if(isset($_POST['for_post']))
{
try
{
?>
<div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
<div class="row">
<div class="col-sm-12">
<?php
if(isset($_POST['for_post_market'])){ $pname = $_POST['for_post_market']; }
if(isset($_POST['for_post_prod_date'])){ $pcat = $_POST['for_post_prod_date']; }
if(isset($_POST['for_post_sale_date'])){ $pprice = $_POST['for_post_sale_date']; }
$query = 'SELECT * FROM product';
$stmt = $DB_con->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['prod_cat_name']][] = $row['prod_name'];
}
?>
<table id="invoices" border="1">
<thead>
<th>Category</th>
<th>Product</th>
<th>Production Date</th>
<th>Sales Date</th>
<th>Market</th>
</thead>
<tbody>
<?php
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
echo "<td>$invoice</td>";
echo "<td>".$pcat."</td>";
echo "<td>".$pprice."</td>";
$count++;
}
}
$a=count($pname);
for($i=0;$i<$a;$i++)
{
echo "<td>".$pname[$i]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{ ?>
</div>
</div>
</div>
<div class="card-box" style="padding-left:5px;padding:10px;padding-bottom:50px">
<div class="row">
<div class="col-sm-12">
<form class="form-inline" method="post">
<div class="form-group m-r-10">
<label for="exampleInputName2">Market : </label>
<div class="input-group">
<select class="selectpicker" multiple data-selected-text-format="count" data-style="btn-white" name="for_post_market[]">
<?php
$stmt = $DB_con->prepare('Select * from location');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['location_name'].'</option>';
}
?>
</select>
<div>
 
<div class="form-group m-r-10">
<label for="exampleInputEmail2">Production Date : </label>
<div class="input-group">
<input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose" name="for_post_prod_date">
<span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
</div>
</div>
<div class="form-group m-r-10">
<label for="exampleInputEmail2">Sales Date : </label>
<div class="input-group">
<input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker2-autoclose" name="for_post_sale_date">
<span class="input-group-addon bg-custom b-0 text-white"><i class="icon-calender"></i></span>
</div>
</div>
<button type="submit" class="btn btn-default waves-effect waves-light btn-md" id="for_post" name="for_post">
Submit
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
The Current Output that I am able to get from this code is :
What I am trying to get is :
Updated Changes Suggested and worked:
<table id="invoices" border="1">
<thead>
<th>Category</th>
<th>Product</th>
<th>Production Date</th>
<th>Sales Date</th>
<th>Market</th>
<th>Input</th>
</thead>
<tbody>
<?php
$a=count($pname);
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices)*$a . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
echo '<td rowspan='. count($pname) . '>' . $invoice . '</td>';
echo '<td rowspan='. count($pname) . '>' . $pcat . '</td>';
echo '<td rowspan='. count($pname) . '>' . $pprice . '</td>';
for($i=0;$i<$a;$i++)
{
echo '<td>'. $pname[$i] . '</td>';
echo '<td><input type="text" class="form-control"></td></tr>';
}
$count++;
}
}
echo '</tbody>';
echo '</table>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{ ?>
You have to set Category column rowspan to count($invoices)*count($pname) and set rowspan to Product, Production date and Sales date columns to count($pname)
Hi I'm very new to PHP because I just started learning this 2 days ago!
I'm trying to display my second table properly in my index.html file but I get this error: Invalid argument supplied for foreach() Take a look
Any help would be much appreciated
Class
public class Category {
public int Id{get;set;}
public string Name {get;set;}
}
public class Product{
public int Id{get;set;}
public int CategoryId{get;set;}
public String Brand {get;set;}
public String Name {get;set;}
public decimal Price{get;set;
}
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>CategoryId</th>
<th>Brand</th>
<th>Name</th>
<th>Barcode</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
include_once 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM product ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_id'] . '</td>';
echo '<td>'. $row['brand'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['barcode'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>CategoryId</th>
<th>Category Name</th>
</tr>
</thead>
include_once 'database.php';
<?php
$sql2 = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Remove the second:
include 'database.php';
or use:
include_once 'database.php';
to prevent double loading of your database file.
You might want to consider not disconnecting and connecting again
You have 6 th elements in the header and 7 td elements in your rows. Try to add a th in the header row.
This implies that the SQL query is failing (PDO returns false if the query fails). The manpage says
If you do not fetch all of the data in a result set before issuing your next call to PDO::query(), your call may fail. Call PDOStatement::closeCursor() to release the database resources associated with the PDOStatement object before issuing your next call to PDO::query().
Edit:
Doesn't look like this is the issue - try adding
echo '<pre>'.print_r($pdo->errorInfo(), true).'</pre>';
after the for each loop, this should output the PDO error details.