Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2012, 05:31 PM   PM User | #1
SeattleMicah
Regular Coder

 
Join Date: Dec 2011
Posts: 201
Thanks: 12
Thanked 26 Times in 26 Posts
SeattleMicah is an unknown quantity at this point
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
SeattleMicah is offline   Reply With Quote
Old 03-30-2012, 10:57 PM   PM User | #2
rvincenten
New Coder

 
Join Date: May 2010
Posts: 69
Thanks: 3
Thanked 10 Times in 10 Posts
rvincenten is an unknown quantity at this point
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) {

   });
 } 
__________________
Like helpful posts in your topic? Thank the people offering you help :)

Last edited by rvincenten; 03-30-2012 at 11:00 PM..
rvincenten is offline   Reply With Quote
Old 04-02-2012, 06:09 PM   PM User | #3
SeattleMicah
Regular Coder

 
Join Date: Dec 2011
Posts: 201
Thanks: 12
Thanked 26 Times in 26 Posts
SeattleMicah is an unknown quantity at this point
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);    

?>
SeattleMicah is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 PM.


Advertisement
Log in to turn off these ads.