0

I have tried to add a php file as the source of data for a jquery calendar that uses json as below:

    <script>
     $(document).ready(function() {
        $("#eventCalendarHumanDate").eventCalendar({
        eventsjson: 'modules/events/json/event.humanDate.json.php',
        jsonDateFormat: 'human'  
            });
      });
   </script> 

The php file works when i echo variables only but when i connected to the database and looped, it fails and i get the error "error getting json" But running my code separately i get no error from the php file itself.

    <?php
$hostname_app_conn = "localhost";
$database_app_conn = "xx";
$username_app_conn = "xx";
$password_app_conn = "";
$app_conn = mysql_pconnect($hostname_app_conn, $username_app_conn, $password_app_conn) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_app_conn, $app_conn );

$query_rs_content = "SELECT * FROM `mod_events_events` WHERE `active`=1 ORDER BY `Id` LIMIT 365";
$rs_content = mysql_query($query_rs_content, $app_conn) or die(mysql_error());
$totalRows_rs_content = mysql_num_rows($rs_content);

header('Content-type: text/json');

echo '[';
$separator = "";
$days = 16;

$i = 1;
echo $separator;
    while($row_rs_content = mysql_fetch_assoc($rs_content))
    {

    echo '  { "date": "'.$row_rs_content['eventday'].'", "type": "'.$row_rs_content['type'].'", "title": "'.$row_rs_content['Title'].'", "description": "'.$row_rs_content['teasertext'].'", "url": "" },';
        }

    $separator = ",";

echo ']';
?>

thanks in advance.

3
  • 2
    You do realize php has a built in function for generating json arrays right? Commented Jan 23, 2014 at 18:43
  • php.net/json_encode Commented Jan 23, 2014 at 18:44
  • please enlighten me. am not that good. Commented Jan 23, 2014 at 18:45

1 Answer 1

1

You should use the json_encode() function, something like this:

//more code above
$array = new array();
while($row_rs_content = mysql_fetch_assoc($rs_content))
{
    $array[] = array(
        'date' => $row_rs_content['eventday'],
        'type' => $row_rs_content['type'],
        'title' => $row_rs_content['Title'],
        'description' => $row_rs_content['teasertext'],
        'url' => '',
    );
}
header('Content-type: application/json');
echo json_encode($array);
die();

Most likely you had some character not being escaped properly or something else that was causing it not to be a valid json array so Javascript is dying trying to parse it.

Sign up to request clarification or add additional context in comments.

5 Comments

This worked as if i did nothing. I think i should get some tutorial. actually, i have been using PHP for a long time joining stuff here and there but i have not educated myself. what is your recommendation?
Recommendation for educating yourself?
Nope, some recommended tutorial or how did you get to this level yourself? am interested..
Practice, lots of practice and anytime I didn't know how to do something google search or stackoverflow search. Also if you can find open source projects download the code and see how others have solved similar problems.
just trying your code from above $array = new array(); can be deleted, i now try to use ajax $post() sending each current loaded month to db and fetch only loaded month events.... pfffff need to find out 1st how to start :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.