I need to display phonebook
table from my crud
database onto a page. dbheader.php connects to the database. crud.model.php
stores the function that fetches the data from database.
//Get all the data from the phonebook table, return as 2D array
function getPhoneBook($conn){
try {
$stmt = $conn->prepare("SELECT * FROM phonebook");
$stmt->execute();
$results = array();
while ($row = $stmt->fetch())
{
$results[] = $row;
}
return $results;
}
//If getPhoneBook() fails, exit with failure status
catch(PDOException $e){
echo "getPhoneBook failed: " . $e->getMessage();
exit();
}
}
The crud.crtl.php
dispay's the actual data to the user every time the page loads.
// include database configuration
include "dbheader.php";
// model with database and business logic code
include "crud.model.php";
// make my TPL array at the top, because it might be populated during
// the switch actions
$TPL = array();
// put code here for things that need to happen every time the page loads
$TPL["phonebook"] = getPhoneBook($conn);
// view with our user interface
include "crud.view.php";
The crud.view.php
provides the UI and contains the table to display the contents of the phonebook table. I'm having issues with data display. I used print_r($TPL)
to check for data in the array, but it comes back empty every time. What can be the cause of this?
<!DOCTYPE html>
<html>
<head>
<title>PhoneBook Database</title>
<style>
td{border: 1px solid black;}
</style>
</head>
<body>
<h1>Phone Book</h1>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone #</th>
<th>Email</th>
<th>Location</th>
<th>MC</th>
<th>Position</th>
<th>Department</th>
</tr>
<?
foreach ($TPL["phonebook"] as $phonedata) {
?>
<tr>
<td><?= $phonedata["id"] ?></td>
<td><?= $phonedata["fname"] ?></td>
<td><?= $phonedata["lname"] ?></td>
<td><?= $phonedata["phone"] ?></td>
<td><?= $phonedata["email"] ?></td>
<td><?= $phonedata["location"] ?></td>
<td><?= $phonedata["mc"] ?></td>
<td><?= $phonedata["pos"] ?></td>
<td><?= $phonedata["dept"] ?></td>
</tr>
<?
}
?>
</table>
<br><br>
<h3>TPL Array Dump</h3>
<pre><? print_r($TPL) ?></pre>
</body>
</html>