从数组中提取变量并显示到视图中

时间:2019-03-28 12:30:58

标签: php templates

我写了这段代码来加载一些视图。我想提取从数据库查询传递到视图的数组数据,然后显示它。我已经尝试过使用ob_start()extract()函数,但似乎不起作用。我认为问题出在我的应用程序的控制器代码上,但我不知道如何解决它,任何帮助或建议将不胜感激。

模型类:

<?php

define( 'STORAGE_PATH', __DIR__ );

class CredentialsManager{
  private $data;
  private $input;
  private $open;
  private $decode;
  private $encode;

  public function __construct(){
    $this->db = new PDO('sqlite:'.STORAGE_PATH.'/clients.db');
  }

  public function save(array $data){
    if( isset( $data ) && is_array( $data ) ){
      $data = [
        $_POST['client_name'],
        $_POST['hosting_provider'],
        $_POST['client_domain'],
        $_POST['username'],
        $_POST['password'],
        $_POST['ftp_host'],
        $_POST['mysql_username'],
        $_POST['mysql_password'],
        $_POST['mysql_host'],
        $_POST['wordpress_user'],
        $_POST['wordpress_password']
      ];

      $stmt = $this->db->prepare( "INSERT INTO clients_details (client_name,hosting_provider,client_domain,username,password,ftp_host,mysql_username,mysql_password,mysql_host,wordpress_user,wordpress_password) VALUES(?,?,?,?,?,?,?,?,?,?,?)" );
      return $stmt->execute( $data );

    }
  }

  public function search(string $data){
    if( isset( $data ) && is_string( $data ) ){
      $stmt = $this->db->prepare("SELECT * FROM clients_details WHERE client_name = ?");
      $stmt->execute( array($data) );
      $result = $stmt->fetchAll( PDO::FETCH_ASSOC );
      if( $result > 0 ){
      return json_encode($result);
      }
    }
  }

}

?>

View.php类:

<?php

class View{
  private $data;

  public function __construct(){

  }

  public function search(){
    require_once 'search.php';
  }

  public function searchResults(array $results_set){
    ob_start();
    extract( $results_set );
    require_once 'search-results.php';
    return ob_get_clean();
  }

}
?>

search-results.php模板文件:

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="assets/bootstrap.min.css" type="text/css">

<script type="text/javascript" src="assets/bootstrap.bundle.min.js"></script>
</head>

<div class="container">
  <div class="row-justify-content-center">
    <div class="col-sm-12 col-md-8 col-lg-8">
      <div class="row">
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $client_name; ?>" placeholder="Cliente">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $client_domain; ?>" placeholder="Dominio">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $hosting_provider; ?>" placeholder="Hosting provider">
        </div>
        <br>
        <div class="col-sm-12 col-md-6 col-lg-6">
          <input type="text" class="form-control" value="<?php echo $username; ?>" placeholder="Username">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-6">
          <input type="text" class="form-control" value="<?php echo $password; ?>" placeholder="Password">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-8">
          <input type="text" class="form-control" value="<?php echo $ftp_host; ?>" placeholder="FTP Host">
        </div>
        <br>
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $mysql_host; ?>" placeholder="MySql host">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $mysql_username; ?>" placeholder="MySql username">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-4">
          <input type="text" class="form-control" value="<?php echo $mysql_password; ?>" placeholder="MySql password">
        </div>
        <br>
        <div class="col-sm-12 col-md-6 col-lg-6">
          <input type="text" class="form-control" value="<?php echo $wordpress_user; ?>" placeholder="Wordpress username">
        </div>
        <div class="col-sm-12 col-md-6 col-lg-6">
          <input type="text" class="form-control" value="<?php echo $wordpress_password; ?>" placeholder="Wordpress password">
        </div>
      </div>  
    </div>
  </div>
</div>

这是其余代码,这些代码管理到控制器的路由并从db输出数据:

<?php
/**
 * Controller Class
 */
class Controller{
  private $view;
  private $data;

  public function __construct(){
    $this->view = new View;
    $this->model = new CredentialsManager;
  }

  public function index(){
    return $this->view->index();
  }

  public function search(){
    return $this->view->search();
  }

  public function processData(string $request_method, $data){
    if( $request_method === 'POST' ){
      return $this->model->save($_POST);
    }
    elseif( $request_method === 'GET' ){
      $search_results = $this->model->search($data);
      $results_set = json_decode($search_results, true);
      return $this->view->searchResults($results_set);
    }
  }

}

$controller = new Controller;

if( isset($_SERVER['REQUEST_URI']) ){
  $requested_uri = parse_url( $_SERVER['PATH_INFO'], PHP_URL_PATH);
    if( $requested_uri === '' ){
      echo $controller->index();
    }
    elseif( $requested_uri != '' ){
      $method = ltrim( $requested_uri, '/');
      echo @$controller->$method();
    }
}

if( isset($_SERVER['REQUEST_METHOD']) ){
  switch( $_SERVER['REQUEST_METHOD'] ){
    case 'POST':
      echo $controller->processData('POST', $_POST);
      break;
    case 'GET':
      $q = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
      $search_term = ltrim( $q, 'q=');
      echo $controller->processData('GET', $search_term);
      break;
  }
}

?>

PS: I need also to improve the route system, for now is a very dirt solution,it works well, but it will try to manage also the request for the templates page resources and this is not what I want.

1 个答案:

答案 0 :(得分:0)

编辑View.php:

<?php

    class View{
      private $data;

      public function __construct(){

      }

      public function search(){
        require_once 'search.php';
      }

      public function searchResults(array $results_set){
        ob_start();
        require_once 'search-results.php';
        return ob_get_clean();
      }

    }
?>

编辑search-results.php:

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="assets/bootstrap.min.css" type="text/css">

<script type="text/javascript" src="assets/bootstrap.bundle.min.js"></script>
</head>

<div class="container">
  <div class="row-justify-content-center">
    <div class="col-sm-12 col-md-8 col-lg-8">
        <?php foreach($results_set as $row) { ?>
          <div class="row">
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['client_name']; ?>" placeholder="Cliente">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['client_domain']; ?>" placeholder="Dominio">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['hosting_provider']; ?>" placeholder="Hosting provider">
            </div>
            <br>
            <div class="col-sm-12 col-md-6 col-lg-6">
              <input type="text" class="form-control" value="<?php echo $row['username']; ?>" placeholder="Username">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-6">
              <input type="text" class="form-control" value="<?php echo $row['password']; ?>" placeholder="Password">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-8">
              <input type="text" class="form-control" value="<?php echo $row['ftp_host']; ?>" placeholder="FTP Host">
            </div>
            <br>
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['mysql_host']; ?>" placeholder="MySql host">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['mysql_username']; ?>" placeholder="MySql username">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-4">
              <input type="text" class="form-control" value="<?php echo $row['mysql_password']; ?>" placeholder="MySql password">
            </div>
            <br>
            <div class="col-sm-12 col-md-6 col-lg-6">
              <input type="text" class="form-control" value="<?php echo $row['wordpress_user']; ?>" placeholder="Wordpress username">
            </div>
            <div class="col-sm-12 col-md-6 col-lg-6">
              <input type="text" class="form-control" value="<?php echo $row['wordpress_password']; ?>" placeholder="Wordpress password">
            </div>
          </div>
        <?php } ?>          
    </div>
  </div>
</div>