首页 > 解决方案 > 如何将我的原生 PHP 代码转换为 Codeigniter 代码

问题描述

我想将我的原生 php 代码转换为关于图形统计的本机 php 代码,图表中填满了每个班级的学生总数,我使用 chart.js 进行统计,请帮我将代码转换为 CodeIgniter 代码,谢谢。

我一直试图将所有文件夹和代码放入控制器,但没有显示。而且我不知道如何将其转换为 CI。

这是我的 koneksi.php 代码:

<?php
    $koneksi = mysqli_connect("localhost","root","","grafik_mahasiswa");
?>

这是我的 index.php 代码:

<!DOCTYPE html>
<html>
<head>
    <title>Test and Try</title>
    <script type="text/javascript" src="chartjs/Chart.js"></script>
</head>
<body>
    <style type="text/css">
    body{
        font-family: roboto;
    }

    table{
        margin: 0px auto;
    }
    </style>


    <center>
        <h2>Try me</h2>
    </center>


    <?php 
    include 'koneksi.php';
    ?>

    <div style="width: 800px;margin: 0px auto;">
        <canvas id="myChart"></canvas>
    </div>

    <br/>
    <br/>

    <table border="1">
        <thead>
            <tr>
                <th>No</th>
                <th>Nama Mahasiswa</th>
                <th>NIM</th>
                <th>Fakultas</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $no = 1;
            $data = mysqli_query($koneksi,"select * from mahasiswa");
            while($d=mysqli_fetch_array($data)){
                ?>
                <tr>
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $d['nama']; ?></td>
                    <td><?php echo $d['nim']; ?></td>
                    <td><?php echo $d['fakultas']; ?></td>
                </tr>
                <?php 
            }
            ?>
        </tbody>
    </table>


    <script>
        var ctx = document.getElementById("myChart").getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ["Teknik", "Fisip", "Ekonomi", "Pertanian"],
                datasets: [{
                    label: '',
                    data: [
                    <?php 
                    $jumlah_teknik = mysqli_query($koneksi,"select * from mahasiswa where fakultas='teknik'");
                    echo mysqli_num_rows($jumlah_teknik);
                    ?>, 
                    <?php 
                    $jumlah_ekonomi = mysqli_query($koneksi,"select * from mahasiswa where fakultas='ekonomi'");
                    echo mysqli_num_rows($jumlah_ekonomi);
                    ?>, 
                    <?php 
                    $jumlah_fisip = mysqli_query($koneksi,"select * from mahasiswa where fakultas='fisip'");
                    echo mysqli_num_rows($jumlah_fisip);
                    ?>, 
                    <?php 
                    $jumlah_pertanian = mysqli_query($koneksi,"select * from mahasiswa where fakultas='pertanian'");
                    echo mysqli_num_rows($jumlah_pertanian);
                    ?>
                    ],
                    backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)'
                    ],
                    borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero:true
                        }
                    }]
                }
            }
        });
    </script>
</body>
</html>

标签: phpcodeignitermysqlicodeigniter-3

解决方案


阅读https://www.codeigniter.com/userguide3/database/connecting.html,这可能会帮助您开始使用 CI。祝你好运!

如果您还有其他问题,请尽量具体,因为这样更容易回答。例如,如何将原生 PHP 转换为 CI 是相当广泛的,并且不容易用几行来回答。


推荐阅读