0

what i need

  • i need to show the count on icon .

  • i need to make dynamic div from input using array using javascript.

array structure

            Array
        (
            [0] => Array
            (
                [id] => 50615
                [des] => PHARMA Pro&Pack Expo 2015 is organized by IPMMA and will be held at Mumbai, the economic capital of India. This show helps the exhibitors from all ove
                [membership] => 0
                [name] => Pharma Pro&Pack Expo
                [abbr_name] => Pharma Pro&Pack Expo
                [paid] => 0
                [event_wrapper] => 33587
                [event_samll_wrapper] => http://im.gifbt.com/industry/27-350x210.jpg
                [event_url] => pharma-propack-expo
                [website] => http://www.pharmapropack.com
                [eventType] => 1
                [venue_name] => Bombay Convention & Exhibition Centre (BCEC)
                [startDate] => 2015-05-13
                [endDate] => 2015-05-15
                [city] => Mumbai
                [country] => India
                [country_url] => india
                [country_shortname] => India
                [industry_id] => 27
                [industry_name] => Medical & Pharmaceutical
                [industry_url] => medical-pharma
                [event_status] => 
                [total_visitors] => 144
                [total_exhibitor] => 0
                [total_speakers] => 0
            )

            [1] => Array
            (
                [id] => 57271
                [des] => The Iphex is a well acknowledged event in the pharmaceutical and healthcare industry and with the presence of pharmaceutical products and equipments,
                [membership] => 0
                [name] => Iphex
                [abbr_name] => Iphex
                [paid] => 0
                [event_wrapper] => 41539
                [event_samll_wrapper] => http://im.gifbt.com/industry/27-350x210.jpg
                [event_url] => iphex
                [website] => http://iphex-india.com/
                [eventType] => 1
                [venue_name] => Bombay Exhibition Centre
                [startDate] => 2015-05-13
                [endDate] => 2015-05-15
                [city] => Mumbai
                [country] => India
                [country_url] => india
                [country_shortname] => India
                [industry_id] => 27
                [industry_name] => Medical & Pharmaceutical
                [industry_url] => medical-pharma
                [event_status] => 
                [total_visitors] => 134
                [total_exhibitor] => 120
                [total_speakers] => 0
            )

            [2] => Array
            (
                [id] => 175534
                [des] => The TENSYMP, organized by the CimlGlobal will take place from 13th May to the 15th May 2015 at the Courtyard by Marriott, India in Ahmedabad, India. T
                [membership] => 
                [name] => TENSYMP Ahmedabad
                [abbr_name] => TENSYMP Ahmedabad
                [paid] => 
                [event_wrapper] => 
                [event_samll_wrapper] => http://im.gifbt.com/industry/40-350x210.jpg
                [event_url] => tensymp-ahmedabad
                [website] => http://www.tensymp2015.org/
                [eventType] => 2
                [venue_name] => Gujarat International Finance Tec-City
                [startDate] => 2015-05-13
                [endDate] => 2015-05-15
                [city] => Ahmedabad
                [country] => India
                [country_url] => india
                [country_shortname] => India
                [industry_id] => 40
                [industry_name] => Scientific Instruments
                [industry_url] => scientific-instruments
                [event_status] => 
                [total_visitors] => 3
                [total_exhibitor] => 0
                [total_speakers] => 3
            )

js code

      var desktop="/app.php/notificationdetail";
        var http = new XMLHttpRequest
        url = desktop,
        params = url;
        http.open("POST", url, !0), http.setRequestHeader("Content-type", "application/json; charset=UTF-8"), http.setRequestHeader("Content-length", params.length), http.setRequestHeader("Connection", "close"), 
        http.onreadystatechange = function() {
        if (4 == http.readyState && 200 == http.status) {


        var obj=http.responseText;
        alert(typeof obj);//string
        var parsed = JSON.parse(obj);



        for (var c in obj) {
        var newElement = document.createElement('div');
        newElement.id = obj[c]; newElement.className = "notification";
        newElement.innerHTML = obj[c];
        document.body.appendChild(newElement);
        } 
        }
        }, http.send(params);

problem

  • i need to find length of array like if use php counts then it shows 2 array whereas if i use .length() in js it doesn"t return required result.

o/p should should be

  • count in notification should be 2 using javascript .
  • i need to show ['id'],['name'],['city'] in dynamic div.

i have tried using for loop

for (var x=0; x<obj.length; x++)
 {
   var name= obj[x].name;
    console.log(name);//outputs : undefined
 }
afeef
  • 547
  • 3
  • 9
  • 25
  • Why you are not using for loop instead of for in loop ? – Rayon May 14 '15 at 05:30
  • javacript doesn't have associative arrays...you will receive array of objects. Very helpful detailed post/tutorial: [access-process-nested-objects-arrays-or-json](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – charlietfl May 14 '15 at 05:45
  • 1
    @charlietfl Objects. http://www.quirksmode.org/js/associative.html – Mörre May 14 '15 at 05:56
  • can anyone help how to get length of arrays in js i have tried : var obj=http.responseText; console.log(obj.length); //it returning 23543 which is wrong . if handled through php (counts )its works fine .. – afeef May 14 '15 at 06:54
  • we cannot find length of 2d arrays in js – afeef May 14 '15 at 06:55
  • i have tried : for(var i = 0; i < testArray.length; i++){console.log(testArray[i]);} – afeef May 14 '15 at 06:57
  • 1
    `obj` is just a json **string**, your loop is not going to do what you are expecting. Looping through `parsed` is the correct thing but without knowing the structure, it's hard to guess what's the internal will look like. – Jimmy Chandra May 14 '15 at 07:04
  • Or are you saying that calling `/app.php/notificationdetail` will actually return that `Array (...)` that you pasted above? If so, that's not a valid JSON format. Try paste that **url** (**http://..../app.php/notificationdetail**) in the browser, copy it and edit your question and paste the actual json content returned from php. – Jimmy Chandra May 14 '15 at 07:07
  • same array i m sending getting same array format in response.text – afeef May 14 '15 at 07:10
  • its not json its array format i need to fetch data from array is quite simple – afeef May 14 '15 at 07:23
  • i have tried to fetch for (var x=0; x – afeef May 14 '15 at 07:26

1 Answers1

0

On Getting the number of rows (object count) and columns (field count):

Not familiar w/ php Array, but it looks to me it's a one dimensional array with HashTable structure for each array element.

Just to confirm add console.log(parsed); after your var parsed = JSON.parse(obj); line. You should see something like [{id:50615,des:'PHARMA...',...},{id:57271,des:'The Iphex...',...},{...}].

If so, parsed.length should give you the number of objects (rows) in your data structure and Object.keys(parsed[0]).length will give you the number of columns / fields in the object.

NOTE: Only thing here is I am assuming that all objects in the array have equal numbers of fields. If not you might have to loop through the array and figure out the max length of each object in the array and use it as column count.

On displaying specific fields:

Once you understand how your array of objects are layout, you can just use Array.map function to pull out certain fields from inside your original array like so:

var model = parsed.map(function(o) { return { id: o.id, name: o.name, city: o.city }; });

and use the now model which now should only contains an array of object having id, name and city fields however you like.

You seems to already know how to do DOM manipulation so I won't go there. The only suggestion I can give is to either use string concatenation and bulk inject it to a DOM element innerHTML or use documentFragment approach for performance. See: http://jsperf.com/concat-vs-docfrag/4

Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
  • i have tried like console.log(obj.length); $(".notification1").append(obj.length); – afeef May 14 '15 at 06:54
  • i have tried for(var i = 0; i < testArray.length; i++){console.log(testArray[i]);} – afeef May 14 '15 at 06:57
  • That's why I said we need to see how that php Array is getting serialized into Json. And you can see the result by either doing `console.log(obj)` after `var obj = http.responseText` above and see how the JSON string looks like or I am guessing it will be what I am expecting in my answer. If not, once the JSON is known, it's just a matter of figuring that out. Without that, it's very hard to understand. JavaScript does not have 2 dimensional array, rather to do 2d array it will embed array within the container array like `[[..],[..],[..]]`. – Jimmy Chandra May 14 '15 at 07:02
  • same array i m sending getting same array format in response.text – afeef May 14 '15 at 07:10
  • Array ( [0] => Array ( [id] => 50615 ) – afeef May 14 '15 at 07:10
  • 1
    That's not a valid JSON then. You will need to parse that thing into a valid javascript object notation using some sort of a custom parser or have php return a valid JSON from server side. See: http://stackoverflow.com/questions/682260/returning-json-from-php-to-javascript – Jimmy Chandra May 14 '15 at 07:11