EllisLab text mark
Advanced Search
     
JSON Problem
Posted: 09 March 2009 11:14 PM
Avatar
Joined: 2009-03-03
115 posts

Hello,

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


CONTROLLER

$relatedMedia['relatedMedia'$this->collmedia_model->getCollectionPreviews$offset8current_category() ); 

MODEL

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

[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 :

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

 Signature 

Yann
Web Developer
Portfolio

 
Posted: 09 March 2009 11:26 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-03-09
43 posts

Not entirely sure what you are doing in this line…

$response $_POST["jsoncallback""(" json_encode($result) . ")"

What is in $_POST[‘jsoncallback’]?
If you just want to use the result why isn’t it just:

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

function getJSONCollectionPreview$offset$per_page$mediaType {


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

        
echo json_encode($result); 
        
    
 
Posted: 09 March 2009 11:34 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-03
115 posts

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.

 Signature 

Yann
Web Developer
Portfolio

 
Posted: 10 March 2009 12:11 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-03
115 posts

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.

getCollectionPreviews$offset$per_page)
// Example getCollectionPreviews( 1, 8) will show the first packet of 8 elements 

I create a JSON object from this.

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)

 Signature 

Yann
Web Developer
Portfolio

 
Posted: 10 March 2009 12:34 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-03-09
43 posts

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.

 
Posted: 10 March 2009 09:18 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-10
3 posts

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

 Signature 

Hasti Shah
Web Developer