Hi,
I have a database of locations, and I am building a JSON text file by going through the array, pulling specific data, and outputting in a JSON format. The format that it needs to go into looks like this :
{"itemId":"419993F0FEC3312C797C91E",
"itemType":"BT_mapLocation",
"latitude":"36.615763",
"longitude":"-121.904234",
"title":"Location 2",
"subTitle":"801 Lighthouse Ave Monterey CA 93940"},
{"itemId":"11F25D4F6120F19E7F4B220",
"itemType":"BT_mapLocation",
"latitude":"36.5977439",
"longitude":"-121.8949035",
"title":"Location 3",
"subTitle":"823 Alvarado St Monterey CA 93940"}
Not that the end of the first entry ends in a comma, while the end of the last entry does not have a comma. That is a JSON requirement.
I have figured out how to go through my DB, create an array, and spit out the information in the required format. The code I am using (for better or worse) is the following :
$result = mysql_query("SELECT ID, Latitude, Longitude, Description from theguide",$db);
print "{\"childItems\":[<br>";
if ($myrow = mysql_fetch_array($result)) do {
printf(" {\"itemId\":\"%s\",<br>", $myrow["ID"]);
print " \"itemType\":\"BT_MapLocation\",<br>";
printf(" \"latitude\":\"%s\",<br>", $myrow["Latitude"]);
printf(" \"longitude\":\"%s\",<br>", $myrow["Longitude"]);
printf(" \"title\":\"%s\"},<br><br>", $myrow["Description"]);
} while ($myrow = mysql_fetch_array($result));
else {
echo "Sorry, nothing there to see!";
}
print "<br>]}";
What I cannot do is figure out how to strip the comma from just the last entry. I understand the basics of stripping a character from a string (more or less), but am stuck on how to do it for just one string in particular, and that being the last string in the array?
Any help would be greatly appreciated!
Mark