2

I need to load database content according to the following scheme into the variable data.

var data = {
    "62": {
        sku: "62",
        section: "bodyImage",
        img: "images/diy-images/config-images/62.png",
        label: "plain red",
        price: "100"
    },
    "63": {
        sku: "63",
        section: "bodyImage",
        img: "images/diy-images/config-images/63.png",
        label: "plain pink",
        price: "110"
    },
    "360": {
        sku: "360",
        section: "bodyImage",
        img: "images/diy-images/config-images/360.png",
        label: "plain gray",
        price: "120"
    },
};​

I tried to achieve that with the following function but it doesn't work out. What am I missing?

var data = (function() {
    $.ajax({
        url: 'get_data.php',
        data: "",
        dataType: 'json',
        success: function(rows) {
            for (var i in rows) {
                var row = rows[i];

                var id = row[0];
                var section = row[1];
                var img = row[2];
                var label = row[3];
                var price = row[4];
            }
        }
    });
});​
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
Stefan Schneider
  • 157
  • 3
  • 18

3 Answers3

1

You can get json object value by '.' operator

var data = (function() {

    $.ajax({
        url: 'get_data.php',
        data: "",
        dataType: 'json',
        success: function(rows) {
            for (var i in rows) {

                var id = row.sku;          
                var section = row.section;
                var img = row.img;
                var label = row.label;
                var price = row.price;

            }
        }
    });

});​
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
Logan
  • 1,682
  • 4
  • 21
  • 35
0

You can address the properties by name:

for (var i in rows) {
    var row = rows[i];          

    var id = row.sku;          // or var id = i;
    var section = row.section;
    var img = row.img;
    var label = row.label;
    var price = row.price;
    ...
}
VisioN
  • 143,310
  • 32
  • 282
  • 281
0
var data = {}; // data variable for future use
$.ajax({
 url: '',
 data: '',
 dataType: 'json',
 success: function(rows) {
   $.each(rows, function(i, val) {
      var id = val.sku,
          section = val.section,
          img = val.img,
          label = val.label,
          price = val.price;
   });
   // if you want to update data variable then
   data[sku] = {sku: sku, img: img, section: section, label: label, price: price};
  // to set data to a content
   $('#output_div').html(JSON.stringify(data));
 }
})
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Thanks, however I still have problems with the output of the data. If I output them into a div I only get undefined values. I need to have the output exactly in the same way as in the example at the top of the page. – Stefan Schneider Jun 02 '12 at 11:53
  • The only output I get is {}. Maybe something wrong with the mysql query: $result = mysql_query("SELECT * FROM table"); $data = array(); while ( $row = mysql_fetch_row($result) ) { $data[] = $row; } echo json_encode( $data ); – Stefan Schneider Jun 02 '12 at 14:17