0

I just basically have a View with an Associative Array with Accidents Information.

The User will be able to click in the Country. When that happen I want to show them the information about accidents related with that country.

That information comes from PHP and the Click event is captured in JQuery...

How Can I insert the var country inside the index of the associative array that came from PHP with an Index for each country?

.on('click', function(i, row) {

        var country = row.label;

        accident_chart.setData([{
                a: <?php echo "".$charts['accidents_status'][**NEED var country value here**]['accidents']; ?>,
                y: 'Accidents',
            },
            {
                a: <?php echo $charts['accidents_status']['Qatar']['lost_time_accidents']; ?>,
                y: 'Lost Time',
            }
        ]);

    });
0

1 Answer 1

2

You can't do that. Use AJAX instead to get the accident_chart data.

.on('click', function(i, row) {

    var country = row.label;
    $.ajax({
        type: "GET",
        url: "/get_accident_chart", // Just replace it with your PHP controller function that can access your $charts variable
        data: {country: row.label},
        dataType: "json",
        success: function(data) {
        accident_chart.setData(data);
        },
        error: function(data) {
        return data;
        }
    });
});

In your PHP

get_accident_chart(){
    $country = $_GET['country'];
    $accident_chart = array(
        array(
            'a' => $charts['accidents_status'][$country]['accidents'],
            'y' => 'Accidents'
        ),
        array(
            'a' => $charts['accidents_status']['Qatar']['lost_time_accidents'],
            'y' => 'Lost Time'
        )
    );

    echo json_encode($accident_chart);
}
Sign up to request clarification or add additional context in comments.

Comments

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.