Asked  7 Months ago    Answers:  5   Viewed   49 times

I'm trying to plot (with Flot) a pie chart with some data

var data = <?php echo json_encode($data)?>

The result I get from that is this:

var data = [
{"label":"Crear Usuario", "data":"2"},
{"label":"Impresoras", "data":"1"},
{"label":"Problema Correo", "data":"1"},
{"label":"Requisicion Equipo", "data":"1"},
{"label":"Sitio Web", "data":"1"}
]

The problem here is that I need the label and data without the quotes, I already tried json_encode($data, JSON_NUMERIC_CHECK); but only removes the quotes from the numbers.

The following format is what I need:

var data = [
    {label:"Crear Usuario",data:2}, ...

 Answers

71

First, you have to generate your array in php so the data's value are integers, not strings:

I emulated your array from your json_encode(), I guess it looks like this (or it should):

$array =  array(
                array("label" => "Crear Usuario",   "data" => 2),
                array("label" => "Impresoras",      "data" => 1),
                array("label" => "Problema Correo", "data" => 1),
                array("label" => "Requisicion Equipo", "data" => 1),
                array("label" => "Sitio Web", "data" => 1)
            );

    $data = json_encode($array);
  • Notice that the 2 and 1's are unquoted, so this way they are integers, this is important.

Then you are missin in Javascript the JSON.parse() to actually make that output into a json object:

<script>
    var data = '<?php echo $data; ?>';
    var json = JSON.parse(data);
    console.log(json);
    console.log(json[0]);
</script>
  • Notice that var data = ... is SINGLE QUOTED, so you catch the echo from php as a String

The console.log()'s output this for me:

[Object, Object, Object, Object, Object] // First console.log(): one object with the 5 Objects. 
Object {label: "Crear Usuario", data: 2} // secons console log (json[0]) with the first object 

Looks like what you need, am I right?

Wednesday, March 31, 2021
 
FyodorX
answered 7 Months ago
75

Try to use JSON. PHP function json_encode()

EDIT: Code example(server side - PHP):

  // data handling
  $arrayToSend = array(array('name'=>'Most Recent', 'view'=>'recentView'),
                       array('name'=>'Most Popular', 'view'=>'popularView'),
                       array('name'=>'Staff Picks', 'view'=>'staffView'));

  echo json_encode($arrayToSend);

Client side(javascript). Note: YUI is used to show client-side handling:

var callback = {success: function(req) {
                            selectItems(req.responseText);
                         }
                };
YAHOO.util.Connect.asyncRequest('GET',url + '?param=1',callback);

function selectItems(resp) {

  var result = eval('(' + resp + ')');

  for (var i=0; i < result.length; i++) {
    // Do whatever you want with array result :)
  }
}

Comments: 1) In PHP script you have to make response, which outputs your array, previously encoded to JSON format. 2) Except YUI you can also use any appropriate JavaScript library to generate AJAX request(i.e. JQuery, Prototype). In my case, I used function eval() to make array from JSON response.

Hope, it'll help you.

Wednesday, March 31, 2021
 
waylaidwanderer
answered 7 Months ago
35

Yes, add @ before the field like:

$keyvisual_data = array(
    'video_file'            => @$row->field_field_video[0]['rendered']['#item']['uri'],
    'bild_file'             => @$row->field_field_bild[0]['rendered']['#item']['uri'],
    'subline_src'           => @$row->_field_data['nid']['entity']->field_key_titel['und'][0]['safe_value'],
    'screenreader_src'      => @$row->field_field_alt_screenreader[0]['rendered']['#markup'],
    'alt_src'               => @$row->field_field_bild[0]['rendered']['#item']['alt']
);

and then initialize the nulls:

if($keyvisual_data['video_file'] === null)
    $keyvisual_data['video_file'] = $default_video_file;

etc...

Saturday, May 29, 2021
 
Zigglzworth
answered 5 Months ago
39

If you would like to return JSON encoded data, use echo json_encode($array); - json_encode

On the javascript end of things, if you're using jQuery, use jQuery.parseJson()

If you're not using jQuery, you can use JSON.parse(xmlhttp.responseText)

EDIT

OP asked for a example of json_parse in action. http://jsfiddle.net/mxaP6/1/ Is a working example of parsing JSON in jQuery. In a real application, you would replace the string in jQuery.parseJSON() with the string returned from the server containing json data.

A better way to return data than sending an array in parse_json is to make an object, for example:

data.php

$data = new stdClass();
$data->name = "John";
$data->age = 24;
echo json_encode($data);

This would return json looking like this:

{"name":"John","age":24}

Which would parse to a javascript object. I've included the example data in the jsFiddle

Saturday, May 29, 2021
 
HamidR
answered 5 Months ago
99

Use Collections.shuffle() twice, with two Random objects initialized with the same seed:

long seed = System.nanoTime();
Collections.shuffle(fileList, new Random(seed));
Collections.shuffle(imgList, new Random(seed));

Using two Random objects with the same seed ensures that both lists will be shuffled in exactly the same way. This allows for two separate collections.

Friday, June 4, 2021
 
VieStar
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :