Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-PDO:如何将一些返回值包装到javascript中,以便在视图上呈现?_Javascript_Php_Mysql_Ajax_Pdo - Fatal编程技术网

PHP-PDO:如何将一些返回值包装到javascript中,以便在视图上呈现?

PHP-PDO:如何将一些返回值包装到javascript中,以便在视图上呈现?,javascript,php,mysql,ajax,pdo,Javascript,Php,Mysql,Ajax,Pdo,我是MVC新手,我正在尝试从mysql数据库中检索一组图像路径,并用它通过javascript构建一个网格 控制器需要一个调用数据库并返回所有路径的模型 以前,我有一个ajax调用打开了一个设计糟糕的文件,该文件echo json_encode() 目前,所有文件都通过index.php路由。那么,我是否会在控制器中echo json_code(),并打开ajaxindex.php model/interest.php class Interest { private $cxn = Dd::

我是MVC新手,我正在尝试从mysql数据库中检索一组图像路径,并用它通过javascript构建一个网格

控制器需要一个调用数据库并返回所有路径的模型

以前,我有一个ajax调用打开了一个设计糟糕的文件,该文件
echo json_encode()

目前,所有文件都通过
index.php
路由。那么,我是否会在控制器中
echo json_code()
,并打开ajax
index.php

model/interest.php

class Interest
{
  private $cxn = Dd::getInstance();
  private $table = 'interestPix'

  function __construct(){}
  public function get_interest_pix()
  {
    $sql = "SELECT * FROM $this->table";

    if ($stmt = $this->cxn->prepare($sql)) {
      $stmt->execute();
      $result = $stmt->fetchAll();

      $stmt = null;
      $this->$cxn = null;

      return $result;
    }
  }
controller/interest\u controller.php

require_once 'models/interest.php';
/**
 *
 */
class InterestController
{

  function __construct(argument){}
  public function render_interest_pix()
  {
      $model = new Interest();
  }
}
ajax文件:

function interest_grid() {
  if (window.XMLHttpRequest) {
           // code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp = new XMLHttpRequest();
       } else {
           // code for IE6, IE5
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange = function() {
           if (this.readyState == 4 && this.status == 200) {
             var jsonObj = xmlhttp.response;
             myFunc(jsonObj);
           }
       };
       xmlhttp.open("GET","views/interest_controller.php",true);
       xmlhttp.responseType = "json";
       xmlhttp.send();
}

本质上,是的,您将让控制器回显结果的json_编码。确保所有这些都得到了响应,否则在解析结果时可能会出现javascript错误