2016-11-10 8 views
0

私はangularjsを使用してmysqlデータベースからデータをロードします。where句を使用してangularjsを使用してデータベースからデータを取得します

これはアプリケーションの動作方法です。 ユーザーのログインとそのユーザー名はCookieに格納されます。 このユーザ名は、ホーム・ページ

に表示されている私は、これは私のPHPのスクリップでこの値を取得し、where句

で、ユーザ名に基づいてテーブルからデータをフェッチするためにangularjsを通じてPHPスクリプトに渡したい

<?php 

include_once 'connect.php'; 
$data = json_decode(file_get_contents("php://input")); 


$statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc'); 
$statement->execute(array(
          'UserName' => $data->UserCookie       
         )); 

while($row = $statement->fetch()) { 
    $data[] = $row; 
} 

print json_encode($data); 
?> 

は、これはこれは私のホーム・ページ

である私の角度スクリプト

var app = angular.module('Library', ["ngCookies"]); 
app.controller('LibraryController', function($scope, $http, $interval, $cookies) { 

     //get cookie user name 
     $scope.UserCookie = $cookies.get('cookieUserName'); 

//set cookie user name 
$scope.setCookie = function(val){ 
    $cookies.put('cookieUserName', val); 
} 



//display messages  
$scope.loadMessages = function() { 
    $http.post('selectMessages.php',{'UserName': $scope.UserCookie}) 
    .success(function(data) { 
     $scope.Messages = data; 
    })  
} 

//display messages at an interval of 1 seconds 
$interval($scope.loadMessages, 1000); 

}); 

です

<!DOCTYPE html ng-app="Library" ng-controller="LibraryController"> 
<html> 
<head> 
    <title></title> 
</head> 
<body ng-init="loadMessages()"> 
     <div class="form-group"> 
     <label for="Subject">User</label> 
     <input type="text" ng-model="UserCookie" name="UserCookie" class="form-control" id="UserCookie" required="required"></input> 
     </div> 


     <input type="search" class="form-control" ng-model="searchMessage" placeholder="Search"> <br /> 
     <table class="table table-hover"> 
     <thead> 
      <tr> 
      <th>Message (Sender)</th> 
      </tr> 
     </thead> 
     <tbody> 

      <tr ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" style="font-size: 16px" ng-repeat="message in Messages | filter:searchMessage | limitTo:20"> 
      <td>{{ message.Subject }} (<span style="color:green"> {{ message.FromUser }})</span></td> 
      <td> 
      <button class="btn btn-success" ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" > view </button> 

      </td> 
     </tr> 
     </tbody> 
    </table> 

</body> 
</html> 

これは私が得るエラーですError from browser console どうしたらいいですか?助けて!

+0

AJAX要求が何であるかを、あなたのコンソールのネットワークセクションでのチェックイン、そのURLとそれが(それは本当にselectMessages.phpスクリプトに達しない)、右ですか? – krasipenkov

+0

ネットワークセクションにチェックが入っているときは、私が実際に理解していないエラーメッセージです。助けてもらえますか? – Franklin

+0

最初はエラーメッセージを表示しないことに申し訳ありません。これは私が意味していたものです。ネットワークのセクションでチェックされているときは、私が実際に理解していないエラーメッセージです。助けてもらえますか?注意:未定義のプロパティ:stdClass :: $ UserCookieのC:\ xampp \ htdocs \ LibraryApp \ selectMessages.php(16行目)致命的なエラー:C:\ xampp \ htdocs \ LibraryApp \ selectMessages.phpの配列としてstdClass型のオブジェクトを使用することはできませんon line 20 – Franklin

答えて

0

を参照してください。 ng-repeatに複製キーがあります。

0

更新された回答と、表示されているエラーに応じて、コードにいくつかの隠れたエラーや問題があります。あなたは入力を取得して、それをjsonからデコードし、$data変数に保持します(そして、は、2番目のパラメータとしてtrueを渡さない限りオブジェクトを返します)。そして、whileループでは、新しい要素を$dataに追加しようとしています(array)。しかし、そうではありません。

そして、ここではそれがどのように見えるべきかです:

<?php 

include_once 'connect.php'; 
$data = json_decode(file_get_contents("php://input")); 

$outputData = []; 

if($data && isset($data->UserCookie)) { 
    $statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc'); 
    $statement->execute(array(
           'UserName' => $data->UserCookie 
          )); 

    while($row = $statement->fetch()) { 
     $outputData[] = $row; 
    } 
} 

print json_encode($outputData); 
?> 
+0

こんにちは、私はあなたのコードを試して、それは私に結果とブラウザのネットワークセッションでエラーメッセージが表示されません。しかし、あなたが送信したPHPスクリプトのif文を削除すると、このエラーが表示されるネットワークセッションを調べます:** "通知:未定義のプロパティ:C:\ xampp \ htdocs \ LibraryApp \ selectMessagesのstdClass :: $ UserCookie .php on line 11 "**。私は、PHPスクリプトがVariable ** UserCookie **を角度から取得していないことを知っています。あなたはPHPスクリプトに角度から変数を渡す他の方法を提案できますか?ありがとう – Franklin

+0

ありがとうございました...私の問題は解決しました。私はそれを変更し、ログイン後にユーザーの詳細をセッションに保存しました。以下は私の更新されたコードです。 – Franklin

+0

'(?isset($ _ SESSION)) {session_start(); $ UserName = $ _SESSION ['UserName']; include_once 'connect。php '; $ data = json_decode(file_get_contents( "php:// input")); $ outputData = [];$ statement = $ conn->準備( 'MessageId、Subject、MessageContent、CONCAT(日付、 "、"時間)AS日付、FromUser、ToUser、メッセージタイプ、ファイル、FileNameからMessageType = "Broadcast"またはToUser =:MessageId descによるUserNameの順序 '); $ statement-> execute(array( 'UserName' => $ UserName)); ($ outputData);}?> '$ outputData [] = $ row;} print {jout_code} – Franklin

関連する問題