我正在构建一个简单的" AngularJS
个应用,其中有一篇文章新闻源。我的文章存储在mysql
数据库中,我使用php PDO
提取它们。我曾经使用AJAX
使用简单的LAMP
配置(php5, mysql, apache2
)执行此操作,并且一切都按预期工作。
现在我尝试使用yeoman
(bower
+ grunt
)重建项目,当我尝试使用{{1}调用我的.php脚本时} service,响应是实际的脚本内容(它返回代码而不是运行查询)。
我不知道这是我的代码,我的grunt配置还是别的问题。到目前为止,我还没有尝试制作发行版本。
以下是相关页面:
观点:
$http.get
控制器: '使用严格的';
angul
<div id="articles-sidebar">
<h2>Search articles archive:</h2>
<form id="searchbox" method="post">
<input name="searchword" type="text" placeholder="author, title, keyword...">
<input type="submit" value="Search">
</form>
<div id="author">
</div>
</div>
<div id="articles-feed">
</div>
<div id="comments-box">
<form id="comment" method="post">
<textarea name="comment-text" placeholder="Your comment..."></textarea>
<input name="submit-comment" type="submit" value="Post Comment">
</form>
<div id="comments-feed">
</div>
</div>
db_auth.php:
ar.module('ptcAngularJsApp')
.controller('ArticlesCtrl', function ($scope, $http) {
$scope.articles = [];
$http.get('/scripts/php/articles.php').
success(function(response) {
$scope.articles = response;
console.log(response);
}). //end http get success
error(function(err) {
console.log(err);
}); //end http get error
});
articles.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$hostname = "localhost";
$username = "";
$password = "";
$db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br/>";
die();
}
gruntfile.js
<?php
require('db_connexion.php');
//search form
if (!empty($_POST["searchword"])) {
$searchword = $_POST["searchword"];
$query = $db->prepare(
'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles, authors, categories, article_categories
WHERE articles.author_id = authors.id
AND articles.id = article_categories.article_id
AND article_categories.category_id = categories.id
AND ((title LIKE :searchword) OR (extract LIKE :searchword) OR (body LIKE :searchword) OR (name LIKE :searchword) OR (cat_name LIKE :searchword))'
); //end DB QUERY
$query->execute(array(":searchword" => "%" . $searchword . "%"));
$result = $query->fetchAll();
//turns timestamp into integer
for($i = 0; $i < count($result); ++$i) {
$result[$i]['posted'] = strtotime($result[$i]['posted']);
}
echo json_encode($result);
die();
}
//article title link
else if (!empty($_POST["title"])) {
$title = $_POST["title"];
$query = $db->prepare(
"SELECT articles.title, articles.posted, articles.body, authors.name, authors.img, authors.bio, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles INNER JOIN authors ON articles.author_id = authors.id
INNER JOIN article_categories ON articles.id = article_categories.article_id
INNER JOIN categories ON article_categories.category_id = categories.id
WHERE title LIKE :title; SELECT comment.user_id, comment.text, comment.posted FROM articles RIGHT JOIN comment ON articles.id = comment.article_id
WHERE title LIKE :title;"
); //end DB QUERY
$query->execute(array(":title" => $title));
$result = $query->fetchAll();
$result[0]['posted'] = strtotime($result[0]['posted']);
$query->nextRowset();
$result2 = $query->fetchAll();
for($x=0; $x < count($result2); $x++) {
$result2[$x]['posted'] = strtotime($result2[$x]['posted']);
}
echo json_encode(array('article'=>$result, 'comments'=>$result2));
die();
}
//loading article comments
else if (!empty($_POST["comment_load"])) {
$comment_load = $_POST["comment_load"];
$query = $db->prepare(
"SELECT comment.user_id, comment.text, comment.posted FROM articles RIGHT JOIN comment ON articles.id = comment.article_id
WHERE title LIKE :comment_load;"
);
$query->execute(array(":comment_load" => $comment_load));
$result = $query->fetchAll();
for($x=0; $x<count($result); $x++) {
$result[$x]['posted'] = strtotime($result[$x]['posted']);
}
echo json_encode($result);
die();
}
//saving comment
else if (!empty($_POST["comment-text"])) {
$input_text = $_POST["comment-text"];
$query = $db->prepare(
'INSERT INTO comment (user_id, text, article_id)
VALUES (101, :input_text, 4);'
);
$query->execute(array(":input_text" => $input_text));
echo json_encode($result);
die();
}
//default GET article feed
else {
$query = $db->prepare(
'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles, authors, categories, article_categories
WHERE articles.author_id = authors.id
AND articles.id = article_categories.article_id
AND article_categories.category_id = categories.id'
); //end DB QUERY
$query->execute();
$result = $query->fetchAll();
//turns timestamp into integer
for($i = 0; $i < count($result); ++$i) {
$result[$i]['posted'] = strtotime($result[$i]['posted']);
}
echo json_encode($result);
die();
}
欢迎任何有关尝试使其工作的想法。感谢
答案 0 :(得分:2)
更改
$http.get('/scripts/php/articles.php')
到
$http.get('http://YOURDOMAIN.COM/scripts/php/articles.php')
当然,您需要将YourOURDOMAIN.COM替换为localhost或您正在使用的任何其他域。
答案 1 :(得分:0)
问题是grunt没有运行我的php脚本。
当我运行grunt build
获取分发版本,然后使用apache运行该版本时,我的数据库查询结果正常。
我将尝试弄清楚如何将php集成到grunt中,这样我就可以在我的开发环境中运行它而不必每次构建它,并在我到达它之后更新这个anwser。
如果有其他人知道这样做的好教程,生成器或库,请写下来。