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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘