0

i wont create MapData for jvectormap from database, like this

{"AP":3,"DE":1,"GB":2,"ID":24,"US":10}

but my code always return like this

["{\"AP\":3}","{\"DE\":1}","{\"GB\":2}","{\"ID\":24}","{\"US\":10}"]

this my javascript

    $(document).ready(function () {

    var mapData ={} 
               $.ajax({ url: '/orders/map', type: 'GET', dataType: 'json' }).success(function (data) {
               mapData = (data);
            });
    var dd =    JSON.stringify( mapData, null, '\t'); 


        $('#world-map').vectorMap({
            map: 'world_mill_en',

            regionStyle: {
                initial: {
                    fill: '#e4e4e4',
                    "fill-opacity": 1,
                    stroke: 'none',
                    "stroke-width": 0,
                    "stroke-opacity": 0
                }
            },
            series: {
                regions: [{
                    values: dd,
                    scale: ["#1ab394", "#22d6b1"],
                    normalizeFunction: 'polynomial'
                }]
            },
            onRegionTipShow: function (e, el, code) {
                console.log(dd[code]);
                    var mapcode = ( dd[code] ) ? ' ( ' + dd[code] + ' Visitor/Visitors  )' : ' ( No visitor yet! )';
                el.html(el.html() + mapcode );
            }
        });
    }); 

this my codeigniter controllers

        function map(){
        $rows = array();
        $query = $this->db->query('SELECT country, COUNT(1) AS rpt_count FROM vistors GROUP BY country');   
        foreach($query->result() as $row)
        {    
        $rows[] = '{"'.$row->country.'":'.$row->rpt_count.'}';
        }      

    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($rows));
        }

comebody can help me please

gofur triad
  • 83
  • 2
  • 6
  • Probably duplicate: https://stackoverflow.com/questions/4935632/parse-json-in-javascript. You may use JSON.parse (data) – SphynxTech Aug 06 '17 at 18:19
  • You are making JSON encoding twice. In foreach loop try with `$rows[$row->country] = $row->rpt_count;`. This way you'll get an array with key like `US` and value like `10` etc. Just encode once in output class. – Tpojka Aug 06 '17 at 19:22
  • nice work thanks man – gofur triad Aug 07 '17 at 00:22

2 Answers2

0

You need to parse the data to get an object you can get data from.

var mapData = JSON.parse(data);
cal1fornia
  • 87
  • 7
0
<php
$query = $this->db->query('SELECT country, COUNT(1) AS rpt_count FROM vistors GROUP BY country');   
$rows = "{";
$data_count = 0;
$data_length = count($query);
foreach($query->result() as $row){
   $key = $row->country;
   $value = $row->rpt_count;
   $rows .=$key.':'.$value;
   $data_count +=1;
   if($data_count != $data_length){
       $rows .=',';
   }
}
$rows .= "}";
return $rows;
Tolga EGE
  • 11
  • 1