2016-10-19 7 views
1

IDパラメータをPHPに渡して、スクリプト内の特定のSQLデータベース行を取得できるようにします。

IDパラメータはURLで使用できます(例:http://localhost/RoutingAngularJS/index.php#/students/1)。最後の数字1はデータベース内の行idで、その行の人の情報を取得できます。

$ routeParamsが

var app = angular.module("Demo", ["ngRoute"]) 
      .config(function($routeProvider){ 
      $routeProvider 
      . 
      .when("/students/:id", { 
       templateUrl:"Templates/studentDetails.html", 
       controller:"studentDetailsController" 
      }) 
     }) 
      .controller("studentDetailsController", function ($scope, $http, $routeParams) { 
       $http({ 
        url:"api/ReadOneStudent.php", 
         params:{id:$routeParams.id}, 
        method: "get" 

       }).then(function(response){ 
        $scope.student = response.records; 
       })    
      }); 

マイReadOneStudent.phpとしてのコントローラで使用されて、私は今????であり、$スチューデント> IDにIDを渡すのが好き

<?php 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
// include database and object files 
    include_once 'database.php'; 
    include_once 'Students.php'; 

    // instantiate database and product object 
    $database = new Database(); 
    $db = $database->getConnection(); 

    // initialize object 
    $student = new Students($db); 

    // get id of product to be edited 
    //$data = json_decode(file_get_contents("php://input"));  

    // set ID property of product to be edited 
    $student->id = ????; 

    // read the details of product to be edited 
    $student->readOneStudent(); 

    // create array 
    $student_arr[] = array(
     "id" => $student->id, 
     "name" => $student->name, 
     "gender" => $student->gender, 
     "city" => $product->city 
    ); 
    echo '{"records":[' . $student_arr . ']}'; 
    // make it json format 
    //print_r(json_encode($student_arr)); 

?> 

です。

おかげ

EDIT:

var app = angular.module("Demo", ["ngRoute"]) 
       .config(function($routeProvider){ 
       $routeProvider 
       . 
       .when("/students/:id", { 
        templateUrl:"Templates/studentDetails.html", 
        controller:"studentDetailsController" 
       }) 
      }) 
       .controller("studentDetailsController", function ($scope, $http, $routeParams) { 
        $http({ 
         url:"api/ReadOneStudent.php", 
          params:$routeParams, 
         method: "get" 

        }).then(function(response){ 
         $scope.student = response.data; 
        })    
       }); 

ReadOneStudent.php

<?php 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
// include database and object files 
    include_once 'database.php'; 
    include_once 'Students.php'; 

    // instantiate database and product object 
    $database = new Database(); 
    $db = $database->getConnection(); 

    // initialize object 
    $student = new Students($db); 

    // get id of product to be edited 
    //$data = json_decode(file_get_contents("php://input"));  

    // set ID property of product to be edited 
    if (!isset($_GET['id'])) { 
     http_response_code(400); // bad request 
     exit; 
    } 

    $student->id = $_GET['id']; 

    // read the details of product to be edited 
    $found = $student->readOneStudent(); // assuming this returns something useful 
    if (!$found) { 
     http_response_code(404); 
     exit; 
    } 

    // create array 
    // Seeing as this is called "read ONE student", why return an array? 
    header('Content-type: application/json'); 
    echo json_encode([ 
     'id'  => $student->id, 
     'name' => $student->name, 
     'gender' => $student->gender, 
     'city' => $student->city 
    ]); // maybe you can even just use json_encode($student) 
    exit; 
    // make it json format 
    //print_r(json_encode($student_arr)); 

?> 

Students.php

<?php 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
    class Students{ 
     // database connection and table name 
     private $conn; 
     private $table_name = "tblStudents"; 

     // object properties 
     public $id; 
     public $name; 
     public $gender; 
     public $city; 

     // constructor with $db as database connection 
     public function __construct($db){ 
      $this->conn = $db; 
     }    
     public function readOneStudent(){ 

      // query to read single record 
      $query = "SELECT 
         id, name, gender, city 
         FROM 
         " . $this->table_name . " 
         WHERE 
         id = ? 
         LIMIT 
         0,1"; 

      // prepare query statement 
      $stmt = $this->conn->prepare($query); 

      // bind id of product to be updated 
      $stmt->bindParam(1, $this->id); 

      // execute query 
      $stmt->execute(); 

      // get retrieved row 
      $row = $stmt->fetch(PDO::FETCH_ASSOC); 

      // set values to object properties 
      $this->name = $row['name']; 
      $this->gender = $row['gender']; 
      $this->city = $row['city']; 
     } 
    } 

?> 
+0

に単に1)** **独自のJSONをロール決して、また '$ _GET [ 'ID']' – Phil

+0

を使用します。 'json_encode'を使います。 2)あなたは 'then()'コールバックで 'response.data.records'を使いたいでしょう – Phil

+0

' $ routeProvider'と 'when'の間に2つのドット' ..'があります – Phil

答えて

1

ここではいくつか奇妙なことがあります。まず、あなたのPHP

Students.phpは -

if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    return false; 
} 

// set values to object properties 
$this->name = $row['name']; 
$this->gender = $row['gender']; 
$this->city = $row['city']; 

$stmt->closeCursor(); 
return true; 

ReadOneStudent.php便利readOneStudent()戻り何かを作る - 便利状況とJSONで応答

if (!isset($_GET['id'])) { 
    http_response_code(400); // bad request 
    exit; 
} 

$student->id = $_GET['id']; 
if (!$student->readOneStudent()) { 
    http_response_code(404); 
    exit; 
} 

// Seeing as this is called "read ONE student", why return an array? 
header('Content-type: application/json'); 
echo json_encode([ 
    'id'  => $student->id, 
    'name' => $student->name, 
    'gender' => $student->gender, 
    'city' => $student->city 
]); // maybe you can even just use json_encode($student) 
exit; 

次に、私は思いますルート設定でresolveプロパティを使用してください。

$routeProvider.when("/students/:id", { 
    templateUrl: "Templates/studentDetails.html", 
    controller:"studentDetailsController", 
    resolve: { 
     student: function($routeParams, $http) { 
      return $http.get('api/ReadOneStudent.php', { 
       params: {id: $routeParams.id} 
      }).then(function(response) { 
       return response.data 
      }); 
     } 
    } 
}) 

、最終的には自分のコントローラ

.controller('studentDetailsController', function($scope, student) { 
    $scope.student = student; 
}) 
+0

私はEDITの変更を元の投稿。私があなたが示唆したように解決策を使用し、コントローラーの設定をした場合、私は400エラーが発生します。つまり、IDは適切なIDではありません。私のEDITに示されているようにすると、私は404エラーがあり、IDを持つ行を見つけることができません。何が間違っていますか? – batuman

+0

@batumanあなたが 'readOneStudent()'メソッドが何も返さないので、*これは何か有用なものを返すと仮定してコメントを見落としたと思います* – Phil

+0

@batuman私はいくつかの改良を加えて答えを更新しました。 'params:$ routeParams'を試してみるのは間違っているかもしれません。私の新しい回答 – Phil

1

GETするのwiへのアクセスを得るために$_GET['id']を使用するようにしてくださいid in PHP

関連する問題