0

I have json data which looks as follows:

{
"firmware": [
    {
        "Request": "/Firmware/samsung",
        "Count": "2954"
    },
    {
        "Request": "/Firmware/apple",
        "Count": "2954"
    }
],
"exe": [
    {
        "Request": "/applications/appstore.exe",
        "Count": "4482"
    }
]    
}

I am trying to read it via PHP script and display it. But not seeing any data. The script is as follows:

<script type="text/javascript" src="http://localhost/json/softwarelogs.js"></script>
<fieldset class="light" style="width: 60%; margin-right: 20px; margin-bottom: 20px; float: left;" >
<legend>Top Firmware Downloads</legend>
<div class="statlist" id="topFirmware"> </div>
</fieldset>

<fieldset class="light" style="width: 60%; margin-right: 20px; margin-bottom: 20px; float: left;">
<legend>Top EXE Downloads</legend>
<div class="statlist" id="exe"> </div>
</fieldset>

<script>
   var htmlString = "<ol>";
   $.each(firmware, function(i, item) {
    htmlString = htmlString + "<li>" + item.Request;
    });

    $('#topFirmware').html(htmlString + "</ol>");
    htmlString = "<ol>";
    $.each(exe, function(i, item) {
    htmlString = htmlString + "<li>" + item.Request;
    });

    $('#exe').html(htmlString + "</ol>");

$.fn.digits = function(){
    return this.each(function(){
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
})

</script>

I am fairly new to PHP programming so not sure about the arrangement and syntax. Please correct me and guide me if something is wrong and if you need more information. Thanks in advance!!

deep
  • 686
  • 4
  • 17
  • 33

2 Answers2

1

You can't directly load (and use) JSON data by including it via script tags, try this instead:

$.getJSON('/json/softwarelogs.js', function(data) {
   var htmlString = "<ol>";
   $.each(data.firmware, function(i, item) {
    htmlString = htmlString + "<li>" + item.Request;
    });

    $('#topFirmware').html(htmlString + "</ol>");
    htmlString = "<ol>";
    $.each(exe, function(i, item) {
    htmlString = htmlString + "<li>" + item.Request;
    });

    $('#exe').html(htmlString + "</ol>");

    $.fn.digits = function(){
        return this.each(function(){
            $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
    })
});
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • _"You can't load (and use) JSON data by including it via script tags"_ Eh, yes you can. If you add `var x =` in front of it, but then, technically, you'd be loading a JavaScript object. – Cerbrus May 19 '15 at 05:58
  • @Rob - Should I insert it into the script tags or create a seperate PHP page? Thanks. – deep May 19 '15 at 06:06
  • No data is yet coming. Any suggestions? Thanks. – deep May 19 '15 at 07:30
  • 1
    @deep it depends, if your json is stored in a php variable then in you don't need the `getJSON`, just do something like `var data = ;`, if it's an array or object: `var data = ;`, then you can use it in your js. If your server outputs JSON at a url, then the `getJSON` method is appropriate. – Rob M. May 19 '15 at 16:38
0

Suppose your json is saved in a variable like

Note:- If its a simple string then you could change is to json using JSON.parse(jsonString);

var jsData={
"firmware": [
    {
        "Request": "/Firmware/samsung",
        "Count": "2954"
    },
    {
        "Request": "/Firmware/apple",
        "Count": "2954"
    }
],
"exe": [
    {
        "Request": "/applications/appstore.exe",
        "Count": "4482"
    }
]    
};

Now change the following statement

$.each(firmware, function(i, item)

to

$.each(jsData.firmware, function(i, item)
Let me see
  • 5,063
  • 9
  • 34
  • 47