Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    $.getJSON parsing with PHP

    I want to use jquery $.getJSON to retrieve data from a php page. However there is no documentation or examples that show how to format the PHP page to send the data properly to the $.getJSON function(data). If anyone had any help with this i would appreciate it.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yeah that JSON-P deal seems to be a home-grown idea that is still in the embyronic stage at best. However you should be able to format the data in PHP as json though. Use PHP's json_encode() function.

    http://us3.php.net/manual/en/function.json-encode.php

  • #3
    New Coder
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    JSON for JQuery plugin Aino's Galleria

    Quote Originally Posted by Fumigator View Post
    Yeah that JSON-P deal seems to be a home-grown idea that is still in the embyronic stage at best. However you should be able to format the data in PHP as json though. Use PHP's json_encode() function.

    http://us3.php.net/manual/en/function.json-encode.php
    Yeah i checked that already, its not very helpful, the real issue here was getting the correct format for the page, that was readable by the jquery plugin i was using which is Aino's Galleria.

    I was unsure if it would read a variable returned by the php or simply the page itself. Apparantly when formatted correctly the page outputs the JSON.

    The format that worked for me was encoding each individual array with the json encode and stripping the slashes.

    If anyone is wondering this is the code I ran from the $.getJSON after querying the database and getting the first row.

    PHP Code:
    echo "[";

    do
    {

        
    $json_items = array();
        
        
        if(
    $row[8] == 1// if published
        
    {
            
    $jsonstring .= "{";
            
    //store row into value
            
    $image $row[5];
            
    // push data into items array
            
    $json_items['image'] = $image;
            
    //$jsonstring .= "'image':'".$image."',";
            
            
    $thumb $row[6];
            
    $json_items['thumb'] = $thumb;
            
    //$jsonstring .= "'thumb':'".$thumb."',";
            
            
    $title $row[3];
            if(
    $title != "Default Title"// if other than default
            
    {
                
    $json_items['title'] = $title;
                
    //$jsonstring .= "'title':'".$title."',";
            
    }
            else
            {
                
    $categID $row[2];
                
    $resultcat make_query($oConnection$database'categName''tbl_category''categ'$categID);
                
    $row2 db_fetch_row($resultcat);
                
    $json_items['title'] = $row2[0];
                
    //$jsonstring .= "'title':'".$row2[0]."',";
            
    }
            
            
    $description $row[4];
            if(
    $description != "Default Description"// if other than default
            
    {
                
    $json_items['description'] = $description;
                
    //$jsonstring .= "'description':'".$description."',";
            
    }
            
            
            
    $link $row[5];
            
    $json_items['link'] = $link;
            
    //$jsonstring .= "'link':'".$link."'";
            
            //array_push($json_array, $json_items);
            //$json_array = array($json_items);
            
    print_r(stripslashes(json_encode($json_items)));
            
            
            
            
    //echo ",\n";
            //$jsonstring .= "}";
        
    }
        
        
    $row db_fetch_row($results);
        if(!
    $row)
        {
            
        }
        else
        {
            if(
    $row[8] == 1// if published
            
    {
            echo 
    ",\n";
            
            
    //$jsonstring .= ",";
            
    }
        }
    }
    while(
    $row);

    echo 
    "]"
    Alot of the notated markup there is what did not work. It involved pushing it all into a string, then sending it through the json_encode(), which showed up in the javascript alert() back on the calling page, however it did not work.

    the proper alert() markup (the one that worked right) should look like this in your javascript alert

    Code:
    [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
    And just another note for $.getJSON you must place your gallery code within the function
    Code:
    $.getJSON(getaddress and ?query=, function(data){
    
    $('#gallery').galleria({
    
    data_type: 'json',
    data_source: data
    
    });
    
    });
    It has to be like that or it will not read the (data), and i have been unsuccessful in saving that (data) to a global variable.

    If anyone had suggestions for this that would be great, since I was hoping to be able to change that variable based on different calls to the database.

    Hope that helps some.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    That's rather bizzare, that you're using print_r() to produce output for your jQuery. You should be able to build your multi-dimensional array in PHP and simply "echo json_encode();".

  • #5
    New Coder
    Join Date
    Jan 2010
    Location
    Wisconsin
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    $.getJSON(getaddress and ?query=, function(data){
    
    $('#gallery').galleria({
    
    data_type: 'json',
    data_source: data
    
    });
    
    });
    It has to be like that or it will not read the (data), and i have been unsuccessful in saving that (data) to a global variable.

    If anyone had suggestions for this that would be great, since I was hoping to be able to change that variable based on different calls to the database.

    Hope that helps some.
    Update, you can pass the variable if you place a function in the code

    Code:
    //namespacing - very helpful
    var GA = {
    galL: function(){
    $.getJSON(getaddress and ?query=, function(data){
    
    GA.galInit(data);
    
    });
    },
    galInit: function(galdata){
    
    $('#gallery').galleria({
    
    data_type: 'json',
    data_source: data
    
    });
    
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •