php - 如何将我的原生 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>
解决方案
阅读https://www.codeigniter.com/userguide3/database/connecting.html,这可能会帮助您开始使用 CI。祝你好运!
如果您还有其他问题,请尽量具体,因为这样更容易回答。例如,如何将原生 PHP 转换为 CI 是相当广泛的,并且不容易用几行来回答。
推荐阅读
- google-cloud-platform - 用于 DataFlow 和 DataPrep 的 ZONE_RESOURCE_POOL_EXHAUSTED
- r - R Bass Diffusion 使用 nls() 和预测器
- objective-c - 如何从 Objective-C 调用 Swift 可变参数方法?
- sql - 从 LEFT JOIN 中仅检索一条记录
- php - 在PHP中获取给定中心纬度/经度的圆的外部坐标
- c++ - 模板类的不同对象的通用接口
- ios - iOS - 如何使用自定大小的 CollectionView 单元格设置锚点以调整换行符的间距
- c# - 同时获取同一个 azure blob 的租约
- salesforce - 如果现有记录然后根据您更新某些字段创建新记录,如何在帐户对象上编写触发器?
- java - Map 的 putIfAbsent 实现中的冗余分配