my title is confusing..sorry about that..anyway..kindly look at my code:
<?php
include('../connectDB.php'); //connect to db
echo '{ ';
echo '"success": 1, ';
echo '"result": [ ';
$result = mysql_query("SELECT * FROM roominventory");
while($row = mysql_fetch_array($result)) { //start while
$getId = $row['id']; //get value
$getRoomId = $row['room'];
echo '{ ';
$ar = $row['arrival']; //assign value to variable
$dep = $row['departure'];
$date = str_replace('-', '/', $ar); //format the date
$formatArr = date('m/d/Y', strtotime($date));
$date2 = str_replace('-', '/', $dep); //format the date
$formatDep = date('m/d/Y', strtotime($date2));
$mSt = strtotime($formatArr) * 1000; //convert to milliseconds
$mEd = strtotime($formatDep) * 1000;
echo '"id": ' . '"' . $getId. '"' . ', ';
$resulta = mysql_query("SELECT * FROM rooms_amenities WHERE id='$getRoomId'");
while($rowa = mysql_fetch_array($resulta)) {
$getName = $rowa['name'];
}
echo '"title": ' . '"' . $getName . '"' . ', ';
echo '"url": ' . '"' . 'http://example.com' . '"' . ', ';
echo '"class": ' . '"' . 'event-warning' . '"' . ', ';
echo '"start": ' . '"' . $mSt . '"' . ', '; //echo the converted date
echo '"end": ' . '"' . $mEd . '" ';
echo '} ';
echo ', '; //comma (should only in between entries and not in the very end of the very last entry)
} //end while
echo '] ';
echo '}';
?>
now this is the result of the file:
{ "success": 1, "result": [ { "id": "254", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1470693600000", "end": "1471384800000" } ] }
no problem about that. now, problem is that when there's more than 1 row in the table in my db the result becomes like this:
{ "success": 1, "result": [ { "id": "255", "title": "Standard Room 201", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, { "id": "256", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, ] }
notive the "comma" at the last entry "1472076000000" }, ] }
what my desired/expected result should be like this:
{ "success": 1, "result": [ { "id": "255", "title": "Standard Room 201", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" }, { "id": "256", "title": "Standard Room 202", "url": "exampledotcom", "class": "event-warning", "start": "1471903200000", "end": "1472076000000" } ] }
notice the "comma" after the first entry and between the second entry { "id: "...}, { "id: "...} and no comma after the very last entry.
i tried to echo the comma outside/end of while-loop. but in the result, the comma is only at the very last entry, no in-between
if last row is reached then there should be no comma at the very last entry. i don't know how can i make the desired result.
is there any other approach/way to this? like using array/json_encode? ..but i don't know how to do it