Welcome Guest, Not a member yet? Register   Sign In
JSON Problem
#1

[eluser]yannyannyann[/eluser]
Hello,

I would like to use JSON in my project, I am new to this and I am still stuck:


CONTROLLER
Code:
$relatedMedia['relatedMedia'] = $this->collmedia_model->getCollectionPreviews( $offset, 8, current_category() );

MODEL
Code:
function getJSONCollectionPreview( $offset, $per_page, $mediaType ) {



        $result = $this->db->query(" .... " )->result();
// the query is not the problem

        $response = $_POST["jsoncallback"] . "(" . json_encode($result) . ")";
        echo $response;            
        
    }


VIEW

Code:
[removed]
    
        $(function() {
            
            console.log ( 'before' );            
            
            //retrieve comments to display on page
            $.getJSON("http://localhost:8888/cedh/codeigniter/photo?jsoncallback=?", function(data) {
            
                    
            console.log ( 'line' );
                
            
                }
            });    
        });        
    [removed]


And in Firebug I have this information :

Code:
before
GET http://localhost:8888/cedh/codeigniter/photo?jsoncallback=jsonp1236651123498&_=1236651123664
http://localhost:8888/cedh/codeigniter/photo?jsoncallback=jsonp1236651123498&_=1236651123664
200 OK 84ms    
jquery-1....1.min.js (line 19)
syntax error
[Break on this error] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n


I have no idea how to really implement the JSON, I tried several things tonight, but still get this error message.

I am loooost Smile
#2

[eluser]jacobc[/eluser]
Not entirely sure what you are doing in this line...
Code:
$response = $_POST["jsoncallback"] . "(" . json_encode($result) . ")";
What is in $_POST['jsoncallback']?
If you just want to use the result why isn't it just:
Code:
echo json_encode($result)

I use mootools... but basically you would get the response in javascript and decode it using whatever function you have available, and you'd have the result array.

And you would most likely want to json_encode an array. So I'd use ->result_array() instead.

MODEL
Code:
function getJSONCollectionPreview( $offset, $per_page, $mediaType ) {



        $result = $this->db->query(" .... " )->result_array();
// the query is not the problem

        echo json_encode($result);
        
    }
#3

[eluser]yannyannyann[/eluser]
OK,

now I have that error : "Call to undefined function json_encode()" in my model

I thaught it was natively supported ?


-> I upgraded to PHP 5.2.6 and json_encode() works now.
#4

[eluser]yannyannyann[/eluser]
But I still don't understand how I could do what I want:

- I have table in my database that contains let's say 20 entries.
- I want to display in my view, only 8 thumbnails.
- When you click on previous / next links you can see the next / previous 8 thumbnails.

So I have my PHP function that does the SQL query.
Code:
getCollectionPreviews( $offset, $per_page)
// Example getCollectionPreviews( 1, 8) will show the first packet of 8 elements
I create a JSON object from this.
Code:
json_encode()
Then I should be able to read my JSON in my view with jQuery, but how ?



Also,
How do I call my PHP function to load the next or previous 8 thumbnails ? (click on link NEXT / PREVIOUS)
#5

[eluser]jacobc[/eluser]
I'm not an expert in jQuery...

Are you sure json is the best way to address the problem?

Would it be easier to get an ajax call to give you the html to display the images? Create a view collectionpreview.php and access it through a controller/action.

If you want to use json. You would want to echo a json encoded multidimensional array...

In the form of maybe:
$response[0]['src'] = path_to_image
$response[0]['link'] = link_to_view_page

And then you would need to create the elements using jQuery.
#6

[eluser]Hasti[/eluser]
Hi
Listen Just get Dataset array from database and then
use
$result = json_encode(dataset);
echo $result;

Then
In View
In responseText you will get the whole array.
Now process that array in jquery using .each // Loop
or manually responseText['0']['datafield']

If you want example than i will make for you just reply it




Theme © iAndrew 2016 - Forum software by © MyBB