模型视图控制器 - PHP MVC 在数据库显示下拉列表中获取数据


model view controller - php mvc fetch data in Database display dropdown

如何将模型添加到控制器中以在下拉列表中显示值?我得到的错误:为 foreach() 提供的参数无效我没有使用任何框架。只是原生的 php。我是MVC和PHP的新手,请帮忙!

我的控制器:

<?php 
  class Index extends Controller {
    function __construct(){
      parent::__construct();
    }
    function index(){
      $landArray = $this->model->fetchData();
      $this->view->render('index/index');
      var_dump($landArray); // display all row in database
    }
} 

型:

<?php
class Index_Model extends Model {
 function __construct(){
    parent::__construct();
  }
 function fetchData(){
    $selectIsland = $this->connection->prepare("SELECT island_id, island 
                               from island" );
    $selectIsland->setFetchMode(PDO::FETCH_ASSOC);
    $selectIsland->execute();
    $islandResult = $selectIsland->fetchAll();
    return $islandResult;
 }
}

视图:

<select>
    <option value="">--- Select Island---</option>
      <?php
            foreach($islandResult as $row){
              echo '<option value="'.$row['island_id'].'">'.$row['island'].'</option>';
            endforeach
       }?>
</select>

这是我的库视图和呈现方法。

<?php
class View {
 function __construct(){
  }
public function render($name, $noInclude = false, $landArray){
 if($noInclude == true){
   require 'views/'.$name.'.php';
  }else{
  require 'views/header.php';
  require 'views/'.$name.'.php';
  require 'views/foother.php';
}
}
}
}

您从未调用过模型fetchData()方法。在MVC控制器控制一切。在控制器中,必须从模型调用方法。然后将这些内容传递到您的视图文件。

<?php 
  class Index extends Controller {
    function __construct(){
      parent::__construct();
    }
    function index(){
      $landArray = $this->model->fetchData();
      $this->view->render('index/index', false, array('islandResult' => $landArray)); // as we are sending param to view and render accept that as 3rd param so we need to specify the 2nd param too!
    }
} 
Model:
<?php
class Index_Model extends Model {
 function __construct(){
    parent::__construct();
  }
 function fetchData(){
    $selectIsland = $this->connection->prepare("SELECT island_id, island 
                               from island" );
    $selectIsland->execute();
    $islandResult = $selectIsland->fetchAll();
    return $islandResult;  //if you need something to pass to view from db first you have to pass it to controller from model
 }
}
View:
<select>
    <option value="">--- Select Island---</option>
      <?php
            foreach($islandResult as $row){
              echo '<option value="'.$row['island_id'].'">'.$row['island'].'</option>';
            }
       ?>
</select>
View/Render Library Method:
    class View {
         function __construct(){
         }
         public function render($name, $noInclude = false, $arrayParam = array()){ //look closely...here i've made 3rd parameter as default argument so that your other codes which don't need to send param to view works smoothly.
             if(count($arrayParam) > 0){
                 extract($arrayParam);
             }
             if($noInclude == true){
                 require 'views/'.$name.'.php';
             }else{
                 require 'views/header.php';
                 require 'views/'.$name.'.php';
                 require 'views/foother.php';
             }
         }
     }

现在,您将能够在从控制器发送的视图中使用参数。 您在控制器中作为数组键传递的任何内容现在都可以用作视图中的变量。

注意:检查我从代码中的注释。