BillyMcNab
BillyMcNab

Reputation: 11

Iterating Through PHP Array for Chart Display

I'm having trouble iterating through a PHP array in order to display a chart. Right now, my code is only resulting in the display of one column in the chart (this column is displaying correctly), but I can't seem to get other columns to display.

This is my code right now (in a php section at the top of my html page). I know that the issue is with this section of code, because the chart is rendering perfectly, but just not adding a column for each record in the table.

I'd really appreciate any insight into the mistakes I'm making here. Thank you.

$valueAnimalType = $_POST['animaltype'];

$connect = mysqli_connect("127.0.0.1","____","_____",3306);
$result = mysqli_query($connect,"SELECT * FROM DISPOSAL");
$datas = array();

if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        $datas[] = $row;
    }

    foreach ($datas as $data){
        $datas = array(
        array('y' => $data[$valueAnimalType], "label" => $data['DisposalName'] ));
    }
}

Upvotes: 1

Views: 33

Answers (1)

bestestefan
bestestefan

Reputation: 871

modify your code here:

    foreach ($datas as $data) {

        //LINE BELOW    
        $datas [] =
            array('y' => $data[$valueAnimalType], "label" => $data['DisposalName']);
        //LINE ABOVE
    }

you're overwriting your $datas each time loop passes with last record, now it's appending new item to array

Upvotes: 1

Related Questions