简体   繁体   中英

PHP/ JSON: Data from MySQL database does not appear in calendar

Currently, I created a system that uses daypilot calendar. For example, when I clicked a date, it will show the list of activities on that selected date.

Below code is successful display the list:

<?php

require_once '../../../config/configPDO.php';

$Fac_ID = strtolower($_GET['Fac_ID']);
$Room_Desc = strtolower($_GET['Room_Desc']);

$stmt = $conn->prepare("SELECT * FROM booking LEFT JOIN room ON booking.Room_ID = room.Room_ID 
WHERE room.Fac_ID = '$Fac_ID' AND room.Room_Desc = '$Room_Desc' AND Book_Status <> 'Reject'");

$stmt->execute();
$result = $stmt->fetchAll();

class Event {}
$events = array();

foreach($result as $row) {
  $e = new Event();
  $e->id = $row['Book_No'];
  $e->text =  "Meeting Name: ". $row['Meeting_Description']. " || " ."Requested by: ".$row['Requested_by']. " || " ."Location: ".$row['Fac_ID']. ", " .$row['Room_Desc'];
  $e->start = $row['StartTime'];
  $e->end = $row['EndTime'];
  $events[] = $e;
}

header('Content-Type: application/json');
echo json_encode($events);

?>

But, when I change the code (Because want to retrieve json data), the data doesn't display at calendar. Below is the code:

<?php

      require_once '../../../config/configPDO.php';

      $Fac_ID = strtolower($_GET['Fac_ID']);
      $Room_Desc = strtolower($_GET['Room_Desc']);

      //retrieve json
      $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=$Fac_ID&Room_Desc=$Room_Desc&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
      $data = file_get_contents($url); //line 10
      $json = json_decode($data); 
      $result = $json->bookingList; //line 12

      class Event {}
      $events = array(); //

      foreach($result as $row) { //line 17
        $e = new Event();
        $e->id = $row->bookNo;
        $e->text =  "Meeting Name: ".$row->desc. " || " ."Requested by: ".$row->requestedBy." || " ."Location: ".$row->facID.", " .$row->roomName;
        $e->start = $row->startTime;
        $e->end = $row->endTime;
        $events[] = $e;
      }

      header('Content-Type: application/json');
      echo json_encode($events);
  ?>

And, This is the error that I found at console:

  1. PHP Warning: file_get_contents( http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=TGT&Room_Desc=LEVEL 1 &Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\\inetpub\\wwwroot\\ebooking\\pages\\manual_booking\\backend\\backend_events.php on line 10

  2. PHP Notice: Trying to get property 'bookingList' of non-object in C:\\inetpub\\wwwroot\\ebooking\\pages\\manual_booking\\backend\\backend_events.php on line 12

  3. PHP Warning: Invalid argument supplied for foreach() in C:\\inetpub\\wwwroot\\ebooking\\pages\\manual_booking\\backend\\backend_events.php on line 17

Can I know where I need to change? Thanks

This is probably because of the space in "Level 1". Try urlencoding your $_GET parameters:

$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID=" . urlencode($Fac_ID) . "&Room_Desc=" . urlencode($Room_Desc) . "&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";

If you want to make the variable substitution more explicit, you could use strtr :

$template = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayPABooking?Fac_ID={Fac_ID}&Room_Desc={Room_Desc}&Room_ID=&Book_No=&Book_Date_From=&Book_Date_To=";
$url = strtr($template, array(
    '{Fac_ID}' => $Fac_ID,
    '{Room_Desc}' => $Room_Desc
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM