0

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>
tereško
  • 58,060
  • 25
  • 98
  • 150
Yury Stanev
  • 37
  • 1
  • 10
  • try `print_r($TPL["phonebook"]);` – Rotimi Feb 16 '18 at 19:45
  • `print_r($TPL)` would return any data in the array, specifying phonebook would be useless if the data isn't in there already. – Stark Feb 16 '18 at 19:47
  • @Akintunde007 Tried it just now, it doesn't make any difference. – Yury Stanev Feb 16 '18 at 19:48
  • You should learn how to set up PDO correctly: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developer .. and that includes using exceptions for error reporting. Also, `PdoStatement` implements ` Traversable`. – tereško Feb 16 '18 at 19:52
  • Could you post your `crud.view.php` please? Is it the last file of your question ? – Syscall Feb 16 '18 at 19:53
  • @Syscall he's referring the that php template as "view". This is why newbies should stay away from MVC, till they learn some basics. – tereško Feb 16 '18 at 19:54
  • `print_r()` display an empty array or nothing ? try `var_dump()`, that could display more information (null, false, ...). – Syscall Feb 16 '18 at 19:56
  • @Syscall Yes it's the last file in the question. – Yury Stanev Feb 16 '18 at 19:57
  • He is getting "white page of death". He does not know, what it is, but he want to use "mvc" ... it's quite sad. – tereško Feb 16 '18 at 19:58
  • @Syscall `var_dump()` doesn't display anything as well. – Yury Stanev Feb 16 '18 at 20:00
  • _"white page of death"_... So, read your Apache logs. – Syscall Feb 16 '18 at 20:03
  • Btw, your code has nothing MVC related. – Stark Feb 16 '18 at 20:10
  • 1
    @Craig lately people seem to think, that it stands for "My Very Code" – tereško Feb 16 '18 at 20:14
  • After checking the logs: php_error.log has only notice about the unidentified variable, all my logs contain only warning no errors. [link]https://pastebin.com/P4f0WSrL [link]https://pastebin.com/4LnXQkNR [link]https://pastebin.com/cREiwmV7 [link]https://pastebin.com/hYCNFJrx [link]https://pastebin.com/W3xWSfNq – Yury Stanev Feb 16 '18 at 20:33

0 Answers0