} }); //Get Method to get the data from database function days($givendate) { $now = time(); // or your date as well $your_date = strtotime($givendate); $datediff = $your_date - $now; return floor($datediff / (60 * 60 * 24)); } $app->get('/houses(/:id)', function ($id = null) use($app, $db) { if ($id == null) { $data = array(); $count = 0; foreach ($db->houses() as $houses) { $houses_tenants = array(); $houses_deposits = array(); foreach ($houses->houses_tenants() as $p) { $count++; $houses_tenants[] = array('id' => $p->tenants['id'], 'name' => $p->tenants["name"], 'phone' => $p->tenants["phone"]); } foreach ($houses->houses_deposits() as $p) { $houses_deposits[] = array('id' => $p->deposits['id'], 'rent' => $p->deposits["rent"], 'date' => $p->deposits["date"], 'status' => $p->deposits["status"]); } $data[] = array('house_id' => $houses['id'], 'house_name' => $houses['name'], 'house_address' => $houses['address'], 'house_entry_date' => $houses['entry_date'], 'house_fixed_date' => $houses['due_date'], 'house_rent' => $houses['rent'], 'house_no_of_rooms' => $houses['totalrooms'], 'house_totaldeposit' => $houses['totaldeposit'], 'house_totaldepositleft' => $houses['depositleft'], 'house_dthbill' => $houses['dthbill'], 'house_dthbilldate' => $houses['dthbilldate'], 'house_dthbilldays' => days($houses['dthbilldate']), 'house_powerbill' => $houses['powerbill'], 'house_powerbilldate' => $houses['powerbilldate'], 'house_powerbilldays' => days($houses['powerbilldate']), 'house_wifibill' => $houses['wifibill'], 'house_wifibilldate' => $houses['wifibilldate'], 'house_wifibilldays' => days($houses['wifibilldate']), 'house_owner' => array('name' => $houses->owners['owner_name'], 'address' => $houses->owners['owner_address'], 'phone' => $houses->owners['owner_phone'], 'email' => $houses->owners['owner_email']), 'tenants' => $houses_tenants, 'deposits' => $houses_deposits); } } else { $data = null; if ($houses = $db->houses()->where('id', $id)->fetch()) { $houses_tenants = array();
$app->response()->header('content-type', 'application/json'); echo json_encode($tenants); }); $app->get('/owners/search/:id', function ($id = null) use($app, $db) { $data = array(); foreach ($db->owners()->where("owner_name LIKE ?", "%" . $id . "%") as $owners) { $data[] = array('id' => $owners['id'], 'address' => $owners['owner_address'], 'name' => $owners['owner_name'], 'phone' => $owners['owner_phone'], 'email' => $owners['owner_email']); } $tenants = array('aaData' => $data); $app->response()->header('content-type', 'application/json'); echo json_encode($tenants); }); //Post method to insert data into database $app->post('/houses', function () use($app, $db) { $array = (array) json_decode($app->request()->getBody()); $data = $db->houses()->insert($array); $app->response()->header('Content-Type', 'application/json'); echo json_encode($data['id']); }); $app->post('/sellers_products', function () use($app, $db) { $array = (array) json_decode($app->request()->getBody()); $data = $db->sellers_products()->insert($array); $app->response()->header('Content-Type', 'application/json'); echo json_encode($data['id']); // echo json_encode($array); }); $app->post('/houses_tenants', function () use($app, $db) { $array = (array) json_decode($app->request()->getBody()); $data = $db->houses_tenants()->insert($array); $app->response()->header('Content-Type', 'application/json'); echo json_encode($data);