Welcome Guest, Not a member yet? Register   Sign In
Writing and Pulling JSON with jquery
#1

[eluser]c2n[/eluser]
I am new to programming, and I am trying to find the proper way to use jquery to call a controller with a JSON array.

Here is my code:

Code:
<?php

class Featured extends Controller
{
    function Featured()
    {
            parent::Controller();    
    }
    
    function getEvents()
    {
        $featured_data = '      
        ({
            "events": [
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                }
            ]
        })
        ';
      
       echo $featured_data;
      
    }
}

//eof

Then in a separate jquery file I am trying to call this function:
Code:
//Loading Events use text()
          $.getJSON("featured/getEvents?callback=?",
                function(data){
                
              $.each(data.events, function(i,item){
                      
                      var title = item.title.substr(0,20);
                      
                    $("#title-"+i).text(title);
                    if ( i == 4 ) return false;
                   });
                });

Featured is the file name and getevents is the function that is above.

I'm not even able to get a alert in the function which means the data is not getting through.
Any help?
#2

[eluser]umefarooq[/eluser]
hi just have look on the following example using jquery,CI and google map using JSON it will help you

http://www.webmasterdubai.com/download.html

download and check the example how its working.or its better you have the event function in you jquery file.
#3

[eluser]Josh Ellis[/eluser]
You don't have to hand write JSON... just build an array, then use "json_encode".

Like...

Code:
class Featured extends Controller
{
    function Featured()
    {
            parent::Controller();    
    }
    
    function getEvents()
    {
        $featured_data['events'] = array(
            'id'    => '01',
            'title'    => 'CAVANAUGH...',
            'img'    => 'images/thumbs/demo1.jpg'
        );
        
        $featured_data = json_encode($featured_data);
        echo $featured_data;
    }
}

Also, put something like this in your view

Code:
<a href="j@v@script:void(0)" id="test_link">Click</a>

And in the head of your view link to the full path for your jquery file. This often catches people out.

What if you change the URL below to be the full URL without the callback (it's on your own website, yeah?) Also, you MAY be leaving out the "index.php" from the URL.

Code:
$().ready(function(){
    $('#test_link').click(function(){
        $.getJSON("http://localhost/ci/index.php/featured/getEvents", function(data){
            $.each(data.events, function(i,item){
                var title = item.title.substr(0,20);
                $("#title-" + i).text(title);
                if ( i == 4 ) return false;
            });
        });
    });
});

Just replace the "@'s" with "a"
#4

[eluser]c2n[/eluser]
Thanks, I got it without doing that, there was a format issue in the JSON, your method helps with that.

I am trying to bind this using ajax to a list of calendar events, but I have no clue on to how to go about this. This code binds to the php loop by using i++ but I think there has to be a better way, even then I am not sure how to do the click events that load different data based on the date.

Any help or ways I can go?
#5

[eluser]c2n[/eluser]
Using this:

Code:
function getEvents()
    {
        $featured_data['events'] = array(
            'id'    => '01',
            'title'    => 'CAVANAUGH',
            'img'    => 'images/thumbs/demo1.jpg'
        );
        
        $featured_data = json_encode($featured_data);
        echo $featured_data;
    }

Doesn't Pull:

but this works, why is that? I thought json_encode wrote it in the same way:

Code:
function getEvents()
    {
        $featured_data = '      
        ({
            "events": [
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                },
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                },
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                },
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                },
                {
                    "id": "01",
                    "title": "CAVANAUGH\'S RIVER DECK FRIDAY NITES!!!",
                    "img" : "images/thumbs/demo1.jpg",
                    "author" : "",
                    "author_events_total" : "102",
                    "description": "This place is kewl!",
                    "recomend" : "true",
                    "link" : "events/",
                    "total_attending" : "2,030",
                    "rsvp_status" : "yes",
                    "like_total" : "20",
                    "comment_total" : "203",
                    "location": ""
                }




            ]
        })
        ';
      
       echo $featured_data;
      
    }




Theme © iAndrew 2016 - Forum software by © MyBB