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("&nbsp;&nbsp;{\"itemId\":\"%s\",<br>", $myrow["ID"]);
  print "&nbsp;&nbsp;\"itemType\":\"BT_MapLocation\",<br>";
  printf("&nbsp;&nbsp;\"latitude\":\"%s\",<br>", $myrow["Latitude"]);
  printf("&nbsp;&nbsp;\"longitude\":\"%s\",<br>", $myrow["Longitude"]);
  printf("&nbsp;&nbsp;\"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

    Where is there an array for you to work with? You are printing it out - not assigning it to a variable that you can modify. unless you use output buffering there is no way to change something you've already output.

      Well, given the code I've posted above, is there a way to accomplish what I'm looking to do? Or, is there a better way to do the whole thing? I'm up for learning as much as I can!

      Mark

        Given your current code:

        $result = mysql_query("SELECT ID, Latitude, Longitude, Description from theguide",$db);
        $num = mysql_num_rows($result);
        $i = 1;
        print "{\"childItems\":[<br>";
        if ($myrow = mysql_fetch_array($result)) do {
          printf("&nbsp;&nbsp;{\"itemId\":\"&#37;s\",<br>", $myrow["ID"]);
          print "&nbsp;&nbsp;\"itemType\":\"BT_MapLocation\",<br>";
          printf("&nbsp;&nbsp;\"latitude\":\"%s\",<br>", $myrow["Latitude"]);
          printf("&nbsp;&nbsp;\"longitude\":\"%s\",<br>", $myrow["Longitude"]);
          printf("&nbsp;&nbsp;\"title\":\"%s\"}", $myrow["Description"]);
          if( $num < $i ) printf(",");
          printf("<br><br>");
          $i++;
        } while ($myrow = mysql_fetch_array($result));
          else {
          echo "Sorry, nothing there to see!";
          }
        print "<br>]}";
        

          Thanks! This worked once I changed $num < $i to $num > $i. Before that, it would not print any commas at all. But now its working as I need.

          Is there a better, more elegant way to do this in general? You can refer me to a particular PHP doc page or something...you don't need to write the code (unless you have nothing else to do!).

          Mark

            HA my bad I meant to write $i < $num ... I transpose characters, words, etc all the time sorry.
            As far as more elegant - I have no idea =D The counter method is how I do on every 5th row, every last row, etc when the need arises. The only thing I would change, is all those print/printf's i would change to single quote delimited strings, since you don't have any variables in the strings that need parsing (PHP has to search each string for a var before passing to the function) and would also remove the need to escape the quotes within the string (making it more readable in my mind).

              Hi,

              I'm not quite sure what you mean about the print/printf's. Could you provide me an example?

              Appreciate all your help...this all goes into my bag of tricks for the future!

              Mark

                Basically I just mean when you delimit strings that don't need to be parsed by PHP to use single quotes ' instead of " otherwise PHP is going to try to parse it (even if it doesn't need to, because how would it know) and example for one of your lines:

                  printf('&nbsp;&nbsp;{"itemId":"&#37;s",<br>', $myrow['ID']);

                  Ah, ok...gotcha. Thank you for that example...helps a lot!

                    GoNorthWest wrote:

                    Is there a better, more elegant way to do this in general?

                    How about [man]json_encode[/man]?

                      Write a Reply...