1

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'] ));
    }
}
2
  • What format are you expecting the result to be? Maybe provide a sample expected result? Commented Mar 13, 2018 at 21:39
  • Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and should not be used in new code. Commented Mar 13, 2018 at 21:56

1 Answer 1

1

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

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.