0

Hello,

I'm having a problem with a script I'm working on, basically, it outputs a SQL query in JSON, however, it's not doing it properly.. E.g.

Right way:

[{"id":111,"title":"Event1","start":"2011-10-10","url":"http:\/\/yahoo.com\/"},{"id":222,"title":"Event2","start":"2011-10-20","end":"2011-10-22","url":"http:\/\/yahoo.com\/"}]

Wrong way:

[{"id":"1","title":"dfssafsaf","start":"2011-10-22","url":"http:\/\/yahoo.com\/"},{"id":"3","title":"dfssafsaf","start":"1-1-1","url":"http:\/\/yahoo.com\/"}[b],][/b]

As you can see it inserts a ",]" and I only want it to show "]" because it's the end of the results..

Here is the code:

<?php

    $dbh=mysql_connect ("localhost", username", "password") or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("tbl_name");

    $get_events = "SELECT * FROM calendar";
    $get_results = mysql_query($get_events);
    $i;

    if(mysql_affected_rows() >= 1)
    {   
        echo '[';
        while ($post = mysql_fetch_array($get_results))
        {
            $year = $post['year'];
            $month = $post['month'];
            $day = $post['day'];
            $json = array ('id' => $post['calendar_id'],'title' => "dfssafsaf", 'start' => "$year-$month-$day", 'url' => "http://yahoo.com/");
            $output = json_encode($json) . ',';
            echo $output;   
        }   
        echo ']';

      }else{
      echo 'No';

    }
?>

Any suggestions?

Thanks :)

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pritaeas
0
if(mysql_affected_rows() >= 1)
{
  $json = array();	
  while ($post = mysql_fetch_array($get_results))
  {
    $year = $post['year'];
    $month = $post['month'];
    $day = $post['day'];
    $json[] = array ('id' => $post['calendar_id'],'title' => "dfssafsaf", 'start' => "$year-$month-$day", 'url' => "http://yahoo.com/");
  }
  echo json_encode($json);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.