2017-12-19 6 views
0

ファイルに送信:POSTパラメータを取得し、第二のPHP私は次のPHPファイルを操作する必要が

leer_directorio_todos_filtrados.php

<?php 
// required headers 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

// include database and object files 
include_once 'database.php'; 
include_once 'directorio_todo_filtrado.php'; 

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

// initialize object 
$product = new Directorio($db); 

// query products 
$stmt = $product->read(); 
$num = $stmt->rowCount(); 

//here the POST request parameter 
    $received = $_POST['valor']: 


// check if more than 0 record found 
if($num>0){ 

    // products array 
    $products_arr=array(); 
    // $products_arr["records"]=array(); 

    // retrieve our table contents 
    // fetch() is faster than fetchAll() 
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
     // extract row 
     // this will make $row['name'] to 
     // just $name only 
     extract($row); 

     $product_item=array(
      "id" => $id_directorio, 
      "nombre" => $nombre, 
      "apellidos" => $apellidos, 
      "apodo" => $apodo, 
      "cumple" => $cumple, 
      "conyuge" => $conyuge, 
      "cumple_conyuge" => $cumple_conyuge, 
      "aniversario_bodas" => $aniversario_bodas, 
      "empresa" => $empresa, 
      "direccion_empresa" => $direccion_empresa, 
      "tel_negeocio" => $tel_negocio, 
      "fecha_ingreso" => $fecha_ingreso, 
      "num_rotario" => $num_rotario, 
      "padrino" => $padrino, 
      "direccion_casa" => $direccion_casa, 
      "tel_casa" => $tel_casa, 
      "celular" => $celular, 
      "email" => $email, 
      "email_privado" => $email_privado, 
      "clasificacion" => $clasificacion, 
      "imagen" => $imagen 
     ); 

     array_push($products_arr, $product_item); 
    } 

    echo json_encode($products_arr); 
} 

else{ 
    echo json_encode(
     array("message" => "No items found.") 
    ); 
} 
?> 

とdirectorio_todo_filtrado.php:

<?php 
class Directorio{ 

    // database connection and table name 
    private $conn; 
    private $table_name = "tb_directorio"; 

    // object properties 
    public $id_directorio; 
    public $nombre; 
    public $apellidos; 
    public $apodo; 
    public $cumple; 
    public $conyuge; 
    public $cumple_conyuge; 
    public $aniversario_bodas; 
    public $empresa; 
    public $direccion_empresa; 
    public $tel_negocio; 
    public $fecha_ingreso; 
    public $num_rotario; 
    public $padrino; 
    public $direccion_casa; 
    public $tel_casa; 
    public $celular; 
    public $email; 
    public $email_privado; 
    public $clasificacion; 
    public $imagen; 
    public $usuario_app; 


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

    function read(){ 

      // select all query 
      $query = "SELECT 
         * 
        FROM 
         " . $this->table_name . " 
        ORDER BY apellidos "; 

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

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

      return $stmt; 
     } 
} 

I最初のファイルにPOSTリクエストを送信したいのですが、次にSELECTクエリをフィルタリングするために、POSTパラメータを変数として2番目のファイルに渡すことがわかりません。

答えて

1

DirectorioオブジェクトのreadメソッドにPOSTパラメータを渡すことができます。

leer_directorio_todos_filtrados.php

<?php 
// required headers 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

// include database and object files 
include_once 'database.php'; 
include_once 'directorio_todo_filtrado.php'; 

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

// initialize object 
$product = new Directorio($db); 

//here the POST request parameter 
$received = $_POST['valor']: 

// query products 
$stmt = $product->read($received); 
$num = $stmt->rowCount(); 

// check if more than 0 record found 
if($num>0){ 

    // products array 
    $products_arr=array(); 
    // $products_arr["records"]=array(); 

    // retrieve our table contents 
    // fetch() is faster than fetchAll() 
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
     // extract row 
     // this will make $row['name'] to 
     // just $name only 
     extract($row); 

     $product_item=array(
      "id" => $id_directorio, 
      "nombre" => $nombre, 
      "apellidos" => $apellidos, 
      "apodo" => $apodo, 
      "cumple" => $cumple, 
      "conyuge" => $conyuge, 
      "cumple_conyuge" => $cumple_conyuge, 
      "aniversario_bodas" => $aniversario_bodas, 
      "empresa" => $empresa, 
      "direccion_empresa" => $direccion_empresa, 
      "tel_negeocio" => $tel_negocio, 
      "fecha_ingreso" => $fecha_ingreso, 
      "num_rotario" => $num_rotario, 
      "padrino" => $padrino, 
      "direccion_casa" => $direccion_casa, 
      "tel_casa" => $tel_casa, 
      "celular" => $celular, 
      "email" => $email, 
      "email_privado" => $email_privado, 
      "clasificacion" => $clasificacion, 
      "imagen" => $imagen 
     ); 

     array_push($products_arr, $product_item); 
    } 

    echo json_encode($products_arr); 
} 

else{ 
    echo json_encode(
     array("message" => "No items found.") 
    ); 
} 
?> 

とdirectorio_todo_filtrado.php:

<?php 
class Directorio{ 

// database connection and table name 
private $conn; 
private $table_name = "tb_directorio"; 

// object properties 
public $id_directorio; 
public $nombre; 
public $apellidos; 
public $apodo; 
public $cumple; 
public $conyuge; 
public $cumple_conyuge; 
public $aniversario_bodas; 
public $empresa; 
public $direccion_empresa; 
public $tel_negocio; 
public $fecha_ingreso; 
public $num_rotario; 
public $padrino; 
public $direccion_casa; 
public $tel_casa; 
public $celular; 
public $email; 
public $email_privado; 
public $clasificacion; 
public $imagen; 
public $usuario_app; 


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

function read($filter){ 

     // select all query 
     $query = "SELECT 
        * 
       FROM 
        " . $this->table_name . " 
       ORDER BY apellidos "; 

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

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

     return $stmt; 
    } 
} 
関連する問題