在 CodeIgniter 3 中访问关联数组中的数据

Accessing data in an associative array in CodeIgniter 3

我正在使用 CodeIgniter 3 并将关联数组从控制器传递到视图。在视图中,我正在尝试使用 ChartJS 呈现图表,因此我需要分别获取第 0 个和第 1 个索引的数据。 {"label":["A+","AB-","B+","B-","O+"],"data":["6","2","5","2","3"]} 是我传递的关联数组。下面是控制器中的代码。

public function bloodTypesChart()
    {
        $totalPackets = $this->db->count_all_results();

        $chartData = [];
        $blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE (isAvailable) = '1' GROUP BY blood_type")->result_array();

        $record = $this->db->from('packets')
            ->select('count(PacketID) count, BloodType blood_type')
            ->group_by('blood_type')
            ->get()
            ->result_array();

        foreach ($record as $row) {
            $chartData['label'][] = $row['blood_type'];
            $chartData['data'][] = $row['count'];
        }
        $chartData['chart_data'] = json_encode($chartData);
        $this->load->view('insight', $chartData);
    }

我需要将 labeldata 索引中的数据分别传递给 ChartJS 函数。我怎样才能在视图中做到这一点?

查看

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"/>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url('assests/styles/table.css'); ?>">
    <title>Kegalle Blood Bank - Blood Types Insight</title>
</head>
<body style="margin-bottom: 50px">
<?php include_once 'navbars/navbar2.php' ?>

<?php
if (!($this->session->userdata('isStaffLoggedIn'))) {
    redirect('home/login');
}
?>


<div class="container">
    <div class="row">
        <div class="col-md-12 text-center mt-5">
            <h1>Insight</h1>
        </div>
        <div class="row text-center m-5">
            <div class="col-md-6">
                <div class="card border-info mx-sm-1 p-3 shadow-lg">
                    <div class="card border-danger shadow text-danger p-3 my-card"><span class="fa fa-heart"
                                                                                         aria-hidden="true"></span>
                    </div>
                    <div class="text-danger text-center mt-3"><h4>Total Donors</h4></div>
                    <div class="text-danger text-center mt-2"><h1><?php echo $donorcount; ?></h1></div>

                </div>
            </div>
            <div class="col-md-6">
                <div class="card border-success mx-sm-1 p-3 shadow-lg">
                    <div class="card border-success shadow text-success p-3 my-card"><span class="fa fa-eye"
                                                                                           aria-hidden="true"></span>
                    </div>
                    <div class="text-success text-center mt-3"><h4>Total Available Packets</h4></div>
                    <div class="text-success text-center mt-2"><h1><?php echo $packetcount; ?></h1></div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="chart-container container">
    <div class="bar-chart-container">
        <canvas id="bar-chart" style="background: #fff"></canvas>
    </div>
</div>

<?php include_once 'footer/footer.php' ?>
</body>

<script>
    new Chart(document.getElementById("bar-chart"), {
        type: 'bar',
        data: {
            labels: <?php echo $label?>,
            datasets: [
                {
                    label: "Population (millions)",
                    backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                    data: <?php echo $data?>
                }
            ]
        },
        options: {
            legend: { display: false },
            title: {
                display: true,
                text: 'Predicted world population (millions) in 2050'
            }
        }
    });
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.2/dist/chart.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>

</html>

因为您希望您的变量出现在 Javascript 中,您需要 json_encode 它们。您的 $label 和 $data 仍在 PHP 中,您可以将其回显到 json.

在您看来,您可以执行以下操作

   data: {
        labels: <?= json_encode($label)?>,
        datasets: [
            {
                label: "Population (millions)",
                backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                data: <?= json_encode($data)?>
            }
        ]
    },