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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    201
    Thanks
    12
    Thanked 26 Times in 26 Posts

    php array/jquery ajax method/append row

    Hello I am doing some research on this topic. My knowledge of jquery and PHP are limited but I think this could be pretty easy to do, if you guys could point me in the right direction I would really appreciate it.

    My goal is to append two rows of static data vs just one. I know I need to change the array so it spits out two data sets and then change the jquery method to handle the two sets, but I don't have enough knowledge to change it and make it scale so if I need to handle 30 sets off data it can.

    my environment:

    jquery append row based on json data received from php file.
    PHP Code:
    $(document).ready(function() {
        $(
    "#btnAdd").click(function() {

            
    // get data
            
    $.ajax({
                
    url'/ajax/data.php',
                
    type"GET",
                
    cachefalse,
                
    error: function(data) {
                    $(
    "div#error").html('error: '+data);
                },
                
    success: function(data) {
                        var 
    json jQuery.parseJSON(data);
                        var 
    newRow = $("<tr><td>"+json.icon+"</td><td>"+json.app+"</td><td>"+json.timestamp+"</td><td>"+json.location+"</td><td>"+json.comments+"</td></tr>");
                        $(
    "#mainTable tbody").append(newRow);
                }    
            });
        });
    }); 
    php associative array that produces the data.

    PHP Code:
    <?php

    $retArray 
    = array();
    $retArray['icon'] = 'icon';
    $retArray['app'] = 'GGP';
    $retArray['timestamp'] = date('m/d/Y');
    $retArray['location'] = 'Bellevue';
    $retArray['comments'] = 'It works!';

    echo 
    json_encode($retArray);

    ?>
    thanks - Micah

  • #2
    New Coder
    Join Date
    May 2010
    Posts
    69
    Thanks
    3
    Thanked 10 Times in 10 Posts
    Make a multidimensional array()?
    PHP Code:
    $data = array();
    $data[] = array('id' => 2'foo' => 'bar');
    $data[] = array('id' => 6'woof' => 'meow');
    header('Content-Type: application/json');
    echo 
    json_encode($data); 
    jQuery should automagically json_decode the array if you send it the proper content type.
    PHP Code:
     success: function(data) { 
        for (var 
    i in data) {
             var 
    rowData data[i];
             
    console.log(rowData);
        }
     }
    // or even jQuery.each()
     
    success: function(data) { 
       $.
    each(data, function(iarr) {

       });
     } 
    Last edited by rvincenten; 03-30-2012 at 11:00 PM.
    Like helpful posts in your topic? Thank the people offering you help :)

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    201
    Thanks
    12
    Thanked 26 Times in 26 Posts
    Thanks the each method http://api.jquery.com/each/ I Think is deffinitely what I want to use to pass through my array values. Still need to figure it out a little better and how it works with the mutli-dimensional array.

    PHP Code:
    <?php

    $dataArray 
    = array 
            ( 
                
    'id1'  => array 
                        
                        (     
    'icon' => 'icon',
                            
    'app' => 'GGP',
                            
    'timestamp' => date('m/d/y'),
                            
    'loction' => 'Bellevue',
                            
    'comments' => 'It works!',
                                ),
                
    'id2'  => array 
                        
                        (     
    'icon' => 'icon',
                            
    'app' => 'Meijer',
                            
    'timestamp' => date('m/d/y'),
                            
    'loction' => 'San diego',
                            
    'comments' => 'It works!',
                                )
            );
                 
    echo 
    json_encode($dataArray);    

    ?>


  •  

    Posting Permissions

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