So I need to retreive the data from backendless and echo it properties one by one.
SO this is my Pasien.php class where there are setter and getter:
<?php
namespace Hospinet;
class Pasien
{
private $Tgl_Lahir;
private $Tgl_Masuk;
private $Perawatan;
private $No_Kamar;
private $Nama_Pasien;
private $No_Asuransi;
private $Jenis_Asuransi;
private $Jenis_Kamar;
private $Id_RS;
private $Id_Dokter;
private $Id_Pasien;
private $Gender;
public function construct( $tgl_Lahir, $tgl_Masuk, $perawatan, $no_Kamar, $nama_Pasien, $no_Asuransi, $jenis_Asuransi, $jenis_Kamar, $id_RS, $id_Dokter, $id_Pasien, $gender ) {
$this->Tgl_Lahir = $tgl_Lahir;
$this->Tgl_Masuk = $tgl_Masuk;
$this->Perawatan = $perawatan;
$this->No_Kamar = $no_Kamar;
$this->Nama_Pasien = $nama_Pasien;
$this->No_Asuransi = $no_Asuransi;
$this->Jenis_Asuransi = $jenis_Asuransi;
$this->Jenis_Kamar = $jenis_Kamar;
$this->Id_RS = $id_RS;
$this->Id_Dokter = $id_Dokter;
$this->Id_Pasien = $id_Pasien;
$this->Gender = $gender;
}
public function getTgl_Lahir() {
return $this->Tgl_Lahir;
}
public function setTgl_Lahir( $Tgl_Lahir ) {
$this->Tgl_Lahir = $Tgl_Lahir;
}
public function getTgl_Masuk() {
return $this->Tgl_Masuk;
}
public function setTgl_Masuk( $Tgl_Masuk ) {
$this->Tgl_Masuk = $Tgl_Masuk;
}
public function getPerawatan() {
return $this->Perawatan;
}
public function setPerawatan( $Perawatan ) {
$this->Perawatan = $Perawatan;
}
public function getNo_Kamar() {
return $this->No_Kamar;
}
public function setNo_Kamar( $No_Kamar ) {
$this->No_Kamar = $No_Kamar;
}
public function getNama_Pasien() {
return $this->Nama_Pasien;
}
public function setNama_Pasien( $Nama_Pasien ) {
$this->Nama_Pasien = $Nama_Pasien;
}
public function getNo_Asuransi() {
return $this->No_Asuransi;
}
public function setNo_Asuransi( $No_Asuransi ) {
$this->No_Asuransi = $No_Asuransi;
}
public function getJenis_Asuransi() {
return $this->Jenis_Asuransi;
}
public function setJenis_Asuransi( $Jenis_Asuransi ) {
$this->Jenis_Asuransi = $Jenis_Asuransi;
}
public function getJenis_Kamar() {
return $this->Jenis_Kamar;
}
public function setJenis_Kamar( $Jenis_Kamar ) {
$this->Jenis_Kamar = $Jenis_Kamar;
}
public function getId_RS() {
return $this->Id_RS;
}
public function setId_RS( $Id_RS ) {
$this->Id_RS = $Id_RS;
}
public function getId_Dokter() {
return $this->Id_Dokter;
}
public function setId_Dokter( $Id_Dokter ) {
$this->Id_Dokter = $Id_Dokter;
}
public function getId_Pasien() {
return $this->Id_Pasien;
}
public function setId_Pasien( $Id_Pasien ) {
$this->Tgl_Masuk = $Tgl_Masuk;
}
public function getGender() {
return $this->Gender;
}
public function setGender( $Gender ) {
$this->Gender = $Gender;
}
}
I tried something like this in my index.php
<?php
namespace Hospinet;
use backendless\Backendless;
use backendless\services\persistence\BackendlessDataQuery;
use backendless\model\BackendlesCollection;
use Hospinet\Pasien;
include "backendless/autoload.php";
include "pasien.php";
Backendless::initApp('appid', 'REST key', 'v1');
$Pasien = new Pasien();
$result_collection = Backendless::$Persistence->of("Pasien")->find($data_query_or_relation_depth = null);
print_r( $result_collection->getAsArray() );
?>
And when I run it, this is what I got
backendless\model\BackendlessCollection Object
(
[data] => Array
(
[offset] => 0
[data] => Array
(
[0] => Array
(
[Id_Dokter] => C0E0607E
[created] => 1495593955000
[Gender] => L
[ownerId] => [Id_Pasien] => 1
[No_Asuransi] => H259030
[__meta] => {"relationRemovalIds":{},"selectedProperties":["Id_Dokter","created","Gender","ownerId","Id_Pasien","No_Asuransi","Nama_Pasien","No_Kamar","Tgl_Lahir","Perawatan","Jenis_Asuransi","Jenis_Kamar","Id_RS","___class","Tgl_Masuk","updated","objectId"],"relatedObjects":{}}
[Nama_Pasien] => Agustinus Kuncoro
[No_Kamar] => ICU-2
[Tgl_Lahir] => 29 Maret 1979
[Perawatan] => Hijau
[Jenis_Asuransi] => BPJS
[Jenis_Kamar] => ICU
[Id_RS] => 80923920
[___class] => Pasien
[Tgl_Masuk] => 17 Maret 2017
[updated] => 1495594474000
[objectId] => 347D197A-7235-04AB-FF0A-F9D5215F2F00
)
)
[nextPage] => [totalObjects] => 1
)
[stored_next_page_link] =>
)
Well it works, but what I want is to print the properties separately... something like this when I run the PHP code:
Name: Agustinus Kuncoro
Id = 1
Gender = L
I am new to PHP so I am really confused, could I use getter like in Android in PHP?
The response is simply a native PHP Array, so you can work with it respectively:
print_r($result_collection->getAsArray()[0]['Id_Dokter'])
print_r($result_collection->getAsArray()[0]['Name_Pasien'])
print_r($result_collection->getAsArray()[0]['Gender'])
Or simpler with echo
, since you need print_r
only to print arrays:
echo $result_collection->getAsArray()[0]['Id_Dokter']
echo $result_collection->getAsArray()[0]['Name_Pasien']
echo $result_collection->getAsArray()[0]['Gender']
You can also iterate each item in array:
foreach ($result_collection->getAsArray() as &$item)
echo $item['Id_Dokter'] . "\n";