Search code examples
phphtmlmodel-view-controller

PHP-How to access an array in the controller from the view?


I'm making a basic website, and I am having issues accessing an array from the controller, in the view. I want to be able to access the $TPL["results"] array. Currently, when I load the controller (the home page) it says "Invalid argument for foreach loop. When I var_dump the array from the view, it is empty, but if i do it from the controller, it displays contents.

Controller:

<?php 
  include "app.config.php";
  include "app.model.php";
  include "app.view.php";

  $TPL["results"] = readAllEntries($conn);
?>

View:

<?php
$encoded = addslashes(json_encode($TPL["results"]));
?>
<table>
   <tr>
   <th></th>
   <th>ID <a onclick='sorted("ascending", "<?php echo $encoded; ?>", 0);'><img src="images/uparrow.png"/></a> <a onclick='sorted("descending", "<?php echo $encoded; ?>", 0);'><img src="images/downarrow.png"/></a></th>
   <th>Last Name <a onclick='sorted("ascending", "<?php echo $encoded; ?>", 2);'><img src="images/uparrow.png"/></a> <a onclick='sorted("descending", "<?php echo $encoded; ?>", 2);'><img src="images/downarrow.png"/></a></th>
   <th>First Name <a onclick='sorted("ascending", "<?php echo $encoded; ?>", 1);'><img src="images/uparrow.png"/></a> <a onclick='sorted("descending", "<?php echo $encoded; ?>", 1);'><img src="images/downarrow.png"/></a></th>
   <th>Email <a onclick='sorted("ascending", "<?php echo $encoded; ?>", 4);'><img src="images/uparrow.png"/></a> <a onclick='sorted("descending", "<?php echo $encoded; ?>", 4);'><img src="images/downarrow.png"/></a></th>
   <th>Phone <a onclick='sorted("ascending", "<?php echo $encoded; ?>", 3);'><img src="images/uparrow.png"/></a> <a onclick='sorted("descending", "<?php echo $encoded; ?>", 3);'><img src="images/downarrow.png"/></a></th>
   </tr>
 <?php
 $rowNum = 0;
    foreach($TPL["results"] as $entry) 
    {   
    ?>
       <tr id="Row_<?php echo $rowNum;?>">
            <td>DE</td>
            <td><?php echo $entry['id']?></td>
            <td><?php echo $entry['lname']?></td>
            <td><?php echo $entry['fname']?></td>
            <td><?php echo $entry['email']?></td>
            <td><?php echo $entry['phone']?></td>
            <?php  $rowNum++; ?>
         </tr>
    <?php
    }
    ?>

If I take away the "include app.view.php" from the controller and add "include app.ctrl.php" to the view, the data loads, but then I can't access actions like adding to the database through the controller. I tried using $_session, but it also gave an empty array.


Solution

  • https://www.php.net/manual/en/function.include.php

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

    so maybe try to put the view after getting entries? I would also add null coalescing operator

    <?php 
      include "app.config.php";
      include "app.model.php";
    
      $TPL["results"] = readAllEntries($conn) ?? [];
    
      include "app.view.php";
    ?>