douqiao8032 2015-05-01 11:38
浏览 48

使用AngularJS访问wpdb

I'm starting with AngularJS and I want to use it in my WordPress site.

I'm trying to print a table with the data of a table in my WordPress database but I can't access to the WordPress functions and variables.

This following is some of my code.

page-demo.php

<?php
/*
 * Template Name: Demo
 */

get_header(); 
?>

<!DOCTYPE html>
<html>
<head>

<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script language="JavaScript"
    src="<?php echo get_option('siteurl')?>/wp-content/themes/theme1200/js/app.js"></script>
<script language="JavaScript"
    src="<?php echo get_option('siteurl')?>/wp-content/themes/theme1200/js/smart-table.min.js"></script>


<meta name="description"
    content="Basic AngularJS example of data binding" />
<meta charset="utf-8">
<title>JS Bin</title>
</head>

<body >
    <div ng-app="myApp" ng-controller="mainCtrl">
        <h3>Basic Smart-Table Starter</h3>
        <table st-table="rowCollection" class="table table-striped">
            <thead>
              <tr>
                  <th>first name</th>
                  <th>last name</th>
                  <th>birth date</th>
                <th>balance</th>
                <th>email</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="row in rowCollection">
                <td>{{row.NOMBRE}}</td>

            </tr>
            </tbody>
        </table>
    </div>

  </body>
</html>

<?php 
get_footer(); 
?>

app.js

angular.module('myApp', [ 'smart-table' ]).controller('mainCtrl',
        function($scope, $http) {
            $http.get('../wp-content/themes/theme1200/model/model.php').
            success(function(data) {
                $scope.rowCollection = data;            
            })      
        });

model.php

<?php 

global $wpdb;

//$query = "SELECT * FROM JUGADOR";
//$resultado = $wpdb->get_results($query);

$sql = "SELECT APELLIDOS, NOMBRE, HANDICAP FROM JUGADOR ORDER BY APELLIDOS";
$res = $wpdb->get_results($wpdb->prepare($sql));

$result = $res->fetchAll( PDO::FETCH_ASSOC );
# JSON-encode the response
$json = json_encode( $result );
echo $json;

?>

But I'm having an error when it execute the following line.

$res = $wpdb->get_results($wpdb->prepare($sql));

I think that this file can't access to wordpress functions and variables.

Can anybody help me?

  • 写回答

2条回答 默认 最新

  • doufei7464 2015-05-01 13:10
    关注

    You are correct in thinking that your script does not have access to Wordpress functions and variables. Running a php script directly like that will mean Wordpress is not set up.

    A better approach is to hook in to Wordpress and set up urls which will return the data you want from there, for Angular to use. There are a couple of different ways you can go about this.

    There is a Wordpress REST API which can be used with the likes of Angular. It allows you to get data very easily, but the focus is on core Wordpress data not a custom databse. But it's worth a look.

    Another is to create your own endpoint, which will allow you to return whatever you want from a given url. Here is an article looking at that approach.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭