0

I have a question about using data pulled from the database in javascript.

In my controller I have this:

$this->view->projects = $projectsarray;

I send the array to my view. There I will loop through my array and show the titles. Now I need to get that array in javascript because I want to create a highchart with the data...

Does anyone knows how I could do this easily?

EDIT: I now have this in my controller: (overviewcontroller)

public function getprojectdataAction($id){

}

In my javascript file:

var id = 1;
$.post('/overview/getprojectdata/',{id:id},function(data){
    alert("test");
});

But I get the error that the resource can't be found:

POST http://www.namesite.com/overview/getprojectdata/ 404 (Not Found)

What am I doing wrong?

4

3 回答 3

2

I assume $this->view->projects is array or object

in your view file

<script>

var projects = <?php echo json_encode($this->projects);?>;
console.log(projects);
</script>

then watch your firebug ...

于 2013-08-12T09:21:08.453 回答
1

you can do this by converting array to JSON you can send a ajax request to same controller

and return echo json_encode($array); then you can use directly with responce by decoding it...

$.post('controller/action',{id:id},function(data){
    var arrayJson=JSON.parse(data);
});

you can use arrayJson as data array...

于 2013-08-12T08:33:12.617 回答
0

you can use json_encode in the php part of the code to generate a javascript object out of your array. For example:

$php = array("myAttrib" => "hallo");
echo json_encode($php);

and in javascript you can use

alert (output.myAttrib);

, where output refers to echo json_encode(php). This code would open a box showing "hallo". Your question seems to be quite similar to how to use json_encode. For creating of a highchart also this post Highcharts data series issue with ajax/json and PHP could be of interest for you.

于 2013-08-12T08:07:13.143 回答