0

my php code is this

$newArr = array();

$newArr['Item1']=$array1;
$newArr['Item2']=$array2;
$newArr['Item3']=$array3;
$newArr['Item4']=$array4;
$newArr['Item5']=$array5;
$newArr['Item6']=$array6;
$newArr['Item8']=$array8;

echo json_encode($newArr);

i wanted to build a line chart with chartjs with those data. so the chart would looks like this....

enter image description here

here is my current code js......

$(document).ready(function(){
$.ajax({
    url : "http://localhost/clicklog/clicklog.php",
    type : "GET",
    success : function(newArr){
        //console.log(newArr.Item1);


        Clicks1 = [];
        Clicks2 = [];
        Clicks3 = [];
        Clicks4 = [];
        Clicks5 = [];
        Clicks6 = [];
        Clicks8 = [];

        for(var i in newArr){

            //console.log(newArr[i]);

            for(var x in newArr[i]){
                //console.log(newArr[i][x]);
                Clicks1.push(newArr[i][x]);

            }

        }

        console.log(Clicks1);

with this code, my variable "Clicks1" has this value

[18948, 13783, 9484, 5468, 46267, 33511, 22824, 13204, 5378, 3773, 2564, 1586, 18076, 13252, 8979, 5394, 10273, 7139, 4713, 2632, 8812, 6326, 4465, 2751, 18083, 12924, 8886, 5354]

which is comes from "newArray" without sorting.

i wanted to put value in the "var clicks1" only for those data that comes from "item 1"

here is the result of my php code..

{"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}}

thank you for the help

1 Answer 1

1

i wanted to put value in the "var clicks1" only for those data that comes from "item 1"

Here's how you do that, but keep reading:

var newArr = {"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}};

var Clicks1 = [];
var data1 = newArr.Item1;
for (x in data1) {
   Clicks1.push(data1[x]);
}
console.log(Clicks1);

...but then you'll have to keep doing that for Clicks2 through Clicks7. Whenever you find yourself, in any programming language, with variables like "var1, var2, var3, var4" and so on, it tells you you want to use an array instead.

Without more context one can't say specifically how to change it, because we don't know what you're going to do with Clicks1 through Clicks7, but you'll most likely want to change the PHP to be dealing with arrays (the non-associative kind), and then using the resulting arrays in your JavaScript code.

But for what it's worth, this gives you a single array with the data from each ItemX in your existing response, in order by ItemX label, in an array called allclicks:

var newArr = {"Item1":{"2009-04-17":18948,"2009-04-18":13783,"2009-04-19":9484,"2009-04-20":5468},
"Item2":{"2009-04-17":46267,"2009-04-18":33511,"2009-04-19":22824,"2009-04-20":13204},
"Item3":{"2009-04-17":5378,"2009-04-18":3773,"2009-04-19":2564,"2009-04-20":1586},
"Item4":{"2009-04-17":18076,"2009-04-18":13252,"2009-04-19":8979,"2009-04-20":5394},
"Item5":{"2009-04-17":10273,"2009-04-18":7139,"2009-04-19":4713,"2009-04-20":2632},
"Item6":{"2009-04-17":8812,"2009-04-18":6326,"2009-04-19":4465,"2009-04-20":2751},
"Item8":{"2009-04-17":18083,"2009-04-18":12924,"2009-04-19":8886,"2009-04-20":5354}};

// An array we'll put arrays inside
var allclicks = [];
// Loop through the items in alphabetical order
Object.keys(newArr).sort().forEach(function(itemKey) {
  // Get the item for this key
  var item = newArr[itemKey];
  
  // Add an array to allclicks for this item's clicks
  itemclicks = allclicks[allclicks.length] = [];
  
  // Loop through this items dates to get the clicks
  for (date in item) {
    itemclicks.push(item[date]);
  }
});
console.log(allclicks);

Then you use allclicks[0], allclicks[1], etc., rather than Clicks1, Clicks2, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

your solution is work (y), you are great. sorry, i am quite new with "coding" or also can be said programming.
@MuhammadFebrik: Glad that's working! No need to apologize, we're all new at things all the time. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.