0

I am trying to get the following json_encode output:

The Array:

echo json_encode(array(
             array(
             'id' => 111,
             'title' => "Event1",
             'start' => "2012-8-10",
             'end' => "2012-8-15",
             'url' => "http://yahoo.com/",
                ),
            array(
             'id' => 222,
             'title' => "Event2",
             'start' => "2012-8-20",
             'end' => "2012-8-22",
             'url' => "http://yahoo.com/"
               )
              ));

OUTPUT:

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

However, when I am using the following code

global $wpdb;
$row = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'calendar' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date ASC");
foreach ($row as $post) {
    $postid = $post->ID;
    $post_title = $post->post_title;
    $startDate =  get_post_meta($post->ID,'calendar_start-date',true);
    $endDate =  get_post_meta($post->ID,'calendar_end-date',true);
    $link =  get_post_meta($post->ID,'calendar_link',true);
    $arr = array('id' => $postid, 'title' => $post_title, 'start' => $startDate, 'end' => $endDate, 'url' => $link);
    echo json_encode($arr);
    }

I get the output as

{"id":"320","title":"Test Event One","start":"2012-8-17","end":"2012-8-24","url":"http:\/\/www.yahoo.com"}{"id":"321","title":"Test Event Two","start":"2012-8-21","end":"2012-8-30","url":"http:\/\/www.google.com"}

I understand it is probably due to the json_encode being inside the foreach loop. However when I try it outside, it only shows the first result. How to combine these foreach values to achieve the output as mentioned above?

1 Answer 1

3

you could use this:

global $wpdb;
$posts = array();
$row = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'calendar' AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date ASC");
foreach ($row as $post) {
    $postid = $post->ID;
    $post_title = $post->post_title;
    $startDate =  get_post_meta($post->ID,'calendar_start-date',true);
    $endDate =  get_post_meta($post->ID,'calendar_end-date',true);
    $link =  get_post_meta($post->ID,'calendar_link',true);
    $arr = array('id' => $postid, 'title' => $post_title, 'start' => $startDate, 'end' => $endDate, 'url' => $link);
    $posts[] = $arr;
}
echo json_encode($posts);

I think that should work

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

Comments

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.